2013-07-02 03:28:46 +12:00
import re
2013-07-02 15:26:25 +12:00
import random
2013-07-02 06:27:01 +12:00
from datetime import datetime
2011-11-20 22:23:31 +13:00
2014-02-14 16:36:57 +13:00
import tweepy
from util import hook , timesince
2013-12-01 19:28:24 +13:00
TWITTER_RE = ( r " (?:(?:www.twitter.com|twitter.com)/(?:[-_a-zA-Z0-9]+)/status/)([0-9]+) " , re . I )
2012-05-17 11:52:31 +12:00
2013-12-01 19:37:12 +13:00
def get_api ( bot ) :
2013-07-02 03:28:46 +12:00
consumer_key = bot . config . get ( " api_keys " , { } ) . get ( " twitter_consumer_key " )
consumer_secret = bot . config . get ( " api_keys " , { } ) . get ( " twitter_consumer_secret " )
2011-11-20 22:23:31 +13:00
2013-07-02 03:28:46 +12:00
oauth_token = bot . config . get ( " api_keys " , { } ) . get ( " twitter_access_token " )
oauth_secret = bot . config . get ( " api_keys " , { } ) . get ( " twitter_access_secret " )
2011-11-20 22:23:31 +13:00
2013-07-02 03:28:46 +12:00
if not consumer_key :
2013-12-01 19:37:12 +13:00
return False
2011-11-20 22:23:31 +13:00
2013-07-02 03:28:46 +12:00
auth = tweepy . OAuthHandler ( consumer_key , consumer_secret )
auth . set_access_token ( oauth_token , oauth_secret )
2011-11-20 22:23:31 +13:00
2013-12-01 19:37:12 +13:00
return tweepy . API ( auth )
@hook.regex ( * TWITTER_RE )
def twitter_url ( match , bot = None ) :
2014-03-19 17:40:21 +13:00
# Find the tweet ID from the URL
2013-12-01 19:37:12 +13:00
tweet_id = match . group ( 1 )
2014-02-13 14:34:50 +13:00
2014-03-19 17:40:21 +13:00
# Get the tweet using the tweepy API
2013-12-01 19:37:12 +13:00
api = get_api ( bot )
if not api :
return
try :
tweet = api . get_status ( tweet_id )
user = tweet . user
except tweepy . error . TweepError :
return
2014-03-19 17:40:21 +13:00
# Format the return the text of the tweet
2013-12-01 19:37:12 +13:00
text = " " . join ( tweet . text . split ( ) )
if user . verified :
prefix = u " \u2713 "
else :
prefix = " "
time = timesince . timesince ( tweet . created_at , datetime . utcnow ( ) )
return u " {} @ \x02 {} \x02 ( {} ): {} ( {} ago) " . format ( prefix , user . screen_name , user . name , text , time )
2014-02-13 14:34:50 +13:00
2013-12-01 19:37:12 +13:00
@hook.command ( " tw " )
@hook.command ( " twatter " )
@hook.command
def twitter ( inp , bot = None ) :
2014-02-14 00:47:01 +13:00
""" twitter <user> [n] -- Gets last/[n]th tweet from <user> """
2013-12-01 19:37:12 +13:00
api = get_api ( bot )
if not api :
return " Error: No Twitter API details. "
2012-03-23 13:32:48 +13:00
2013-07-02 15:26:25 +12:00
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 :
2013-11-29 11:13:11 +13:00
return u " Error {} : {} " . format ( e [ 0 ] [ 0 ] [ ' code ' ] , e [ 0 ] [ 0 ] [ ' message ' ] )
2013-07-02 15:26:25 +12:00
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
2013-07-02 03:28:46 +12:00
if inp . find ( ' ' ) == - 1 :
username = inp
tweet_number = 0
else :
username , tweet_number = inp . split ( )
tweet_number = int ( tweet_number ) - 1
2012-02-29 00:29:53 -08:00
2013-07-02 03:28:46 +12:00
if tweet_number > 300 :
return " This command can only find the last \x02 300 \x02 tweets. "
2013-07-02 15:26:25 +12:00
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 :
2013-11-29 11:13:11 +13:00
return u " Error {} : {} " . format ( e [ 0 ] [ 0 ] [ ' code ' ] , e [ 0 ] [ 0 ] [ ' message ' ] )
2013-07-02 15:26:25 +12:00
# 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 :
2013-11-29 11:13:11 +13:00
return u " The user \x02 {} \x02 has no tweets. " . format ( user . screen_name )
2013-07-02 15:26:25 +12:00
# grab the newest tweet from the users timeline
try :
tweet = user_timeline [ tweet_number ]
except IndexError :
tweet_count = len ( user_timeline )
2013-11-29 11:13:11 +13:00
return u " The user \x02 {} \x02 only has \x02 {} \x02 tweets. " . format ( user . screen_name , tweet_count )
2013-07-02 15:26:25 +12:00
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
2014-02-14 00:47:01 +13:00
else :
# ???
2014-03-19 17:40:21 +13:00
return " Invalid Input "
2013-07-02 03:28:46 +12:00
2014-03-19 17:40:21 +13:00
# Format the return the text of the tweet
2013-07-02 21:50:58 +12:00
text = " " . join ( tweet . text . split ( ) )
2013-07-02 06:27:01 +12:00
if user . verified :
2013-11-29 11:18:25 +13:00
prefix = u " \u2713 "
2013-07-02 06:27:01 +12:00
else :
prefix = " "
time = timesince . timesince ( tweet . created_at , datetime . utcnow ( ) )
2012-02-29 00:29:53 -08:00
2013-07-02 21:50:58 +12:00
return u " {} @ \x02 {} \x02 ( {} ): {} ( {} ago) " . format ( prefix , user . screen_name , user . name , text , time )
2012-02-26 02:32:48 +13:00
2011-11-20 22:23:31 +13:00
2013-07-02 06:08:26 +12:00
@hook.command ( " twinfo " )
2011-11-20 22:23:31 +13:00
@hook.command
2013-07-02 03:28:46 +12:00
def twuser ( inp , bot = None ) :
2013-09-04 18:30:04 +08:00
""" twuser <user> -- Get info on the Twitter user <user> """
2013-07-02 03:28:46 +12:00
2013-12-01 19:37:12 +13:00
api = get_api ( bot )
if not api :
2013-07-02 06:08:26 +12:00
return " Error: No Twitter API details. "
2013-07-02 03:28:46 +12:00
2011-11-20 22:23:31 +13:00
try :
2013-07-02 03:28:46 +12:00
# try to get user by username
user = api . get_user ( inp )
except tweepy . error . TweepError as e :
if e [ 0 ] [ 0 ] [ ' code ' ] == 34 :
return " Could not find user. "
else :
return " Unknown error "
2011-11-20 22:23:31 +13:00
2013-07-02 06:27:01 +12:00
if user . verified :
2013-11-29 11:18:25 +13:00
prefix = u " \u2713 "
2013-07-02 06:27:01 +12:00
else :
prefix = " "
2013-09-10 14:49:33 +12:00
if user . location :
2013-11-29 11:13:11 +13:00
loc_str = u " is located in \x02 {} \x02 and " . format ( user . location )
2013-09-10 14:49:33 +12:00
else :
loc_str = " "
if user . description :
2013-11-29 11:13:11 +13:00
desc_str = u " The users description is \" {} \" " . format ( user . description )
2013-09-10 14:49:33 +12:00
else :
desc_str = " "
return u " {} @ \x02 {} \x02 ( {} ) {} has \x02 {:,} \x02 tweets and \x02 {:,} \x02 followers. {} " \
" " . format ( prefix , user . screen_name , user . name , loc_str , user . statuses_count , user . followers_count ,
desc_str )