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