2009-03-15 04:14:07 +00:00
|
|
|
"""
|
|
|
|
twitter.py: written by Scaevolus 2009
|
|
|
|
retrieves most recent tweets
|
|
|
|
"""
|
|
|
|
|
2009-07-10 06:41:09 +00:00
|
|
|
import re
|
2009-11-18 03:18:32 +00:00
|
|
|
import random
|
2009-07-10 06:41:09 +00:00
|
|
|
import urllib2
|
2009-03-17 00:55:54 +00:00
|
|
|
from lxml import etree
|
2009-11-18 01:21:11 +00:00
|
|
|
from time import strptime, strftime
|
2009-03-15 04:14:07 +00:00
|
|
|
|
2009-07-08 17:04:30 +00:00
|
|
|
from util import hook
|
2009-03-16 04:30:46 +00:00
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-06-23 06:45:58 +00:00
|
|
|
def unescape_xml(string):
|
|
|
|
# unescape the 5 chars that might be escaped in xml
|
|
|
|
|
|
|
|
# gratuitously functional
|
2009-07-08 17:28:15 +00:00
|
|
|
# return reduce(lambda x, y: x.replace(*y), (string,
|
2009-06-23 06:45:58 +00:00
|
|
|
# zip('> < ' "e; &'.split(), '> < \' " &'.split()))
|
|
|
|
|
|
|
|
# boring, normal
|
2009-07-08 17:28:15 +00:00
|
|
|
return string.replace('>', '>').replace('<', '<').replace(''',
|
|
|
|
"'").replace('"e;', '"').replace('&', '&')
|
|
|
|
|
2009-06-23 06:45:58 +00:00
|
|
|
|
2009-03-16 04:30:46 +00:00
|
|
|
@hook.command
|
2009-07-10 06:41:09 +00:00
|
|
|
def twitter(inp):
|
2009-11-18 01:21:11 +00:00
|
|
|
".twitter <user>/<user> <n>/<id> - gets last/<n>th tweet from <user>/gets tweet <id>"
|
2009-07-10 06:41:09 +00:00
|
|
|
inp = inp.strip()
|
|
|
|
if not inp:
|
2009-03-15 04:14:07 +00:00
|
|
|
return twitter.__doc__
|
|
|
|
|
2009-11-18 01:21:11 +00:00
|
|
|
|
2009-11-18 03:18:32 +00:00
|
|
|
url = 'http://twitter.com'
|
2009-11-18 01:21:11 +00:00
|
|
|
getting_nth = False
|
2009-07-10 06:41:09 +00:00
|
|
|
getting_id = False
|
2009-11-18 03:18:32 +00:00
|
|
|
searching_hashtag = False
|
|
|
|
if re.match(r'^\d+$', inp):
|
2009-07-10 06:41:09 +00:00
|
|
|
getting_id = True
|
2009-11-18 03:18:32 +00:00
|
|
|
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):
|
2009-11-18 01:21:11 +00:00
|
|
|
getting_nth = True
|
|
|
|
name, num = inp.split()
|
|
|
|
if int(num) > 3200:
|
|
|
|
return 'error: only supports up to the 3200th tweet'
|
2009-11-18 03:18:32 +00:00
|
|
|
url += '/statuses/user_timeline/%s.xml?count=1&page=%s' % (name, num)
|
2009-11-18 03:24:23 +00:00
|
|
|
elif re.match(r'^#\w+$', inp):
|
2009-11-18 03:18:32 +00:00
|
|
|
url = 'http://search.twitter.com/search.atom?q=%23' + inp[1:]
|
|
|
|
searching_hashtag = True
|
2009-07-10 06:41:09 +00:00
|
|
|
else:
|
2009-11-18 03:24:23 +00:00
|
|
|
return 'error: invalid request'
|
2009-07-10 06:41:09 +00:00
|
|
|
|
2009-03-24 22:53:56 +00:00
|
|
|
try:
|
2009-07-10 06:41:09 +00:00
|
|
|
xml = urllib2.urlopen(url).read()
|
|
|
|
except urllib2.HTTPError, e:
|
|
|
|
errors = {400 : 'bad request (ratelimited?)',
|
|
|
|
401: 'tweet is private',
|
|
|
|
404: 'invalid user/id',
|
|
|
|
500: 'twitter is broken',
|
|
|
|
502: 'twitter is down ("getting upgraded")',
|
|
|
|
503: 'twitter is overloaded (lol, RoR)'}
|
|
|
|
if e.code == 404:
|
|
|
|
return 'error: invalid ' + ['username', 'tweet id'][getting_id]
|
|
|
|
if e.code in errors:
|
|
|
|
return 'error: ' + errors[e.code]
|
|
|
|
return 'error: unknown'
|
|
|
|
|
|
|
|
tweet = etree.fromstring(xml)
|
2009-03-15 04:14:07 +00:00
|
|
|
|
2009-11-18 03:18:32 +00:00
|
|
|
if searching_hashtag:
|
|
|
|
ns = '{http://www.w3.org/2005/Atom}'
|
2009-11-18 03:24:23 +00:00
|
|
|
tweets = tweet.findall(ns + 'entry/' + ns + 'id')
|
|
|
|
if not tweets:
|
|
|
|
return 'error: hashtag not found'
|
|
|
|
id = random.choice(tweets).text
|
2009-11-18 03:18:32 +00:00
|
|
|
id = id[id.rfind(':') + 1:]
|
2009-11-18 03:24:23 +00:00
|
|
|
print id
|
2009-11-18 03:18:32 +00:00
|
|
|
return twitter(id)
|
|
|
|
|
2009-07-10 06:41:09 +00:00
|
|
|
if not getting_id:
|
|
|
|
tweet = tweet.find('status')
|
|
|
|
if tweet is None:
|
2009-11-18 01:21:11 +00:00
|
|
|
if getting_nth:
|
|
|
|
return 'error: user does not have that many tweets'
|
|
|
|
else:
|
|
|
|
return 'error: user has no tweets'
|
|
|
|
|
|
|
|
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', ''))
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-11-18 01:21:11 +00:00
|
|
|
return "%s %s: %s" % (time, screen_name, text)
|