cleanup twitter plugin

This commit is contained in:
Ryan Hitchman 2013-07-04 13:48:35 -05:00
parent f3c034df2e
commit 4bc268f5a4
1 changed files with 18 additions and 23 deletions

View File

@ -1,20 +1,18 @@
import random import random
import re import re
from time import strptime, strftime from time import strptime, strftime
from xml.sax.saxutils import unescape
from util import hook, http from util import hook, http
@hook.api_key('twitter')
@hook.command @hook.command
def twitter(inp, bot=None): def twitter(inp, api_key=None):
".twitter <user>/<id> -- get <user>'s last tweet/get tweet <id>" ".twitter <user>/<id> -- get <user>'s last tweet/get tweet <id>"
api_keys = {} if not isinstance(api_key, dict) or any(key not in api_key for key in
api_keys['consumer'] = bot.config.get("api_keys", {}).get("twitter_consumer", None) ('consumer', 'consumer_secret', 'access', 'access_secret')):
api_keys['consumer_secret'] = bot.config.get("api_keys", {}).get("twitter_consumer_secret", None)
api_keys['access'] = bot.config.get("api_keys", {}).get("twitter_access", None)
api_keys['access_secret'] = bot.config.get("api_keys", {}).get("twitter_access_secret", None)
for k in api_keys:
if api_keys[k] is None:
return "error: api keys not set" return "error: api keys not set"
getting_id = False getting_id = False
@ -26,7 +24,7 @@ def twitter(inp, bot=None):
request_url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=%s" % inp request_url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=%s" % inp
try: try:
tweet = http.get_json(request_url, oauth=True, oauth_keys=api_keys) tweet = http.get_json(request_url, oauth=True, oauth_keys=api_key)
except http.HTTPError, e: except http.HTTPError, e:
errors = {400: 'bad request (ratelimited?)', errors = {400: 'bad request (ratelimited?)',
401: 'unauthorized', 401: 'unauthorized',
@ -42,17 +40,14 @@ def twitter(inp, bot=None):
return 'error: ' + errors[e.code] return 'error: ' + errors[e.code]
return 'error: unknown %s' % e.code return 'error: unknown %s' % e.code
if getting_id: if not getting_id:
tweet = tweet[0]
text = tweet["text"] text = tweet["text"]
screen_name = tweet["user"]["screen_name"] screen_name = tweet["user"]["screen_name"]
time = tweet["created_at"] time = tweet["created_at"]
else:
text = tweet[0]["text"]
screen_name = tweet[0]["user"]["screen_name"]
time = tweet[0]["created_at"]
text = text.replace('&gt;', '>').replace('&lt;', '<').replace('&apos;',"'").replace('&quote;', '"').replace('&amp;', '&') text = unescape(text, {'&apos;': "'", "&quot;": '"'})
time = strftime('%Y-%m-%d %H:%M:%S', strptime(time, '%a %b %d %H:%M:%S +0000 %Y')) time = strftime('%Y-%m-%d %H:%M:%S', strptime(time, '%a %b %d %H:%M:%S +0000 %Y'))
return "%s %s: %s" % (time, screen_name, text) return "%s %s: %s" % (time, screen_name, text)