当前位置: 首页>>代码示例>>Python>>正文


Python Twitter.direct_messages方法代码示例

本文整理汇总了Python中twitter.Twitter.direct_messages方法的典型用法代码示例。如果您正苦于以下问题:Python Twitter.direct_messages方法的具体用法?Python Twitter.direct_messages怎么用?Python Twitter.direct_messages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twitter.Twitter的用法示例。


在下文中一共展示了Twitter.direct_messages方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from twitter import Twitter [as 别名]
# 或者: from twitter.Twitter import direct_messages [as 别名]
class TweetBot:
    def __init__(self, tUser, tPassword):
        self.tbox = Twitter(tUser, tPassword)

    def recordMessage(self, message):
        # Need to parse the really lame timestamp twitter uses in 'created_at'
        dm = DirectMessages(messageID = message['id'])
        # Expect the message to be  'follow XX'  where XX is a state
        # abbreveiation
        action = message['text'].split()[0].lower()
        state = message['text'].split()[1].upper()
        sender = message['sender_screen_name']

        follower=Follower.get_by(userid = sender)
        if not follower:
            follower=Follower(userid = sender)
            session.commit()

        ustate = UnitedStates.get_by(value=state)
        if not ustate:
            self.tbox.direct_messages.new(user = sender,
                                          text=SORRY_MSG % sender)
            return

        if action == 'follow':
            fstate = FollowerStates.get_by(value='Active')
            follower.fState = fstate
            follower.uState = ustate
            follower.update()
            self.tbox.direct_messages.new(user = sender,
                                          text = FOLLOW_SUCCESS_MSG %
                                          (state,state))
        elif action == 'silence':
            fstate = FollowerStates.get_by(value='Slienced')
            follower.fState = fstate
            follower.uState = ustate
            follower.update()
            self.tbox.direct_messages.new(user = sender,
                                          text = SILENCE_SUCCESS_MSG %
                                          (state,state))
        elif action == 'stop':
            fstate = FollowerStates.get_by(value='Inactive')
            follower.fState = fstate
            follower.uState = ustate
            follower.update()
            self.tbox.direct_messages.new(user = sender,
                                          text = STOP_SUCCESS_MSG %
                                          (state,state))
        else:
            self.tbox.direct_messages.new(user = sender,
                                          text = UNKNOWN_MSG % sender)

        session.commit()


    def getMessages(self):
        prevMsgs = DirectMessages.query().order_by(
            desc(DirectMessages.messageTime)).limit(1).all()
        if len(prevMsgs) < 1:
            lastMessageID = None
        else:
            lastMessageID = prevMsgs[0].messageID
        print "Getting Direct Messages since %s " %(lastMessageID)
        messages = self.tbox.direct_messages(since_id = lastMessageID)
        for aMessage in messages:
            self.recordMessage(aMessage)


    def makeFriends(self):
        currentFriends=set(self.tbox.friends.ids.stormwarn())
        followers=set(self.tbox.followers.ids.stormwarn())
        newFriends=followers - currentFriends
        for aFriend in newFriends:
            try:
                self.tbox.friendships.create(id=aFriend)
            except Exception:
                print "Failed to follow %s" % aFriend
                continue
            screen_name=self.tbox.users.show(id = aFriend)['screen_name']
            self.tbox.direct_messages.new(user = screen_name,
                                          text = FOLLOWING_MSG)
        session.commit()

    def run(self):
        while True:
            print("Making friends and processing messages.")
            self.makeFriends()
            self.getMessages()
            time.sleep(900)
开发者ID:matburt,项目名称:stormtweet,代码行数:91,代码来源:tweetBot.py


注:本文中的twitter.Twitter.direct_messages方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。