Skip to content

Commit 498431a

Browse files
Telegram_Bot: replace current method with nawab_log
1 parent 7257e3e commit 498431a

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

tg_bot.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ def help(self, update, context):
4242
def display_tweet(self, context):
4343
job = context.job
4444
global KILL_SIGNAL
45-
if self.level == logging.CRITICAL or self.level == logging.WARNING:
46-
with open(self.dirpath + "results.log", "a") as fp:
47-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' INFO ' + 'Telegram_Bot '
48-
+ 'starting display parameter' + '\n')
49-
self.nw_logger.logger('Telegram_Bot starting display parameter', 'info', 'Results')
45+
self.tw_bot.nawab_log('starting display parameter', 'info', 'Telegram_Bot ')
5046

5147
# the previous date from which the tweets need to be sent
5248
previous_date = self.tw_bot.nawab_find_prev_date()
@@ -61,13 +57,8 @@ def display_tweet(self, context):
6157
u = self.twitter_api.get_status(id=tid_store['Id'])
6258
username = u.author.screen_name
6359
except tweepy.TweepError as e:
64-
if self.level == logging.CRITICAL:
65-
with open(self.dirpath + "error.log", "a") as fp:
66-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' ERROR ' + 'Telegram_Bot ' +
67-
"Tweepy failed to get the status of the user from the " +
68-
str(tid_store['Id']) + ' because of ' + e.reason + "\n")
69-
self.nw_logger.logger('Telegram_Bot Tweepy failed to get the status of the user from the ' +
70-
str(tid_store['Id']) + ' because of ' + e.reason, 'error', 'Error')
60+
self.tw_bot.nawab_log('Tweepy failed to get the status of the user from the ' +
61+
str(tid_store['Id']) + ' because of ' + e.reason, 'error', 'Telegram_Bot ')
7162
pass
7263
url = 'https://twitter.com/' + \
7364
username + '/status/' + str(tid_store['Id'])
@@ -100,11 +91,7 @@ def start(self, update, context):
10091
self.display_tweet, 2, context=chat_id)
10192
context.chat_data['job'] = new_job
10293
update.message.reply_text('Successfully started!')
103-
if self.level == logging.CRITICAL or self.level == logging.WARNING:
104-
with open(self.dirpath + "results.log", "a") as fp:
105-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' INFO ' + 'Telegram_Bot '
106-
+ 'new job ' + str(new_job) + '\n')
107-
self.nw_logger.logger('Telegram_Bot new job ' + str(new_job) , 'info', 'Results')
94+
self.tw_bot.nawab_log('new job ' + str(new_job), 'info', 'Telegram_Bot ')
10895
except (IndexError, ValueError):
10996
update.message.reply_text('Did you /start yet?')
11097

@@ -118,25 +105,15 @@ def button(self, update, context):
118105
try:
119106
self.twitter_api.retweet(data)
120107
except tweepy.TweepError as e:
121-
122-
if self.level == logging.CRITICAL:
123-
with open(self.dirpath + "error.log", "a") as fp:
124-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' ERROR ' + 'Telegram_Bot '
125-
+ "Tweepy failed to retweet after reading from the store of id " +
126-
str(data) + ' because of ' + e.reason + "\n")
127-
128-
self.nw_logger.logger('Telegram_Bot' + ' Tweepy failed to retweet after reading from the store of id ' +
129-
str(data) + ' because of ' + e.reason, 'error', 'Error')
108+
self.tw_bot.nawab_log('Tweepy failed to retweet after reading from the store of id ' +
109+
str(data) + ' because of ' + e.reason, 'error', 'Telegram_Bot ')
130110
pass
131111
try:
132112
u = self.twitter_api.get_status(id=int(data))
133113
username = u.author.screen_name
134114
except tweepy.TweepError as e:
135-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' ERROR ' + 'Telegram_Bot ' +
136-
"Tweepy failed to get the status of the user from the " +
137-
str(data) + ' because of ' + e.reason + '\n')
138-
self.nw_logger.logger('Telegram_Bot' + ' Tweepy failed to get the status of the user from the ' +
139-
str(data) + ' because of ' + e.reason, 'error', 'Error')
115+
self.tw_bot.nawab_log('Tweepy failed to get the status of the user from the ' +
116+
str(data) + ' because of ' + e.reason, 'error', 'Telegram_Bot ')
140117
pass
141118
url = 'https://twitter.com/' + \
142119
username + '/status/' + str(data)
@@ -150,12 +127,7 @@ def error(self, update, context):
150127
"""
151128
Log Errors caused by Updates.
152129
"""
153-
if self.level == logging.CRITICAL:
154-
with open(self.dirpath + "error.log", "a") as fp:
155-
fp.write( time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' ERROR ' + 'Telegram_Bot ' +
156-
'Update ' + update + ' caused error ' + context.error + '\n')
157-
self.nw_logger.logger('Telegram_Bot' +
158-
' Update' + update + 'caused error ' + context.error, 'error', 'Error')
130+
self.tw_bot.nawab_log('Update' + update + 'caused error ' + context.error, 'error', 'Telegram_Bot ')
159131

160132
def stop(self, update, context):
161133
if 'job' not in context.chat_data:

twitter_bot.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ def nawab_twitter_authenticate(self):
4343
api = tweepy.API(auth)
4444
return api
4545

46-
def nawab_log(self, msg, msg_type):
46+
def nawab_log(self, msg, msg_type, name):
4747
""" To log all message to file"""
4848
if msg_type == 'error':
4949
if self.level == logging.CRITICAL:
5050
with open(self.dirpath + "error.log", "a") as fp:
51-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' ERROR ' + 'Twitter_Bot ' + msg + "\n")
52-
self.nw_logger.logger('Twitter_Bot' + msg, 'error', 'Error')
51+
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' ERROR ' + name + msg + "\n")
52+
self.nw_logger.logger(name + msg, 'error', 'Error')
5353
else:
5454
if self.level == logging.CRITICAL or self.level == logging.WARNING:
5555
with open(self.dirpath + "results.log", "a") as fp:
56-
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' INFO ' + 'Twitter_Bot ' + msg + "\n")
57-
self.nw_logger.logger('Twitter_Bot' + msg, 'info', 'Results')
56+
fp.write(time.strftime("%Y-%m-%d %I:%M:%S %p") + ',' + ' INFO ' + name + msg + "\n")
57+
self.nw_logger.logger(name + msg, 'info', 'Results')
5858

5959
def nawab_read_list(self):
6060
"""
@@ -166,7 +166,7 @@ def nawab_search(self, api, query):
166166

167167
if len(query) > 0:
168168
for line in query:
169-
self.nawab_log("starting new query search: " + line, 'info')
169+
self.nawab_log("starting new query search: " + line, 'info', 'Twitter_Bot ')
170170
try:
171171
for tweets in tweepy.Cursor(api.search, q=line, tweet_mode="extended",
172172
lang='en', since=latest_date).items(tweet_limit):
@@ -180,23 +180,23 @@ def nawab_search(self, api, query):
180180
min_freq = 2
181181

182182
if (self.__nawab_check_tweet(id)) and ('RT @' in text):
183-
self.nawab_log(str(id) + ' already exists in the database or it is a retweet', 'error')
183+
self.nawab_log(str(id) + ' already exists in the database or it is a retweet', 'error', 'Twitter_Bot ')
184184
else:
185185
if (self.__isUserwhitelisted(user) or (self.__isUserBanned(user, admin_user) and self.__isSafeKeyword(text))):
186186
if not (self.__nawab_check_tweet(id)):
187187
##check if it is a relevant tweet
188188
if self.__nawab_check_relevant(query, text) >= min_freq:
189-
self.nawab_log('Id: ' + str(id) + 'is a relevant tweet and is stored to the db from this iteration', 'info')
189+
self.nawab_log('Id: ' + str(id) + 'is a relevant tweet and is stored to the db from this iteration', 'info', 'Twitter_Bot ')
190190
self.nawab_store_id(id, True)
191191
else:
192-
self.nawab_log('Id: ' + str(id) + 'is not a relevant tweet and will not be from this iteration', 'info')
192+
self.nawab_log('Id: ' + str(id) + 'is not a relevant tweet and will not be from this iteration', 'info', 'Twitter_Bot ')
193193
self.nawab_store_id(id, False)
194194
url = 'https://twitter.com/' + \
195195
user + '/status/' + str(id)
196196

197-
self.nawab_log(url,'info')
197+
self.nawab_log(url,'info', 'Twitter_Bot ')
198198
except tweepy.TweepError as e:
199-
self.nawab_log(' Tweepy failed at ' + str(id) + ' because of ' + e.reason, 'error')
199+
self.nawab_log(' Tweepy failed at ' + str(id) + ' because of ' + e.reason, 'error', 'Twitter_Bot ')
200200
pass
201201

202202
def nawab_retweet_tweet(self, api):
@@ -212,9 +212,9 @@ def nawab_retweet_tweet(self, api):
212212
api.retweet(tweet_id)
213213
retweet_url = 'https://twitter.com/' + \
214214
rt_username + '/status/' + str(tweet_id)
215-
self.nawab_log(' Nawab retweeted ' + str(tweet_id) + ' successfully', 'info')
215+
self.nawab_log(' Nawab retweeted ' + str(tweet_id) + ' successfully', 'info', 'Twitter_Bot ')
216216

217217
except tweepy.TweepError as e:
218218
self.nawab_log(' Tweepy failed to retweet after reading from the store of id ' + str(tweet_id) +
219-
' because of ' + e.reason, 'error')
219+
' because of ' + e.reason, 'error', 'Twitter_Bot ')
220220
pass

0 commit comments

Comments
 (0)