diff options
author | Sean B. Palmer <sbp@aldebaran.local> | 2012-07-07 18:30:23 +0100 |
---|---|---|
committer | Sean B. Palmer <sbp@aldebaran.local> | 2012-07-07 18:30:23 +0100 |
commit | bac52dfb11ff9709eac08e1dc7231dc8055b8f63 (patch) | |
tree | c22919faca235b9bc31febc485fb1b8380ff1299 /modules | |
parent | 8a0d0dd9f0e98b3f2bc7637e0354f8b0a67b6b47 (diff) | |
download | bot-bac52dfb11ff9709eac08e1dc7231dc8055b8f63.tar.gz bot-bac52dfb11ff9709eac08e1dc7231dc8055b8f63.tar.bz2 bot-bac52dfb11ff9709eac08e1dc7231dc8055b8f63.zip |
Added a simple twitter module
Diffstat (limited to 'modules')
-rwxr-xr-x | modules/search.py | 2 | ||||
-rwxr-xr-x | modules/twitter.py | 85 |
2 files changed, 87 insertions, 0 deletions
diff --git a/modules/search.py b/modules/search.py index 13fadbb..b884d1c 100755 --- a/modules/search.py +++ b/modules/search.py @@ -119,6 +119,8 @@ def bing_search(query, lang='en-GB'): def bing(phenny, input): """Queries Bing for the specified input.""" + return phenny.reply("Sorry, .bing is disabled pending a QA review.") + query = input.group(2) if query.startswith(':'): lang, query = query.split(' ', 1) diff --git a/modules/twitter.py b/modules/twitter.py new file mode 100755 index 0000000..132d5cb --- /dev/null +++ b/modules/twitter.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +""" +twitter.py - Phenny Twitter Module +Copyright 2012, Sean B. Palmer, inamidst.com +Licensed under the Eiffel Forum License 2. + +http://inamidst.com/phenny/ +""" + +import re, time +import web + +r_username = re.compile(r'^[a-zA-Z0-9_]{1,15}$') +r_link = re.compile(r'^https?://twitter.com/\S+$') +r_p = re.compile(r'(?ims)(<p class="js-tweet-text.*?</p>)') +r_tag = re.compile(r'(?ims)<[^>]+>') +r_anchor = re.compile(r'(?ims)(<a.*?</a>)') +r_expanded = re.compile(r'(?ims)data-expanded-url=["\'](.*?)["\']') + +def entity(*args, **kargs): + return web.entity(*args, **kargs).encode('utf-8') + +def decode(html): + return web.r_entity.sub(entity, html) + +def expand(tweet): + def replacement(match): + anchor = match.group(1) + for link in r_expanded.findall(anchor): + return link + return r_tag.sub('', anchor) + return r_anchor.sub(replacement, tweet) + +def read_tweet(url): + bytes = web.get(url) + shim = '<div class="content clearfix">' + if shim in bytes: + bytes = bytes.split(shim, 1).pop() + + for text in r_p.findall(bytes): + text = expand(text) + text = r_tag.sub('', text) + text = text.strip() + text = text.replace('\r', '') + text = text.replace('\n', '') + return decode(text) + return "Sorry, couldn't get a tweet from %s" % url + +def format(tweet, username): + return '%s (@%s)' % (tweet, username) + +def user_tweet(username): + tweet = read_tweet('https://twitter.com/' + username + "?" + str(time.time())) + return format(tweet, username) + +def id_tweet(tid): + link = 'https://twitter.com/twitter/status/' + tid + data = web.head(link) + message, status = tuple(data) + if status == 301: + url = message.get("Location") + if not url: return "Sorry, couldn't get a tweet from %s" % link + username = url.split('/')[3] + tweet = read_tweet(url) + return format(tweet, username) + return "Sorry, couldn't get a tweet from %s" % link + +def twitter(phenny, input): + arg = input.group(2).strip() + if isinstance(arg, unicode): + arg = arg.encode('utf-8') + + if arg.isdigit(): + phenny.say(id_tweet(arg)) + elif r_username.match(arg): + phenny.say(user_tweet(arg)) + elif r_link.match(arg): + phenny.say(read_tweet(arg)) + else: phenny.reply("Give me a link, a username, or a tweet id") + +twitter.commands = ['tw', 'twitter'] +twitter.thread = True + +if __name__ == '__main__': + print __doc__ |