Updated twitter plugin with support for hashtags and tweet ids. #yolo
This commit is contained in:
parent
986567a97f
commit
27e05421fa
1 changed files with 52 additions and 26 deletions
|
@ -1,9 +1,12 @@
|
||||||
from util import hook, timesince
|
from util import hook, timesince
|
||||||
import tweepy
|
import tweepy
|
||||||
import re
|
import re
|
||||||
|
import random
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
@hook.command("tw")
|
||||||
|
@hook.command("twatter")
|
||||||
@hook.command
|
@hook.command
|
||||||
def twitter(inp, bot=None, say=None):
|
def twitter(inp, bot=None, say=None):
|
||||||
"twitter <user> [n] -- Gets last/[n]th tweet from <user>"
|
"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)
|
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:
|
if inp.find(' ') == -1:
|
||||||
username = inp
|
username = inp
|
||||||
tweet_number = 0
|
tweet_number = 0
|
||||||
|
@ -32,38 +51,45 @@ def twitter(inp, bot=None, say=None):
|
||||||
|
|
||||||
if tweet_number > 300:
|
if tweet_number > 300:
|
||||||
return "This command can only find the last \x02300\x02 tweets."
|
return "This command can only find the last \x02300\x02 tweets."
|
||||||
else:
|
|
||||||
username = inp
|
|
||||||
tweet_number = 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# try to get user by username
|
# try to get user by username
|
||||||
user = api.get_user(username)
|
user = api.get_user(username)
|
||||||
except tweepy.error.TweepError as e:
|
except tweepy.error.TweepError as e:
|
||||||
if e[0][0]['code'] == 34:
|
if e[0][0]['code'] == 34:
|
||||||
return "Could not find user."
|
return "Could not find user."
|
||||||
else:
|
else:
|
||||||
return "Error {}: {}".format(e[0][0]['code'], e[0][0]['message'])
|
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:
|
if user.verified:
|
||||||
prefix = "+"
|
prefix = "+"
|
||||||
else:
|
else:
|
||||||
prefix = ""
|
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())
|
time = timesince.timesince(tweet.created_at, datetime.utcnow())
|
||||||
|
|
||||||
return u"{}@\x02{}\x02 ({}): {} ({} ago)".format(prefix, user.screen_name, user.name, tweet.text, time)
|
return u"{}@\x02{}\x02 ({}): {} ({} ago)".format(prefix, user.screen_name, user.name, tweet.text, time)
|
||||||
|
|
Reference in a new issue