diff --git a/nawab.py b/nawab.py index bbb5717..e0402a9 100644 --- a/nawab.py +++ b/nawab.py @@ -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 @@ -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 @@ -64,43 +60,41 @@ 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) @@ -108,16 +102,21 @@ def main(): 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__": diff --git a/tg_bot.py b/tg_bot.py index c2c2a91..39bbba3 100644 --- a/tg_bot.py +++ b/tg_bot.py @@ -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): @@ -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( @@ -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) @@ -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') diff --git a/twitter_bot.py b/twitter_bot.py index a616fb9..fb7cc11 100644 --- a/twitter_bot.py +++ b/twitter_bot.py @@ -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 @@ -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") @@ -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: @@ -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") @@ -131,18 +134,18 @@ 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 @@ -150,11 +153,21 @@ def nawab_retweet_tweet(self, api): 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