From 6cbf5f3d96f841f5f0ff605ab29b760313a4fd13 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Tue, 26 Jan 2016 00:08:46 -0600 Subject: [PATCH] twitter: pull the tweet reply method into two _return_tweet_or_retweet_text used to both determine the proper tweet text and bot.reply() with it to the provided event. if we're not reacting to an irc event, this obviously won't work, so this pulls the method into two things so that we can use the string formatting code without necessary needing an event --- dr_botzo/twitter/ircplugin.py | 40 +++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/dr_botzo/twitter/ircplugin.py b/dr_botzo/twitter/ircplugin.py index b58e701..2dd208a 100644 --- a/dr_botzo/twitter/ircplugin.py +++ b/dr_botzo/twitter/ircplugin.py @@ -3,7 +3,6 @@ import logging import time -import requests import twython from django.conf import settings @@ -94,7 +93,8 @@ class Twitter(Plugin): status = match.group(3) try: tweet = self.twit.show_status(id=status) - return self._return_tweet_or_retweet_text(event, tweet=tweet, print_source=print_source, print_id=print_id) + return self._reply_with_tweet_or_retweet_text(event, tweet=tweet, print_source=print_source, + print_id=print_id) except Exception as e: log.error("couldn't obtain status") log.exception(e) @@ -130,8 +130,8 @@ class Twitter(Plugin): tweets = self.twit.get_user_timeline(screen_name=user, count=count, include_rts=True) if tweets: tweet = tweets[-1*index] - return self._return_tweet_or_retweet_text(event, tweet=tweet, print_source=print_source, - print_id=print_id) + return self._reply_with_tweet_or_retweet_text(event, tweet=tweet, print_source=print_source, + print_id=print_id) except Exception as e: log.error("couldn't obtain status") log.exception(e) @@ -232,9 +232,37 @@ class Twitter(Plugin): log.error("twitter settings object does not exist") return self.bot.reply(event, "twitter module not configured") - def _return_tweet_or_retweet_text(self, event, tweet, print_source=False, print_id=True): + 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. + + See _return_tweet_or_retweet_text for details. + + :param event: the irc event to use for the reply + :type event: Event + :param tweet: the tweet (from twython) to inspect and return a string for + :type tweet: dict + :param print_source: whether or not to print the tweet's author (default False) + :type print_source: bool + :param print_id: whether or not to print the tweet's ID (default True) + :type print_id: bool + :returns: tweet text suitable for printing + :rtype: str + """ + + return self.bot.reply(event, self._return_tweet_or_retweet_text(tweet, print_source, print_id)) + + def _return_tweet_or_retweet_text(self, tweet, print_source=False, print_id=True): """Return a string of the author and text body of a status, accounting for whether or not the fetched status is a retweet. + + :param tweet: the tweet (from twython) to inspect and return a string for + :type tweet: dict + :param print_source: whether or not to print the tweet's author (default False) + :type print_source: bool + :param print_id: whether or not to print the tweet's ID (default True) + :type print_id: bool + :returns: tweet text suitable for printing + :rtype: str """ retweet = getattr(tweet, 'retweeted_status', None) @@ -256,7 +284,7 @@ class Twitter(Plugin): if print_id: reply = reply + " [{0:d}]".format(tweet['id']) - return self.bot.reply(event, reply) + return reply plugin = Twitter