From 692fbde76c5c676c5cf0d9a34aab7605c2843860 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Tue, 17 Nov 2009 20:18:32 -0700 Subject: [PATCH] support '.twitter #hashtag' searching. closes #3 --- plugins/twitter.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/plugins/twitter.py b/plugins/twitter.py index c1e08d7..d887d81 100644 --- a/plugins/twitter.py +++ b/plugins/twitter.py @@ -4,6 +4,7 @@ retrieves most recent tweets """ import re +import random import urllib2 from lxml import etree from time import strptime, strftime @@ -31,23 +32,26 @@ def twitter(inp): return twitter.__doc__ + url = 'http://twitter.com' getting_nth = False getting_id = False - if re.match('^\d+$', inp): + searching_hashtag = False + if re.match(r'^\d+$', inp): getting_id = True - url = 'statuses/show/%s.xml' % inp - elif re.match('^\w{,15}$', inp): - url = 'statuses/user_timeline/%s.xml?count=1' % inp - elif re.match('^\w{,15}\s+\d+$', inp): + url += '/statuses/show/%s.xml' % inp + elif re.match(r'^\w{1,15}$', inp): + url += '/statuses/user_timeline/%s.xml?count=1' % inp + elif re.match(r'^\w{1,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) + url += '/statuses/user_timeline/%s.xml?count=1&page=%s' % (name, num) + elif re.match(r'^#\w{1,15}$', inp): + url = 'http://search.twitter.com/search.atom?q=%23' + inp[1:] + searching_hashtag = True else: return 'error: invalid username' - - url = 'http://twitter.com/' + url try: xml = urllib2.urlopen(url).read() @@ -66,6 +70,12 @@ def twitter(inp): tweet = etree.fromstring(xml) + if searching_hashtag: + ns = '{http://www.w3.org/2005/Atom}' + id = random.choice(tweet.findall(ns + 'entry/' + ns + 'id')).text + id = id[id.rfind(':') + 1:] + return twitter(id) + if not getting_id: tweet = tweet.find('status') if tweet is None: