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
This commit is contained in:
Brian S. Stephan 2016-01-26 00:08:46 -06:00
parent e8e42cc580
commit 6cbf5f3d96
1 changed files with 34 additions and 6 deletions

View File

@ -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