Skip to content

Commit

Permalink
Telegram Bot: Integrate the existing autoretweet argument to telegram
Browse files Browse the repository at this point in the history
This commit does
- Checks whether the retweet arg is passed, if it is passed, telegram bot won't show the retweet button and will auto retweet.
- Default case is it won't auto retweet and will send the tweets to the telegram user
  • Loading branch information
iammarco11 authored and Aniketh01 committed May 13, 2020
1 parent ad76f57 commit 55e4b83
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 57 deletions.
65 changes: 32 additions & 33 deletions nawab.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import threading
import argparse
import logging

#TODO: Shouldn't import this here. Need a way to derive this from tg_bot instead.
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler
Expand All @@ -23,22 +24,17 @@ def retrieve_twitter_auth(self):
return tw_bot, api


<<<<<<< HEAD
def twitter_bot_run(self):
def twitter_bot_run(self, auto_retweet=None):
tw_bot, api = self.retrieve_twitter_auth()
tw_bot.nawab_curate_list(api)
tw_bot.nawab_retweet_tweet(api)
=======
def tg_bot_run(data, dirpath,level, isretweet=False, manual_tweet=False):
tw_bot, api = retrieve_twitter_auth(dirpath, data,level)
bot = tg_bot.Telegram_Bot(api, dirpath,level,isretweet, manual_tweet)
updater = bot.nawab_tg_authenticate()
>>>>>>> Nawab: Add arguments to incoporate auto-retweet and manual-retweet

if auto_retweet == True:
tw_bot.nawab_retweet_tweet(api)


def tg_bot_run(self):
def tg_bot_run(self, auto_retweet=None):
tw_bot, api = self.retrieve_twitter_auth()
bot = tg_bot.Telegram_Bot(api, self.dirpath)
bot = tg_bot.Telegram_Bot(api, self.dirpath, self.level, auto_retweet)
updater = bot.nawab_tg_authenticate()

dp = updater.dispatcher
Expand All @@ -64,60 +60,63 @@ def main():
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-r", "--retweet", help="Retweet all tweets automatically, doesn't spawn a telegram bot",
action='store_true', required=False)
parser.add_argument("-t", "--tg_bot", help="Doesn't retweet all automatically, can manually retweet through telegram bot",
action='store_true', required=False)
parser.add_argument("-b", "--blacklist",type=list, required=False, help="Blacklist the given username")
parser.add_argument("-p", "--path", type=str, required=False,
help="Path where the log files be stored. Note to create directory in that path beforehand.")
parser.add_argument('-d', '--default', action="store_const", const=30)
parser.add_argument('-V', '--verbose',action="store_const", const=20)
parser.add_argument('-s', '--silent', action="store_const", const=50)
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
help='Show this help message and exit.')
parser.add_argument('-v', '--version', action='version',
version='%(prog)s 1.0', help="Show program's version number and exit.")

## verbose is log level of Info with integer value 20 and silent is log level of critical of
## integer value of 50. The values are provided to futher assign the level of logging.
## Since logging level hold integer values.
args = vars(parser.parse_args())
## set manual loglevels
if args['default']:
level = args['default']
elif args['verbose']:
if args['verbose']:
level = args['verbose']
elif args['silent']:
level = args['silent']
else:
level = 10
names = args['blacklist']

## Default mode: log level is Warning and above.for further detail visit
## https://www.loggly.com/ultimate-guide/python-logging-basics/
level = logging.WARNING

res = []
data = pd.read_csv('data.csv')
default_dir = '/var/log/nawab/'
## blacklisting the names to current data.csv files
if names:
blist = pd.DataFrame({'Blacklist': names})
if args['blacklist']:
blist = pd.DataFrame({'Blacklist': args['blacklist']})
concats = pd.concat([data,blist])
concats.to_csv('data.csv', index=False)
## if new path is passed then set new default path
## if new path is passed then set new default path
if args['path']:
default_dir = args['path']

u_id = pwd.getpwuid(os.getuid()).pw_name

ownership_command = "sudo chown %s: %s" % (u_id, default_dir)
os.system(ownership_command)

nawab = Nawab(default_dir,data,level)
#Initiate twitter bot
thread = threading.Thread(target=wrapper, args=(
nawab.twitter_bot_run, (), res))
thread.start()
if args['retweet']:
auto_retweet = args['retweet']
thread = threading.Thread(target=wrapper, args=(
nawab.twitter_bot_run, (auto_retweet, ), res))
thread.start()
else:
thread = threading.Thread(target=wrapper, args=(
nawab.twitter_bot_run, (), res))
thread.start()
while thread.is_alive:
#Initiate telegram bot
<<<<<<< HEAD
nawab.tg_bot_run()
=======
tg_bot_run(data, default_dir, level, args['retweet'], args['tg_bot'])
>>>>>>> Nawab: Add arguments to incoporate auto-retweet and manual-retweet
if args['retweet']:
nawab.tg_bot_run(args['retweet'])
else:
nawab.tg_bot_run()


if __name__ == "__main__":
Expand Down
37 changes: 23 additions & 14 deletions tg_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

class Telegram_Bot(object):

def __init__(self, twitter_api, dirpath,level, isretweet, manual_tweet):
def __init__(self, twitter_api, dirpath, level, auto_retweet):
self.dirpath = dirpath
self.twitter_api = twitter_api
self.level = level
self.nw_logger = nawab_logger.Nawab_Logging(dirpath, level)
self.auto_retweet = auto_retweet


def nawab_tg_authenticate(self):
Expand All @@ -40,20 +41,24 @@ def display_tweet(self, context):
job = context.job
global KILL_SIGNAL
tid = pd.read_csv(self.dirpath + 'tid_store.csv')
for index, Id in tid.iterrows():
for index, tid_store in tid.iterrows():

try:
u = self.twitter_api.get_status(id=Id['Id'])
u = self.twitter_api.get_status(id=tid_store['Id'])
username = u.author.screen_name
except tweepy.TweepError as e:
if self.level == logging.CRITICAL:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Tweepy failed to get the status of the user from the " +
str(tid_store['Id']) + ' ' + ' because of ' + e.reason + "\n")
self.nw_logger.logger('\t|Tweepy failed to get the status of the user from the ' +
str(Id['Id']) + ' because of ' + e.reason + '\n\n', 'error', 'Error')
str(tid_store['Id']) + ' ' + ' because of ' + e.reason + '\n\n', 'error', 'Error')
pass
url = 'https://twitter.com/' + \
username + '/status/' + str(Id['Id'])
username + '/status/' + str(tid_store['Id'])

if job.context in config.tg_admin_id:
keyboard = [[InlineKeyboardButton("Retweet", callback_data=int(Id['Id'])),InlineKeyboardButton("View", url=url)]]
if (job.context in config.tg_admin_id) and (self.auto_retweet == False or self.auto_retweet == None):
keyboard = [[InlineKeyboardButton("Retweet", callback_data=int(tid_store['Id'])),InlineKeyboardButton("View", url=url)]]
reply_markup = InlineKeyboardMarkup(keyboard)
else:
keyboard = [[InlineKeyboardButton(
Expand Down Expand Up @@ -93,20 +98,24 @@ def button(self, update, context):
self.twitter_api.retweet(data)
except tweepy.TweepError as e:

if self.level == 50:
if self.level == logging.CRITICAL:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Tweepy failed to retweet after reading from the store of id " +
str(data) + " because of " + e.reason + "\n")
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Tweepy failed to retweet after reading from the store of id " +
str(data) + ' ' + ' because of ' + e.reason + "\n")

self.nw_logger.logger('\t|Tweepy failed to retweet after reading from the store of id ' +
str(data) + ' because of ' + e.reason + '\n\n', 'error', 'Error')
str(data) + ' ' + ' because of ' + e.reason + '\n\n', 'error', 'Error')
pass
try:
u = self.twitter_api.get_status(id=int(data))
username = u.author.screen_name
except tweepy.TweepError as e:
if self.level == logging.CRITICAL:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Tweepy failed to get the status of the user from the " +
str(data) + ' ' + ' because of ' + e.reason + '\n\n')
self.nw_logger.logger('\t|Tweepy failed to get the status of the user from the ' +
str(data) + ' because of ' + e.reason + '\n\n', 'error', 'Error')
str(data) + ' ' + ' because of ' + e.reason + '\n\n', 'error', 'Error')
pass
url = 'https://twitter.com/' + \
username + '/status/' + str(data)
Expand All @@ -118,9 +127,9 @@ def button(self, update, context):

def error(self, update, context):
"""Log Errors caused by Updates."""
if self.level == 50:
if self.level == logging.CRITICAL:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + '\t|Update' + update + 'caused error ' + context.error + '\n\n')
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + '\t|Update' + update + 'caused error ' + context.error + '\n\n')

self.nw_logger.logger(
'\t|Update' + update + 'caused error ' + context.error + '\n\n', 'error', 'Error')
Expand Down
33 changes: 23 additions & 10 deletions twitter_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def nawab_get_id(self):

def nawab_check_tweet(self, tweet_id):
tid = pd.read_csv(self.dirpath + 'tid_store.csv')
if any(Id["Id"] == tweet_id for index, Id in tid.iterrows()):
if any(tid_store["Id"] == tweet_id for index, tid_store in tid.iterrows()):
return True
else:
return False
Expand All @@ -89,8 +89,7 @@ def nawab_search(self, api, query):

if len(query) > 0:
for line in query:
if self.level == 50 or self.level == 30:

if self.level == logging.CRITICAL or self.level == logging.WARNING:
with open(self.dirpath + "results.log", "a") as fp:
fp.write('INFO:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") +
"\t|starting new query search: \t" + line + "\n")
Expand All @@ -106,6 +105,10 @@ def nawab_search(self, api, query):
text = tweets.full_text

if (self.nawab_check_tweet(id)) and ('RT @' in text):
if self.level == logging.WARNING:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + '\t|'+
str(id) + ' ' + " already exists in the database or it is a retweet\n")
self.nw_logger.logger(
'\t|' + str(id) + 'already exists in the database or it is a retweet', 'error', 'Error')
else:
Expand All @@ -115,14 +118,14 @@ def nawab_search(self, api, query):
url = 'https://twitter.com/' + \
user + '/status/' + str(id)

if self.level == 50 or self.level == 30:
if self.level == logging.CRITICAL or self.level == logging.WARNING:
with open(self.dirpath + "results.log", "a") as fp:
fp.write('INFO:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + '\t|' + url + '\n')

self.nw_logger.logger(
'\t|' + url + '\n', 'info', 'Results')

if self.level == 50 or self.level == 30:
if self.level == logging.CRITICAL or self.level == logging.WARNING:
with open(self.dirpath + "results.log", "a") as fp:
fp.write('INFO:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Id: " + str(id) +
" is stored to the db from this iteration \n")
Expand All @@ -131,30 +134,40 @@ def nawab_search(self, api, query):
'\t|Id: ' + str(id) + 'is stored to the db from this iteration', 'info', 'Results')

except tweepy.TweepError as e:
if self.level == 50:
if self.level == logging.CRITICAL:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Tweepy failed at " + str(id) +
" because of " + e.reason + "\n")
' ' + " because of " + e.reason + "\n")
self.nw_logger.logger(
'\t|Tweepy failed at ' + str(id) + 'because of' + e.reason, 'error', 'Error')
pass

def nawab_retweet_tweet(self, api):
tid = pd.read_csv(self.dirpath + 'tid_store.csv')
for index, Id in tid.iterrows():
tweet_id = int(Id['Id'])
for index, tid_store in tid.iterrows():
tweet_id = int(tid_store['Id'])
try:
u = api.get_status(id=tweet_id)
rt_username = u.author.screen_name
api.retweet(tweet_id)
time.sleep(60)
retweet_url = 'https://twitter.com/' + \
rt_username + '/status/' + str(tweet_id)
if self.level == logging.CRITICAL or self.level == logging.WARNING:
with open(self.dirpath + "results.log", "a") as fp:
fp.write('INFO:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Nawab retweeted " +
str(tweet_id) + " successfully \n")


self.nw_logger.logger('\t|Nawab retweeted' +
str(tweet_id) + 'successfully', 'info', 'Results')

except tweepy.TweepError as e:
if self.level == logging.CRITICAL:
with open(self.dirpath + "error.log", "a") as fp:
fp.write('ERROR:' + time.strftime("%m/%d/%Y %I:%M:%S %p ") + "\t|Tweepy failed to retweet after reading from the store of id " +
str(tweet_id) + ' ' +" because of " + e.reason + "\n")

self.nw_logger.logger('\t|Tweepy failed to retweet after reading from the store of id ' +
str(tweet_id) + 'because of' + e.reason, 'error', 'Error')
str(tweet_id) + ' ' + 'because of' + e.reason, 'error', 'Error')
pass

0 comments on commit 55e4b83

Please sign in to comment.