Twitter: when printing tweets, making the printing of the ID optional

add a flag to getstatus/getuserstatus that suppresses the printing of the ID
This commit is contained in:
Brian S. Stephan 2012-07-14 09:55:11 -05:00
parent 709a0cfd9a
commit a8fe6da14f
1 changed files with 28 additions and 19 deletions

View File

@ -39,8 +39,8 @@ class Twitter(Module):
Module.__init__(self, irc, config, server)
# setup regexes
getstatuspattern = "^!twitter\s+getstatus\s+(\S+)$"
getuserstatuspattern = "^!twitter\s+getuserstatus\s+(\S+)(\s+.*|$)"
getstatuspattern = "^!twitter\s+getstatus(\s+noid)?\s+(\S+)$"
getuserstatuspattern = "^!twitter\s+getuserstatus(\s+noid)?\s+(\S+)(\s+.*|$)"
tweetpattern = "^!twitter\s+tweet\s+(.*)"
gettokenpattern = "^!twitter\s+gettoken$"
authpattern = "^!twitter\s+auth\s+(\S+)$"
@ -165,10 +165,13 @@ class Twitter(Module):
match = self.getstatusre.search(what)
if match:
status = match.group(1)
print_id = True
if match.group(1):
print_id = False
status = match.group(2)
try:
tweet = self.twit.GetStatus(status)
return self._return_tweet_or_retweet_text(tweet=tweet, print_source=True)
return self._return_tweet_or_retweet_text(tweet=tweet, print_source=True, print_id=print_id)
except twitter.TwitterError as e:
return "Couldn't obtain status: " + str(e)
@ -177,8 +180,11 @@ class Twitter(Module):
match = self.getuserstatusre.search(what)
if match:
user = match.group(1)
index = match.group(2)
print_id = True
if match.group(1):
print_id = False
user = match.group(2)
index = match.group(3)
try:
if index:
@ -197,7 +203,7 @@ class Twitter(Module):
tweets = self.twit.GetUserTimeline(screen_name=user, count=count, include_rts=True)
if tweets:
tweet = tweets[-1*index]
return self._return_tweet_or_retweet_text(tweet=tweet)
return self._return_tweet_or_retweet_text(tweet=tweet, print_id=print_id)
except twitter.TwitterError as e:
return "Couldn't obtain status: " + str(e)
except ValueError as e:
@ -351,31 +357,34 @@ class Twitter(Module):
# set since_id
self._set_last_since_id(new_since_id)
def _return_tweet_or_retweet_text(self, tweet, print_source=False):
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.
"""
reply = ""
if tweet.retweeted_status:
retweet = tweet.retweeted_status
if print_source:
return "@%s (RT @%s): %s [%s]" % (tweet.user.screen_name.encode('utf-8', 'ignore'),
retweet.user.screen_name.encode('utf-8', 'ignore'),
super(Twitter, self)._unencode_xml(retweet.text.encode('utf-8', 'ignore')),
tweet.id)
reply = "@%s (RT @%s): %s" % (tweet.user.screen_name.encode('utf-8', 'ignore'),
retweet.user.screen_name.encode('utf-8', 'ignore'),
super(Twitter, self)._unencode_xml(retweet.text.encode('utf-8', 'ignore')))
else:
return "(RT @%s): %s [%s]" % (retweet.user.screen_name.encode('utf-8', 'ignore'),
super(Twitter, self)._unencode_xml(retweet.text.encode('utf-8', 'ignore')),
tweet.id)
reply = "(RT @%s): %s" % (retweet.user.screen_name.encode('utf-8', 'ignore'),
super(Twitter, self)._unencode_xml(retweet.text.encode('utf-8', 'ignore')))
else:
if print_source:
return "@%s: %s [%s]" % (tweet.user.screen_name.encode('utf-8', 'ignore'),
super(Twitter, self)._unencode_xml(tweet.text.encode('utf-8', 'ignore')),
tweet.id)
reply = "@%s: %s" % (tweet.user.screen_name.encode('utf-8', 'ignore'),
super(Twitter, self)._unencode_xml(tweet.text.encode('utf-8', 'ignore')))
else:
return "%s [%s]" % (super(Twitter, self)._unencode_xml(tweet.text.encode('utf-8', 'ignore')), tweet.id)
reply = "%s" % (super(Twitter, self)._unencode_xml(tweet.text.encode('utf-8', 'ignore')))
if print_id:
reply = reply + " [{0:d}]".format(tweet.id)
return reply
def _get_last_since_id(self):
"""Get the since_id out of the database."""