Updated twitter plugin with support for hashtags and tweet ids. #yolo

This commit is contained in:
Luke Rogers 2013-07-02 15:26:25 +12:00
parent 986567a97f
commit 27e05421fa

View file

@ -1,9 +1,12 @@
from util import hook, timesince
import tweepy
import re
import random
from datetime import datetime
@hook.command("tw")
@hook.command("twatter")
@hook.command
def twitter(inp, bot=None, say=None):
"twitter <user> [n] -- Gets last/[n]th tweet from <user>"
@ -22,7 +25,23 @@ def twitter(inp, bot=None, say=None):
api = tweepy.API(auth)
if re.match(r'^\w{1,15}$', inp) or re.match(r'^\w{1,15}\s+\d+$', inp):
if re.match(r'^\d+$', inp):
# user is getting a tweet by id
try:
# get tweet by id
tweet = api.get_status(inp)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 34:
return "Could not find tweet."
else:
return "Error {}: {}".format(e[0][0]['code'], e[0][0]['message'])
user = tweet.user
elif re.match(r'^\w{1,15}$', inp) or re.match(r'^\w{1,15}\s+\d+$', inp):
# user is getting a tweet by name
if inp.find(' ') == -1:
username = inp
tweet_number = 0
@ -32,38 +51,45 @@ def twitter(inp, bot=None, say=None):
if tweet_number > 300:
return "This command can only find the last \x02300\x02 tweets."
else:
username = inp
tweet_number = 0
try:
# try to get user by username
user = api.get_user(username)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 34:
return "Could not find user."
else:
return "Error {}: {}".format(e[0][0]['code'], e[0][0]['message'])
try:
# try to get user by username
user = api.get_user(username)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 34:
return "Could not find user."
else:
return "Error {}: {}".format(e[0][0]['code'], e[0][0]['message'])
# get the users tweets
user_timeline = api.user_timeline(id=user.id, count=tweet_number + 1)
# if the timeline is empty, return an error
if not user_timeline:
return "The user \x02{}\x02 has no tweets.".format(user.screen_name)
# grab the newest tweet from the users timeline
try:
tweet = user_timeline[tweet_number]
except IndexError:
tweet_count = len(user_timeline)
return "The user \x02{}\x02 only has \x02{}\x02 tweets.".format(user.screen_name, tweet_count)
elif re.match(r'^#\w+$', inp):
# user is searching by hashtag
search = api.search(inp)
if not search:
return "No tweets found."
tweet = random.choice(search)
user = tweet.user
if user.verified:
prefix = "+"
else:
prefix = ""
# get the users tweets
user_timeline = api.user_timeline(id=user.id, count=tweet_number + 1)
# if the timeline is empty, return an error
if not user_timeline:
return "The user \x02{}\x02 has no tweets.".format(user.screen_name)
# grab the newest tweet from the users timeline
try:
tweet = user_timeline[tweet_number]
except IndexError:
tweet_count = len(user_timeline)
return "The user \x02{}\x02 only has \x02{}\x02 tweets.".format(user.screen_name, tweet_count)
time = timesince.timesince(tweet.created_at, datetime.utcnow())
return u"{}@\x02{}\x02 ({}): {} ({} ago)".format(prefix, user.screen_name, user.name, tweet.text, time)