2010-01-17 04:24:36 +00:00
|
|
|
import random
|
2010-04-23 03:47:41 +00:00
|
|
|
import re
|
2010-01-17 04:24:36 +00:00
|
|
|
from time import strptime, strftime
|
2013-08-17 04:35:00 +00:00
|
|
|
from urllib import quote
|
2013-07-04 18:48:35 +00:00
|
|
|
|
2010-04-23 03:47:41 +00:00
|
|
|
from util import hook, http
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2014-01-14 21:12:37 +00:00
|
|
|
|
2013-07-04 18:48:35 +00:00
|
|
|
@hook.api_key('twitter')
|
2010-01-17 04:24:36 +00:00
|
|
|
@hook.command
|
2013-07-04 18:48:35 +00:00
|
|
|
def twitter(inp, api_key=None):
|
2013-08-17 04:35:00 +00:00
|
|
|
".twitter <user>/<user> <n>/<id>/#<search>/#<search> <n> -- " \
|
2014-01-14 21:12:37 +00:00
|
|
|
"get <user>'s last/<n>th tweet/get tweet <id>/do <search>/get <n>th <search> result"
|
2013-06-26 18:35:39 +00:00
|
|
|
|
2013-07-04 18:48:35 +00:00
|
|
|
if not isinstance(api_key, dict) or any(key not in api_key for key in
|
2014-01-14 21:12:37 +00:00
|
|
|
('consumer', 'consumer_secret', 'access', 'access_secret')):
|
2013-07-04 18:48:35 +00:00
|
|
|
return "error: api keys not set"
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
getting_id = False
|
2013-08-17 04:35:00 +00:00
|
|
|
doing_search = False
|
2013-09-21 00:35:42 +00:00
|
|
|
index_specified = False
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
if re.match(r'^\d+$', inp):
|
|
|
|
getting_id = True
|
2013-06-26 18:35:39 +00:00
|
|
|
request_url = "https://api.twitter.com/1.1/statuses/show.json?id=%s" % inp
|
2010-01-17 04:24:36 +00:00
|
|
|
else:
|
2013-07-30 20:47:01 +00:00
|
|
|
try:
|
|
|
|
inp, index = re.split('\s+', inp, 1)
|
|
|
|
index = int(index)
|
2013-09-21 00:35:42 +00:00
|
|
|
index_specified = True
|
2013-07-30 20:47:01 +00:00
|
|
|
except ValueError:
|
|
|
|
index = 0
|
|
|
|
if index < 0:
|
|
|
|
index = 0
|
|
|
|
if index >= 20:
|
|
|
|
return 'error: only supports up to the 20th tweet'
|
2013-08-17 04:35:00 +00:00
|
|
|
|
|
|
|
if re.match(r'^#', inp):
|
|
|
|
doing_search = True
|
|
|
|
request_url = "https://api.twitter.com/1.1/search/tweets.json?q=%s" % quote(inp)
|
|
|
|
else:
|
|
|
|
request_url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=%s" % inp
|
2013-07-04 18:48:35 +00:00
|
|
|
|
2010-01-17 04:24:36 +00:00
|
|
|
try:
|
2013-07-04 18:48:35 +00:00
|
|
|
tweet = http.get_json(request_url, oauth=True, oauth_keys=api_key)
|
2010-04-23 03:47:41 +00:00
|
|
|
except http.HTTPError, e:
|
2010-03-01 02:32:41 +00:00
|
|
|
errors = {400: 'bad request (ratelimited?)',
|
2014-01-14 21:12:37 +00:00
|
|
|
401: 'unauthorized',
|
|
|
|
403: 'forbidden',
|
|
|
|
404: 'invalid user/id',
|
|
|
|
500: 'twitter is broken',
|
|
|
|
502: 'twitter is down ("getting upgraded")',
|
|
|
|
503: 'twitter is overloaded (lol, RoR)',
|
|
|
|
410: 'twitter shut off api v1.'}
|
2010-01-17 04:24:36 +00:00
|
|
|
if e.code == 404:
|
|
|
|
return 'error: invalid ' + ['username', 'tweet id'][getting_id]
|
|
|
|
if e.code in errors:
|
|
|
|
return 'error: ' + errors[e.code]
|
2010-09-07 18:58:11 +00:00
|
|
|
return 'error: unknown %s' % e.code
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2013-08-17 04:35:00 +00:00
|
|
|
if doing_search:
|
|
|
|
try:
|
|
|
|
tweet = tweet["statuses"]
|
2013-09-21 00:35:42 +00:00
|
|
|
if not index_specified:
|
|
|
|
index = random.randint(0, len(tweet) - 1)
|
2013-08-17 04:35:00 +00:00
|
|
|
except KeyError:
|
|
|
|
return 'error: no results'
|
|
|
|
|
2013-07-04 18:48:35 +00:00
|
|
|
if not getting_id:
|
2013-07-30 20:47:01 +00:00
|
|
|
try:
|
|
|
|
tweet = tweet[index]
|
|
|
|
except IndexError:
|
2013-08-17 04:35:00 +00:00
|
|
|
return 'error: not that many tweets found'
|
2013-07-04 18:48:35 +00:00
|
|
|
|
2014-08-13 19:43:05 +00:00
|
|
|
if 'retweeted_status' in tweet:
|
|
|
|
rt = tweet["retweeted_status"]
|
|
|
|
rt_text = http.unescape(rt["text"]).replace('\n', ' ')
|
|
|
|
text = "RT @%s %s" % (rt["user"]["screen_name"], rt_text)
|
|
|
|
else:
|
|
|
|
text = http.unescape(tweet["text"]).replace('\n', ' ')
|
2013-07-04 18:48:35 +00:00
|
|
|
screen_name = tweet["user"]["screen_name"]
|
|
|
|
time = tweet["created_at"]
|
|
|
|
|
2014-01-14 21:12:37 +00:00
|
|
|
time = strftime('%Y-%m-%d %H:%M:%S',
|
|
|
|
strptime(time, '%a %b %d %H:%M:%S +0000 %Y'))
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2013-10-08 23:26:34 +00:00
|
|
|
return "%s \x02%s\x02: %s" % (time, screen_name, text)
|
|
|
|
|
2014-01-14 21:12:37 +00:00
|
|
|
|
2013-10-08 23:26:34 +00:00
|
|
|
@hook.api_key('twitter')
|
|
|
|
@hook.regex(r'https?://twitter.com/(#!/)?([_0-9a-zA-Z]+)/status/(\d+)')
|
|
|
|
def show_tweet(match, api_key=None):
|
2014-01-14 21:12:37 +00:00
|
|
|
return twitter(match.group(3), api_key)
|