Updated twitter module to allow fetching replied-to tweets.

This commit is contained in:
Hamled 2010-01-16 19:03:10 -08:00
parent 5fe0bb0d7e
commit fc0244c952
1 changed files with 30 additions and 4 deletions

View File

@ -23,29 +23,50 @@ def unescape_xml(string):
return string.replace('&gt;', '>').replace('&lt;', '<').replace('&apos;', return string.replace('&gt;', '>').replace('&lt;', '<').replace('&apos;',
"'").replace('&quote;', '"').replace('&amp;', '&') "'").replace('&quote;', '"').replace('&amp;', '&')
history_max_size = 250
@hook.command @hook.command
def twitter(inp): def twitter(inp, history = list()):
".twitter <user>/<user> <n>/<id>/#<hashtag> -- gets last/<n>th tweet from"\ ".twitter <user>/<user> <n>/<id>/#<hashtag>/@<user> -- gets last/<n>th tweet from"\
"<user>/gets tweet <id>/gets random tweet with #<hashtag>" "<user>/gets tweet <id>/gets random tweet with #<hashtag>/gets replied tweet from @<user>"
inp = inp.strip() inp = inp.strip()
if not inp: if not inp:
return twitter.__doc__ return twitter.__doc__
def add_reply(reply_name, reply_id):
if len(history) == history_max_size:
history.pop()
history.insert(0, (reply_name, reply_id))
def find_reply(reply_name):
for name, id in history:
if name == reply_name:
return id
return None
url = 'http://twitter.com' url = 'http://twitter.com'
getting_nth = False getting_nth = False
getting_id = False getting_id = False
searching_hashtag = False searching_hashtag = False
if inp[0] == '@':
reply_id = find_reply(inp[1:])
if reply_id == None:
return 'error: no replies to %s found' % inp
inp = reply_id
time = 'status/created_at' time = 'status/created_at'
text = 'status/text' text = 'status/text'
reply_name = 'status/in_reply_to_screen_name'
reply_id = 'status/in_reply_to_status_id'
if re.match(r'^\d+$', inp): if re.match(r'^\d+$', inp):
getting_id = True getting_id = True
url += '/statuses/show/%s.xml' % inp url += '/statuses/show/%s.xml' % inp
screen_name = 'user/screen_name' screen_name = 'user/screen_name'
time = 'created_at' time = 'created_at'
text = 'text' text = 'text'
reply_name = 'in_reply_to_screen_name'
reply_id = 'in_reply_to_status_id'
elif re.match(r'^\w{1,15}$', inp): elif re.match(r'^\w{1,15}$', inp):
url += '/users/show/%s.xml' % inp url += '/users/show/%s.xml' % inp
screen_name = 'screen_name' screen_name = 'screen_name'
@ -99,6 +120,11 @@ def twitter(inp):
if time is None: if time is None:
return 'error: user has no tweets' return 'error: user has no tweets'
reply_name = tweet.find(reply_name)
reply_id = tweet.find(reply_id)
if reply_name, reply_id:
add_reply(reply_name, reply_id)
time = strftime('%Y-%m-%d %H:%M:%S', time = 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'))