Gave plugins/twitter.py a facelift

This commit is contained in:
Luke Rogers 2012-02-26 02:32:48 +13:00
parent 8dbf9b6a65
commit 22c75d04eb

View file

@ -1,13 +1,14 @@
""" """
twitter.py: written by Scaevolus 2009 twitter.py: written by Scaevolus 2009, modified by Lukeroge 2012
retrieves most recent tweets retrieves most recent tweets
""" """
import random import random
import re import re
from time import strptime, strftime from time import strptime, strftime
from datetime import datetime
from util import hook, http from util import hook, http, timesince
def unescape_xml(string): def unescape_xml(string):
@ -24,6 +25,30 @@ def unescape_xml(string):
history = [] history = []
history_max_size = 250 history_max_size = 250
def parseDateTime(s):
if s is None:
return None
m = re.match(r'(.*?)(?:\.(\d+))?(([-+]\d{1,2}):(\d{2}))?$',
str(s))
datestr, fractional, tzname, tzhour, tzmin = m.groups()
if tzname is None:
tz = None
else:
tzhour, tzmin = int(tzhour), int(tzmin)
if tzhour == tzmin == 0:
tzname = 'UTC'
tz = FixedOffset(timedelta(hours=tzhour,
minutes=tzmin), tzname)
x = datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S")
if fractional is None:
fractional = '0'
fracpower = 6 - len(fractional)
fractional = float(fractional) * (10 ** fracpower)
return x.replace(microsecond=int(fractional), tzinfo=tz)
@hook.command @hook.command
def twitter(inp): def twitter(inp):
@ -44,7 +69,7 @@ def twitter(inp):
if inp[0] == '@': if inp[0] == '@':
reply_inp = find_reply(inp[1:]) reply_inp = find_reply(inp[1:])
if reply_inp == None: if reply_inp == None:
return 'error: no replies to %s found' % inp return 'No replies to %s found.' % inp
inp = reply_inp inp = reply_inp
url = 'http://twitter.com' url = 'http://twitter.com'
@ -105,18 +130,18 @@ def twitter(inp):
ns = '{http://www.w3.org/2005/Atom}' ns = '{http://www.w3.org/2005/Atom}'
tweets = tweet.findall(ns + 'entry/' + ns + 'id') tweets = tweet.findall(ns + 'entry/' + ns + 'id')
if not tweets: if not tweets:
return 'error: hashtag not found' return 'Hashtag not found!'
id = random.choice(tweets).text id = random.choice(tweets).text
id = id[id.rfind(':') + 1:] id = id[id.rfind(':') + 1:]
return twitter(id) return twitter(id)
if getting_nth: if getting_nth:
if tweet.find('status') is None: if tweet.find('status') is None:
return 'error: user does not have that many tweets' return 'User does not have that many tweets!'
time = tweet.find(time) time = tweet.find(time)
if time is None: if time is None:
return 'error: user has no tweets' return 'User has no tweets!'
reply_name = tweet.find(reply_name).text reply_name = tweet.find(reply_name).text
reply_id = tweet.find(reply_id).text reply_id = tweet.find(reply_id).text
@ -125,10 +150,15 @@ def twitter(inp):
reply_user is not None): reply_user is not None):
add_reply(reply_name, reply_id or -1) add_reply(reply_name, reply_id or -1)
time = strftime('%Y-%m-%d %H:%M:%S', time_raw = strftime('%Y-%m-%d %H:%M:%S',
strptime(time.text, strptime(time.text,
'%a %b %d %H:%M:%S +0000 %Y')) '%a %b %d %H:%M:%S +0000 %Y'))
time_pretty = timesince.timesince(parseDateTime(time_raw), datetime.utcnow())
print time_pretty
text = unescape_xml(tweet.find(text).text.replace('\n', '')) text = unescape_xml(tweet.find(text).text.replace('\n', ''))
screen_name = tweet.find(screen_name).text screen_name = tweet.find(screen_name).text
return "%s %s: %s" % (time, screen_name, text) return "\x02@%s\x02: %s [ %s ago ]" % (screen_name, text, time_pretty)