2017-02-07 21:36:31 +00:00
#!/usr/bin/env python3
2017-03-21 21:26:16 +00:00
import ast , dbtools , html , io , logging , moviepy . editor , PIL . Image , random , setuptools , string , bottools . strings , telegram . ext , twitools , urllib . request , tweepy
2017-02-07 21:36:31 +00:00
2017-02-07 22:11:59 +00:00
logging . basicConfig ( format = ' %(asctime)s - %(name)s - %(levelname)s - %(message)s ' , level = logging . INFO )
2017-02-08 13:58:09 +00:00
logger = logging . getLogger ( __name__ )
def log ( bot , update , error ) :
logger . warn ( " Error %s caused by ' %s ' " % ( error , update ) )
2017-02-07 22:11:59 +00:00
2017-03-18 20:45:02 +00:00
def twoExceptions ( e , message ) :
2017-03-18 18:23:38 +00:00
text = {
2017-03-21 21:26:16 +00:00
32 : bottools . strings . badToken ,
36 : bottools . strings . selfSpam ,
64 : bottools . strings . accountSuspended ,
88 : bottools . strings . rateLimit ,
89 : bottools . strings . badToken ,
99 : bottools . strings . badToken ,
130 : bottools . strings . overload ,
131 : bottools . strings . twitterError ,
161 : bottools . strings . followLimit ,
185 : bottools . strings . tweetLimit ,
186 : bottools . strings . tweetLimit ,
187 : bottools . strings . dupTweet ,
205 : bottools . strings . rateLimit ,
226 : bottools . strings . automatedTweet ,
271 : bottools . strings . selfMute ,
272 : bottools . strings . notMuted ,
323 : bottools . strings . multipleGIFs ,
326 : bottools . strings . accountLocked ,
354 : bottools . strings . longTweet
2017-03-18 20:45:02 +00:00
} . get ( e . api_code )
2017-03-18 18:23:38 +00:00
2017-03-21 21:26:16 +00:00
message . reply_text ( text or bottools . strings . twoFail )
2017-03-18 18:23:38 +00:00
def getTwo ( message ) :
try :
return twitools . twoBotHelper ( message . chat_id )
except ValueError :
2017-03-21 21:26:16 +00:00
message . reply_text ( bottools . strings . noauth )
2017-03-18 18:23:38 +00:00
except tweepy . error . TweepError as e :
2017-03-20 13:32:41 +00:00
raise
2017-02-07 21:54:22 +00:00
def start ( bot , update ) :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . start % ( setuptools . botname ( ) , setuptools . botname ( ) ) )
2017-02-08 11:26:25 +00:00
def auth ( bot , update ) :
2017-02-10 17:41:28 +00:00
db = dbtools . dbHelper ( )
cid = update . message . chat_id
if not ( db . ato ( cid ) or db . ase ( cid ) ) :
auth = tweepy . OAuthHandler ( setuptools . cke ( ) , setuptools . cse ( ) )
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . auth % auth . get_authorization_url ( ) )
2017-02-10 17:41:28 +00:00
dbtools . dbHelper ( ) . storeToken ( cid , auth . request_token )
else :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . authimp )
2017-02-10 17:41:28 +00:00
def verify ( bot , update , args ) :
db = dbtools . dbHelper ( )
cid = update . message . chat_id
2017-02-07 21:54:22 +00:00
2017-02-10 17:41:28 +00:00
if db . ato ( cid ) and not db . ase ( cid ) :
auth = tweepy . OAuthHandler ( setuptools . cke ( ) , setuptools . cse ( ) )
auth . request_token = ast . literal_eval ( db . ato ( cid ) )
try :
auth . get_access_token ( args [ 0 ] )
dbtools . dbHelper ( ) . storeUser ( cid , auth . access_token , auth . access_token_secret )
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . verify )
2017-02-10 17:41:28 +00:00
except Exception as e :
dbtools . dbHelper ( ) . deleteUser ( update . message . chat_id )
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . verifyfail )
2017-02-10 17:41:28 +00:00
else :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . verifyimp )
2017-02-10 17:41:28 +00:00
2017-02-08 12:06:17 +00:00
def unauth ( bot , update ) :
2017-02-08 12:12:05 +00:00
dbtools . dbHelper ( ) . deleteUser ( update . message . chat_id )
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . unauth % setuptools . url ( ) )
2017-02-08 12:06:17 +00:00
2017-02-08 13:09:00 +00:00
def fish ( bot , update ) :
2017-02-08 14:23:15 +00:00
dbtools . dbHelper ( ) . addFish ( update . message . chat_id )
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . fishThanks )
2017-02-08 13:46:59 +00:00
2017-03-20 20:40:02 +00:00
def follow ( bot , update , args ) :
try :
two = getTwo ( update . message )
for user in args :
two . api . create_friendship ( screen_name = user )
except tweepy . error . TweepError as e :
twoExceptions ( e , update . message )
def unfollow ( bot , update , args ) :
try :
two = getTwo ( update . message )
for user in args :
two . api . destroy_friendship ( screen_name = user )
except tweepy . error . TweepError as e :
twoExceptions ( e , update . message )
2017-03-20 13:32:41 +00:00
def getTweetID ( tlid , cid ) :
try :
db = dbtools . dbHelper ( )
2017-03-20 13:42:09 +00:00
db . executeQuery ( " SELECT tid FROM timelines WHERE nr = %i AND cid = %i ; " % ( int ( tlid ) , int ( cid ) ) )
2017-03-20 13:32:41 +00:00
return db . getNext ( ) [ 0 ]
2017-03-20 13:42:09 +00:00
except Exception :
2017-03-20 13:32:41 +00:00
raise ValueError ( " No such tweet in timeline " )
def explicitTweet ( bot , update , args , reply = None ) :
2017-03-18 18:23:38 +00:00
try :
two = getTwo ( update . message )
2017-03-18 20:45:02 +00:00
2017-03-20 16:56:15 +00:00
if update . message . photo or update . message . document or update . message . video or update . message . sticker :
2017-03-21 21:08:21 +00:00
fid = update . message . document . file_id if update . message . document else update . message . sticker . file_id if update . message . sticker else update . message . video . file_id if update . message . video else update . message . photo [ - 1 ] . file_id
2017-03-18 20:45:02 +00:00
path = bot . getFile ( fid ) . file_path
media = urllib . request . urlopen ( path )
mobj = io . BytesIO ( media . read ( ) )
filename = path . split ( " / " ) [ - 1 ]
if filename . split ( " . " ) [ - 1 ] . lower ( ) == " webp " :
out = io . BytesIO ( )
2017-03-20 13:32:41 +00:00
PIL . Image . open ( mobj ) . convert ( ' RGB ' ) . save ( out , format = " JPEG " )
2017-03-18 20:45:02 +00:00
filename = " %s .jpg " % filename . split ( " . " ) [ 0 ]
2017-03-20 16:56:15 +00:00
if update . message . document and filename . split ( " . " ) [ - 1 ] . lower ( ) == " mp4 " :
temp = ' ' . join ( random . choice ( string . ascii_letters + string . digits ) for i in range ( 32 ) )
with open ( " tmp/ %s . %s " % ( temp , filename . split ( " . " ) [ - 1 ] ) , " wb " ) as f :
f . write ( mobj . getvalue ( ) )
moviepy . editor . VideoFileClip ( " tmp/ %s . %s " % ( temp , filename . split ( " . " ) [ - 1 ] ) ) . resize ( 0.3 ) . write_gif ( " tmp/ %s .gif " % temp )
filename = " %s .gif " % temp
out = open ( " tmp/ %s .gif " % temp , " rb " )
2017-03-20 13:32:41 +00:00
else :
out = mobj
2017-03-20 16:56:15 +00:00
2017-03-20 20:40:02 +00:00
two . api . update_with_media ( filename , update . message . caption , reply , file = out )
2017-03-20 16:56:15 +00:00
out . close ( )
2017-03-18 20:45:02 +00:00
else :
2017-03-20 20:40:02 +00:00
two . tweet ( ' ' . join ( args ) , reply )
2017-03-18 20:45:02 +00:00
2017-03-18 18:23:38 +00:00
except tweepy . error . TweepError as e :
2017-03-18 20:45:02 +00:00
twoExceptions ( e , update . message )
2017-03-20 13:32:41 +00:00
def reply ( bot , update , args ) :
try :
reply = getTweetID ( args [ 0 ] , update . message . chat_id )
except :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . cantfind % args [ 0 ] )
2017-03-20 13:32:41 +00:00
explicitTweet ( bot , update , args [ 1 : ] , reply )
def retweet ( bot , update , args ) :
for tweet in args :
try :
tid = getTweetID ( tweet , update . message . chat_id )
getTwo ( update . message ) . api . retweet ( tid )
except ValueError :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . cantfind % tweet )
2017-03-20 13:32:41 +00:00
except tweepy . error . TweepError as e :
twoExceptions ( e , update . message )
def like ( bot , update , args ) :
for tweet in args :
try :
tid = getTweetID ( tweet , update . message . chat_id )
getTwo ( update . message ) . api . create_favorite ( tid )
except ValueError :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . cantfind % tweet )
2017-03-20 13:32:41 +00:00
except tweepy . error . TweepError as e :
twoExceptions ( e , update . message )
2017-02-08 13:46:59 +00:00
def tweet ( bot , update ) :
2017-02-10 17:41:28 +00:00
try :
if dbtools . dbHelper ( ) . getTStatus ( update . message . chat_id ) :
2017-02-10 17:54:30 +00:00
explicitTweet ( bot , update , [ update . message . text ] )
2017-03-21 21:26:16 +00:00
except twepy . error . TweepError as e :
twoExceptions ( e , update . message )
2017-02-10 17:41:28 +00:00
def timeline ( bot , update , args = [ 10 ] ) :
try :
count = int ( args [ 0 ] )
except :
count = 10
2017-03-20 13:32:41 +00:00
db = dbtools . dbHelper ( )
db . executeQuery ( " DELETE FROM timelines WHERE cid = %i ; " % int ( update . message . chat_id ) )
2017-03-18 18:23:38 +00:00
try :
2017-03-20 13:32:41 +00:00
i = 1
2017-03-18 18:23:38 +00:00
two = getTwo ( update . message )
2017-03-20 13:32:41 +00:00
2017-03-18 18:23:38 +00:00
for status in two . api . home_timeline ( count = count ) :
2017-03-20 13:32:41 +00:00
db . executeQuery ( " INSERT INTO timelines VALUES( %i , %i , %i ); " % ( update . message . chat_id , i , status . id ) )
2017-03-20 14:44:12 +00:00
update . message . reply_text ( " Tweet %i : \n %s (@ %s ) at %s : \n %s " % ( i , status . author . name , status . author . screen_name , status . created_at , html . unescape ( status . text ) ) )
2017-03-20 13:32:41 +00:00
i + = 1
2017-03-18 18:23:38 +00:00
except tweepy . error . TweepError as e :
2017-03-18 20:45:02 +00:00
twoExceptions ( e , update . message )
2017-02-08 13:09:00 +00:00
2017-03-20 13:32:41 +00:00
db . commit ( )
2017-02-08 14:14:03 +00:00
def toggleTweet ( bot , update ) :
2017-02-08 14:19:01 +00:00
try :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . toggleTweet % ( bottools . strings . toggleTweetOn if dbtools . dbHelper ( ) . toggleTweet ( update . message . chat_id ) else bottools . strings . toggleTweetOff ) )
except tweepy . error . TweepError as e :
twoExceptions ( e , update . message )
2017-02-08 14:14:03 +00:00
2017-02-08 13:58:09 +00:00
def unknown ( bot , update ) :
2017-03-21 21:26:16 +00:00
update . message . reply_text ( bottools . strings . unknownCommand )
2017-02-08 13:58:09 +00:00
2017-02-10 17:41:28 +00:00
def test ( bot , update , args ) :
print ( args )
unknown ( bot , update )
2017-03-18 18:23:38 +00:00
if __name__ == " __main__ " :
updater = telegram . ext . Updater ( token = setuptools . token ( ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " auth " , auth ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " fish " , fish ) )
2017-03-20 20:40:02 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " follow " , follow , pass_args = True ) )
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " help " , start ) )
2017-03-20 13:32:41 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " like " , like , pass_args = True ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " reply " , reply , pass_args = True ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " retweet " , retweet , pass_args = True ) )
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " start " , start ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " test " , test , pass_args = True ) )
2017-03-21 21:08:21 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " timeline " , timeline , pass_args = True ) )
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " toggletweet " , toggleTweet ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " tweet " , explicitTweet , pass_args = True ) )
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " unauth " , unauth ) )
2017-03-20 20:40:02 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " unfollow " , unfollow , pass_args = True ) )
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_handler ( telegram . ext . CommandHandler ( " verify " , verify , pass_args = True ) )
2017-02-08 14:14:03 +00:00
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_handler ( telegram . ext . MessageHandler ( telegram . ext . Filters . text , tweet ) )
2017-03-18 20:45:02 +00:00
updater . dispatcher . add_handler ( telegram . ext . MessageHandler ( telegram . ext . Filters . photo , tweet ) )
updater . dispatcher . add_handler ( telegram . ext . MessageHandler ( telegram . ext . Filters . sticker , tweet ) )
updater . dispatcher . add_handler ( telegram . ext . MessageHandler ( telegram . ext . Filters . video , tweet ) )
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_handler ( telegram . ext . MessageHandler ( telegram . ext . Filters . command , unknown ) )
2017-03-20 16:56:15 +00:00
updater . dispatcher . add_handler ( telegram . ext . MessageHandler ( telegram . ext . Filters . document , tweet ) )
2017-02-08 13:58:09 +00:00
2017-03-18 18:23:38 +00:00
updater . dispatcher . add_error_handler ( log )
2017-02-07 21:54:22 +00:00
2017-03-18 18:23:38 +00:00
updater . start_polling ( )
updater . idle ( )