From 3b78e9b894e695106f487105dabdf7c9a8850f99 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Tue, 26 Jan 2016 00:39:51 -0600 Subject: [PATCH] twitter: method for polling mentions timeline not stitched together in this commit, so it'll work but it won't be started --- dr_botzo/twitter/ircplugin.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dr_botzo/twitter/ircplugin.py b/dr_botzo/twitter/ircplugin.py index 2dd208a..1df3dfd 100644 --- a/dr_botzo/twitter/ircplugin.py +++ b/dr_botzo/twitter/ircplugin.py @@ -28,6 +28,8 @@ class Twitter(Plugin): self.temp_token = None self.temp_token_secret = None + self.poll_mentions = False + super(Twitter, self).__init__(bot, connection, event) def start(self): @@ -79,6 +81,8 @@ class Twitter(Plugin): self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_auth) self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_replyto) + self.poll_mentions = False + super(Twitter, self).stop() def handle_getstatus(self, connection, event, match): @@ -232,6 +236,29 @@ class Twitter(Plugin): log.error("twitter settings object does not exist") return self.bot.reply(event, "twitter module not configured") + def thread_watch_mentions(self, sleep_time=60): + """Poll mentions from Twitter every sleep_time seconds. + + :param sleep_time: second to sleep between checks + :type sleep_time: int + """ + + while self.poll_mentions: + twittersettings = TwitterClient.objects.get(pk=1) + out_chan = twittersettings.mentions_output_channel.name + since_id = twittersettings.mentions_since_id + + mentions = self.twit.get_mentions_timeline(since_id=since_id) + for mention in mentions: + reply = self._return_tweet_or_retweet_text(tweet=mention, print_source=True) + self.bot.privmsg(out_chan, reply) + since_id = mention['id'] if mention['id'] > since_id else since_id + + twittersettings.mentions_since_id = since_id + twittersettings.save() + + time.sleep(sleep_time) + def _reply_with_tweet_or_retweet_text(self, event, tweet, print_source=False, print_id=True): """Do a bot.reply() with the appropriate text representation of the given tweet.