From e9f3beb86fea36545fb8b967327c3818ce697561 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Tue, 17 Nov 2009 18:21:11 -0700 Subject: [PATCH] support '.twitter ', closes issue #1 --- plugins/twitter.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/plugins/twitter.py b/plugins/twitter.py index 747ec1d..c1e08d7 100644 --- a/plugins/twitter.py +++ b/plugins/twitter.py @@ -6,6 +6,7 @@ retrieves most recent tweets import re import urllib2 from lxml import etree +from time import strptime, strftime from util import hook @@ -24,19 +25,29 @@ def unescape_xml(string): @hook.command def twitter(inp): - ".twitter / - gets last tweet from /gets tweet " + ".twitter / / - gets last/th tweet from /gets tweet " inp = inp.strip() if not inp: return twitter.__doc__ + + getting_nth = False getting_id = False if re.match('^\d+$', inp): getting_id = True - url = 'http://twitter.com/statuses/show/%s.xml' % inp + url = 'statuses/show/%s.xml' % inp elif re.match('^\w{,15}$', inp): - url = 'http://twitter.com/statuses/user_timeline/%s.xml?count=1' % inp + url = 'statuses/user_timeline/%s.xml?count=1' % inp + elif re.match('^\w{,15}\s+\d+$', inp): + getting_nth = True + name, num = inp.split() + if int(num) > 3200: + return 'error: only supports up to the 3200th tweet' + url = 'statuses/user_timeline/%s.xml?count=1&page=%s' % (name, num) else: return 'error: invalid username' + + url = 'http://twitter.com/' + url try: xml = urllib2.urlopen(url).read() @@ -58,7 +69,15 @@ def twitter(inp): if not getting_id: tweet = tweet.find('status') if tweet is None: - return 'error: user has no tweets' + if getting_nth: + return 'error: user does not have that many tweets' + else: + return 'error: user has no tweets' - return unescape_xml(': '.join(tweet.find(x).text.replace('\n', '') for x in - 'created_at user/screen_name text'.split())) + time = strftime('%Y-%m-%d %H:%M:%S', + strptime(tweet.find('created_at').text, + '%a %b %d %H:%M:%S +0000 %Y')) + screen_name = tweet.find('user/screen_name').text + text = unescape_xml(tweet.find('text').text.replace('\n', '')) + + return "%s %s: %s" % (time, screen_name, text)