def Run(self):
'''
All the high-level logic of the bot is driven from here:
- load settings
- connect to twitter
- (let your derived bot class get set up)
- either:
- wait for events from the streaming API
- do bot stuff:
- maybe create one or more tweets
- handle any mentions
- handle any streaming API events that were saved
- send tweets out
- (let your derived bot class clean up)
'''
self.LoadSettings()
# create the Twython object that's going to communicate with the
# twitter API.
appKey = self.settings.appKey
appSecret = self.settings.appSecret
accessToken = self.settings.accessToken
accessTokenSecret = self.settings.accessTokenSecret
if self.stream:
self.twitter = NanobotStreamer(appKey, appSecret, accessToken, accessTokenSecret)
self.twitter.SetOutputPath(self.botPath)
else:
self.twitter = Twython(appKey, appSecret, accessToken, accessTokenSecret)
# give the derived bot class a chance to do whatever it needs
# to do before we actually execute.
self.PreRun()
if self.stream:
if self.debug:
print "About to stream from user account."
try:
# The call to user() will sit forever waiting for events on
# our user account to stream down. Those events will be handled
# for us by the BotStreamer object that we created above
self.twitter.user()
except KeyboardInterrupt:
# disconnect cleanly from the server.
self.twitter.disconnect()
else:
self.CreateUpdate()
self.HandleMentions()
self.HandleStreamEvents()
self.SendTweets()
# if anything we did changed the settings, make sure those changes
# get written out.
self.settings.lastExecuted = str(datetime.now())
self.settings.Write()
# ...and let the derived bot class clean up as it needs to.
self.PostRun()
评论列表
文章目录