unescape > et. al. in twitter messages

This commit is contained in:
Ryan Hitchman 2009-06-23 00:45:58 -06:00
parent d485958f93
commit 2710d6a14a
1 changed files with 15 additions and 4 deletions

View File

@ -9,14 +9,25 @@ from lxml import etree
import hook import hook
def unescape_xml(string):
# unescape the 5 chars that might be escaped in xml
# gratuitously functional
# return reduce(lambda x, y: x.replace(*y), (string,
# zip('&gt; &lt; &apos; &quote; &amp'.split(), '> < \' " &'.split()))
# boring, normal
return string.replace('&gt;', '>').replace('&lt;', '<'). \
replace('&apos;', "'").replace('&quote;', '"').replace('&amp;', '&')
@hook.command @hook.command
def twitter(bot, input): def twitter(input):
".twitter <user> - gets most recent tweet from <user>" ".twitter <user> - gets most recent tweet from <user>"
if not input.inp.strip(): if not input.strip():
return twitter.__doc__ return twitter.__doc__
url = "http://twitter.com/statuses/user_timeline/%s.xml?count=1" \ url = "http://twitter.com/statuses/user_timeline/%s.xml?count=1" \
% urllib.quote(input.inp) % urllib.quote(input)
try: try:
tweet = etree.parse(url) tweet = etree.parse(url)
except IOError: except IOError:
@ -26,5 +37,5 @@ def twitter(bot, input):
return "can't find that username" return "can't find that username"
tweet = tweet.find('status') tweet = tweet.find('status')
bot.say(': '.join(tweet.find(x).text for x in return unescape_xml(': '.join(tweet.find(x).text for x in
'created_at user/screen_name text'.split())) 'created_at user/screen_name text'.split()))