merging
This commit is contained in:
commit
1115f8d1d9
|
@ -3,7 +3,8 @@ twitter.py: written by Scaevolus 2009
|
||||||
retrieves most recent tweets
|
retrieves most recent tweets
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import urllib
|
import re
|
||||||
|
import urllib2
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
from util import hook
|
from util import hook
|
||||||
|
@ -22,21 +23,42 @@ def unescape_xml(string):
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def twitter(input):
|
def twitter(inp):
|
||||||
".twitter <user> - gets most recent tweet from <user>"
|
".twitter <user>/<id> - gets last tweet from <user>/gets tweet <id>"
|
||||||
if not input.strip():
|
inp = inp.strip()
|
||||||
|
if not inp:
|
||||||
return twitter.__doc__
|
return twitter.__doc__
|
||||||
|
|
||||||
url = "http://twitter.com/statuses/user_timeline/%s.xml?count=1" \
|
getting_id = False
|
||||||
% urllib.quote(input)
|
if re.match('^\d+$', inp):
|
||||||
|
getting_id = True
|
||||||
|
url = 'http://twitter.com/statuses/show/%s.xml' % inp
|
||||||
|
elif re.match('^\w{,15}$', inp):
|
||||||
|
url = 'http://twitter.com/statuses/user_timeline/%s.xml?count=1' % inp
|
||||||
|
else:
|
||||||
|
return 'error: invalid username'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tweet = etree.parse(url)
|
xml = urllib2.urlopen(url).read()
|
||||||
except IOError:
|
except urllib2.HTTPError, e:
|
||||||
return 'error'
|
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'
|
||||||
|
|
||||||
if tweet.find('error') is not None:
|
tweet = etree.fromstring(xml)
|
||||||
return "can't find that username"
|
|
||||||
|
if not getting_id:
|
||||||
|
tweet = tweet.find('status')
|
||||||
|
if tweet is None:
|
||||||
|
return 'error: user has no tweets'
|
||||||
|
|
||||||
tweet = tweet.find('status')
|
|
||||||
return unescape_xml(': '.join(tweet.find(x).text.replace('\n','') for x in
|
return unescape_xml(': '.join(tweet.find(x).text.replace('\n','') for x in
|
||||||
'created_at user/screen_name text'.split()))
|
'created_at user/screen_name text'.split()))
|
||||||
|
|
Loading…
Reference in New Issue