forked from vishalnandagopal/ritter-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
714 lines (595 loc) · 29.4 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import tweepy
import feedparser
import time
import os
import json
# Functions defined from the next line up to the TwitterUser class are generic and not specific to Twitter. They're made for the project but can be used anywhere else.
def read_and_check_config(config_error_count:int=0) -> tuple[bool,int,bool,bool,bool,bool,bool,bool,bool,bool,bool,int,int,int,bool,dict,list,bool]:
print("Reading config file")
with open("config.json", "r") as f:
config = json.load(f)
try:
config_values = (
config["run_program"], # -------------------------------- 0
config["cron_job_mode"], # -------------------------------1
config["testing_mode"], # --------------------------------2
config["tweet_only_link"], # -----------------------------3
config["tweet_description"], # ---------------------------4
config["tweet_summarized_article"], # --------------------5
config["move_log_files"], # ------------------------------6
config["delete_old_log_files"], # ------------------------7
config["delete_important_errors"], # ---------------------8
config["time_to_wait_after_a_tweet_in_seconds"], # -------9
config["run_program_every_x_seconds"], # ----------------10
config["time_to_wait_when_not_running_in_seconds"], # ---11
config["ignore_https"], # -------------------------------12
config["rss_feeds_to_fetch"], # -------------------------13
config["dont_summarize_these_urls"], # ------------------14
config["reset_config_to_default"] # ---------------------15
)
except (KeyError,FileNotFoundError,json.JSONDecodeError):
(valid_config,config_error_count) = config_error(config_error_count)
read_and_check_config(config_error_count)
# Checks if the important properties in the config.json file are valid by seeing if their types are appropriate
if not ( (isinstance(config_values[0],bool)) and (isinstance(config_values[1],bool)) and (isinstance(config_values[2],bool)) and (isinstance(config_values[3],bool)) and (isinstance(config_values[4],bool)) and (isinstance(config_values[5],bool)) ):
valid_config = False
elif not ( (isinstance(config_values[10],int)) and (isinstance(config_values[11],int)) ):
valid_config = False
elif not ( (isinstance(config_values[13],dict))):
valid_config = False
else:
valid_config = True
tweet_count = 0
return ( (valid_config,tweet_count) + config_values)
def write_to_file(stuff_to_write: str, file_name: str, directory:str = '', mode :str = 'a', set_pointer_position_from_end_and_truncate:int=0):
# Writes stuff_to_write string to file_name, defaults to append mode. Directory can also be a sub-directories (even if parent directory does not exist). Eg: old_log_files\log_files
try:
if directory:
file_path = directory + "/" + file_name
else:
file_path = file_name
with open(file_path, mode) as f:
if set_pointer_position_from_end_and_truncate > 0:
#This sets the pointer to 1 character from the end. Here, 2 is the whence meaning end of file.
f.seek(-set_pointer_position_from_end_and_truncate,2)
f.truncate()
f.write(stuff_to_write)
except FileNotFoundError:
make_dir(directory)
write_to_file(stuff_to_write,file_name,directory,mode,set_pointer_position_from_end_and_truncate)
def read_file_and_ignore_comments(file_name: str, directory:str = '') -> list:
# reads the file in the given file path and returns a list of all lines that do not start with "#" in the file
l1 = []
try:
if directory:
file_path = directory + "/" + file_name
else:
file_path = file_name
with open(file_path,'r') as f:
l = f.readlines()
for line in l:
if not line.startswith("#"):
line1 = line.rstrip("\n")
l1.append(line1)
except FileNotFoundError:
write_to_file("",file_name,directory,'x')
return l1
def make_value_false_in_config(key_to_make_false:str):
# sets the value of the given key as false again. Used to prevent the program from getting stuck in a loop where it keeps deleting old_log_files or keeps reloading api_keys
with open('config.json', "r") as f:
config = json.load(f)
config[key_to_make_false] = False
to_dump = json.dumps(config,indent=4)
write_to_file(to_dump,'config.json','',mode='w')
print("Changed config.json")
def make_dir(dir_name:str):
# Makes a directory with the given name in the root directory.
try:
# os.makedirs can create parent directories too, if they don't exist when creating a sub directory.
os.makedirs(dir_name)
except FileExistsError:
print(f"Tried making {dir_name} directory, but it already exists")
def move_file_or_dir(current_name:str, dir_to_move_to:str, add_ctime_to_name_after_moving:bool = False):
try:
if add_ctime_to_name_after_moving:
new_name = dir_to_move_to + "/" + current_name + (f" at {time.ctime()}").replace(":","-",3)
os.rename(current_name, new_name)
else:
new_name = dir_to_move_to + "/" + current_name
os.rename(current_name, new_name)
except FileNotFoundError:
make_dir(current_name)
make_dir(dir_to_move_to)
move_file_or_dir(current_name, dir_to_move_to,add_ctime_to_name_after_moving)
def delete_file_or_folder(path:str):
try:
os.remove(path)
print(f"Deleted {path}")
except FileNotFoundError:
print(f"Tried to delete {path} but it does not exist")
except PermissionError:
try:
import shutil
shutil.rmtree(path)
print(f"Deleted contents of {path}")
except NotADirectoryError:
delete_file_or_folder(path)
except FileNotFoundError:
print(f"Tried to delete {path} but it does not exist")
def check_if_exists_in_directory(file_name:str,directory:str='') -> bool:
current_working_dir = os.getcwd()
try:
if directory:
os.chdir(directory)
return file_name in os.listdir()
except FileNotFoundError:
return False
finally:
os.chdir(current_working_dir)
def check_if_file_is_empty(file_name:str,directory:str='') -> bool:
if directory:
file_path = directory + "/" + file_name
else:
file_path = file_name
try:
return os.path.getsize(file_path) == 0
except FileNotFoundError:
return True
def move_or_clear_files(delete_old_log_files:bool = False, move_log_files:bool = False, move_config:bool = False, delete_important_errors:bool = False) -> str:
if move_config:
if check_if_exists_in_directory('config.json'):
move_file_or_dir('config.json','old_log_files')
reset_config()
else:
if delete_old_log_files:
delete_file_or_folder('old_log_files')
make_value_false_in_config('delete_old_log_files')
if delete_important_errors:
delete_file_or_folder('important-errors.txt')
make_value_false_in_config('delete_important_errors')
if move_log_files:
if check_if_exists_in_directory('log_files'):
move_file_or_dir('log_files','old_log_files',True)
make_value_false_in_config('move_log_files')
class GlobalVariables:
def __init__(self) -> None:
# Settings for the bot (flags). Assigns the values from config.json
self.read_config_and_set_variables()
def read_config_and_set_variables(self):
(
self.valid_config,
self.tweet_count,
self.run_program,
self.cron_job_mode,
self.testing_mode,
self.tweet_only_link,
self.tweet_description,
self.tweet_summarized_article,
self.move_log_files,
self.delete_old_log_files,
self.delete_important_errors,
self.time_to_wait_after_a_tweet_in_seconds,
self.run_program_every_x_seconds,
self.time_to_wait_when_not_running_in_seconds,
self.ignore_https,
self.rss_feeds_to_fetch,
self.dont_summarize_these_urls,
self.reset_config_to_default,
) = read_and_check_config()
variables = GlobalVariables()
def reset_config():
default_config = read_file_and_ignore_comments('default-config.json')
delete_file_or_folder('config.json')
for line in default_config:
write_to_file(line + "\n",'config.json')
print("config.json has been reset")
def config_error(config_error_count:int) -> tuple[bool,int]:
# Runs when the config.json file has some error or is not properly formatted. Sets run_program and config_valid to false.
error = f"config.json does not seem to be a valid json file. Time of error is {time.ctime()}"
print(error)
write_to_file( (error + "\n"),'important-errors.txt')
if config_error_count == 9:
error = f"Read invalid config too many times. Will reset config if it is invalid after reading it once again. Time of error is {time.ctime()}"
print(error)
write_to_file( (error + "\n"),'important-errors.txt')
if config_error_count == 10:
reset_config()
error = f"Reset config.json because of invalid config too many times. Time of error is {time.ctime()}"
print(error)
write_to_file( (error + "\n"),'important-errors.txt')
return [False,0]
print(f"Waiting for 10 minutes before reading config.json again. Have read invalid config {config_error_count} time(s) now")
config_error_count += 1
try:
time.sleep(variables.time_to_wait_when_not_running_in_seconds)
except NameError:
time.sleep(21600)
config_error(config_error_count)
def new_link_validator(url: str) -> bool:
# Checks if the url has already been tweeted out by reading all lines in 'log_files\tweeted-urls.txt'
already_tweeted_urls = read_file_and_ignore_comments('tweeted-urls.txt','log_files')
return not url in already_tweeted_urls
def dict_for_tweets(ids :list, type_of_tweet: str, tweet_text) -> dict:
# creates a dictionary of details containing text, readable time and epoch time
tweeted_out_time = time.ctime() # Not the exact tweeted out time since this is the time when the dict_for_tweets is called, and not when the text is tweeted out. But good enough
sub_dict_for_tweet = {}
sub_dict_for_tweet['id'] = str(ids)
sub_dict_for_tweet['text'] = str(tweet_text)
sub_dict_for_tweet['time'] = tweeted_out_time
sub_dict_for_tweet['type'] = type_of_tweet
return sub_dict_for_tweet
def get_tag_data(html_text: str, tag: str) -> str:
# This gets the data of the provided tag from the html text. Separate function to ensure can easily add more tags other than meta tag
tag_string = ""
length = len(html_text)
for i in range(length):
if (html_text[i : (i + (len(tag)))]) == tag:
for j in range(i, length):
if html_text[j] == ">":
tag_string = html_text[i : j + 1]
break
if tag_string == "":
print(f"{tag} not found, try another tag")
return tag_string
def get_url_description_from_html(url:str) -> str:
# Fetches description of a url from by parsing it's html. To be used in case the description is not included in the RSS feeds.
# This works only for meta tags where the data is stored in content="". Can change it easily
# Could have done this using beautifulsoup4 but wanted to do it on my own. bs4 code included below
"""
from bs4 import BeautifulSoup
import requests
html_text = requests.get("https://www.scrapingbee.com/blog_to_json/python-web-scraping-beautiful-soup/").content
soup = BeautifulSoup(html_text, 'html.parser')
description = soup.find('meta', attrs={"name": "description"})
print(description['content'])
# The description of the page
description = soup.find("meta", property="description")
# The text of the page
print(description.get_text())
"""
import requests
try:
response = requests.get(url)
html_text = response.text
if response.status_code != 200:
print("Error fetching page")
else:
tag_string = get_tag_data(html_text, "<meta name=\"description\"")
if tag_string == "":
tag_string = get_tag_data(html_text, "<meta property=\"twitter:description\"")
if tag_string == "":
tag_string = get_tag_data(html_text, "<meta property=\"og:description\"")
print(f"{url} does not have a description meta tag")
tag_content = ""
for i in range(len(tag_string)):
if (tag_string[i : (i + 7)]) == "content":
for j in range(i + 9, len(tag_string)):
if tag_string[j] == '"' or tag_string[j] == "'":
tag_content = tag_string[i + 9 : j]
break
return tag_content
except Exception as e:
if ("NewConnectionError" in str(e)) or ("ConnectionError" in str(e)):
print("Prolly not connected to the internet")
return tag_content
else:
print("Connected to the internet but some other exception exception")
def summarize_article(url: str) -> str:
# Uses the newspaper module to generate a summary of the article. Fetches the article, parses it and then summarises it with the help of nltk. Returns a tuple of sentences in the summarized verion
# not perfect but pretty good
import newspaper
article_obj = newspaper.Article(url)
try:
article_obj.download()
except newspaper.article.ArticleException:
# Returns description in this case. Something better than nothing
description_text = get_description_of_entry_or_url(url)
return description_text
article_obj.parse()
try:
article_obj.nlp()
except LookupError:
#punkt resource not downloaded
import nltk
nltk.download("punkt",quiet=True)
article_obj.nlp()
summarized_text = article_obj.summary
return summarized_text
def split_into_sentences(summarized_text: str) -> list:
import nltk
nltk.download("punkt",quiet=True)
single_sentences = nltk.tokenize.sent_tokenize(summarized_text)
already_added,sentences_to_tweet = [],[]
for i in range(len(single_sentences)):
if (i not in already_added):
(already_added,sentences_to_tweet) = check_merge_sentences(single_sentences, i, already_added, sentences_to_tweet)
return sentences_to_tweet
def check_merge_sentences(single_sentences: list, index: int, already_added: list, sentences_to_tweet:list, count: int = 2) -> tuple[list,list]:
length = 0
for i in range(count):
try:
length += len(single_sentences[index+i]) + 1
except IndexError:
length = 276
if length < 275:
check_merge_sentences(single_sentences, index, already_added,sentences_to_tweet,count+1)
else:
sentence = single_sentences[index]
if (len(sentence) > 280) and (count == 2):
sentences_to_tweet.append(sentence[:277] + "...")
single_sentences[index] = sentence[277:]
check_merge_sentences(single_sentences, index, already_added,sentences_to_tweet,count)
else:
sentences_to_tweet.append(sentence)
already_added.append(index)
for i in range(count-2):
sentence += " " + single_sentences[index+i+1]
already_added.append(index+i+1)
sentences_to_tweet.append(sentence)
return (already_added, sentences_to_tweet)
def tldize(url:str) -> str:
# Returns the domain of the URL. Example: https://github.com/vishalnandagopal -> github.com, https://google.co.in/search?q=vishal -> google.co.in
from urllib.parse import urlparse
domain = urlparse(url).netloc
return domain
def what_to_tweet(entry:dict, description_from_html:bool) -> tuple[str,list,str,bool]:
# Default mode is to tweet the title + URL of the article in the same tweet. To change the mode, edit config.json
url = entry['link']
read_more = False
if variables.tweet_only_link:
sentences_to_tweet = [url]
type_of_tweet = 'link'
elif variables.tweet_description:
description_text = get_description_of_entry_or_url(entry, url, description_from_html)
sentences_to_tweet = split_into_sentences(description_text)
sentences_to_tweet.insert(0,entry['title'])
type_of_tweet = 'description'
read_more = True
elif variables.tweet_summarized_article:
if tldize(url) not in variables.dont_summarize_these_urls:
sentences_to_tweet = split_into_sentences(summarize_article(url))
else:
description_text = get_url_description_from_html(url)
sentences_to_tweet = split_into_sentences(description_text)
sentences_to_tweet.insert(0,entry['title'])
type_of_tweet = 'summary'
read_more = True
else:
if len(entry['title']) > 254:
sentences_to_tweet = [shorten_tweet_text(entry['title'],character_count=254) + url]
else:
sentences_to_tweet = [entry['title'] + ' ' + url]
type_of_tweet = 'title'
return (url, sentences_to_tweet, type_of_tweet, read_more)
def get_description_of_entry_or_url(entry, url:str, description_from_html:bool) -> str:
if description_from_html:
description_text = get_url_description_from_html(url)
else:
try:
description_text = entry['description']
except KeyError:
# Description of the article isn't present in the the RSS feed
description_text = get_url_description_from_html(url)
return description_text
def shorten_tweet_text(text: str,character_count: int = 276) -> str:
# Shortens the text to 276 characters and appends "..." at the end. (Twitter's character limit is 280)
return (text[0:character_count] + "...")
def log_to_file(ids: list, url: str, type_of_tweet: str, tweet_text):
# takes care of logging the details of the tweet to text files for future use
#removes the 1st 0 from the list of tweet_ids
ids.pop(0)
# log_to_jsons the IDs of the tweets to tweet-ids.txt
for tweet_id in ids:
write_to_file( (str(tweet_id) + "\n"), 'tweet-ids.txt','log_files')
# log_to_jsons all the URLs that have been tweeted out
write_to_file( (url + "\n"), 'tweeted-urls.txt','log_files')
# log_to_jsons a json of tweet details to tweet-details.json
# used to log_to_json a str(dictionary) but decided json is better
dict_to_write = { url: dict_for_tweets(
ids,
type_of_tweet,
tweet_text
)
}
if check_if_file_is_empty('tweet-details.json','log_files'):
to_dump = json.dumps(dict_to_write, sort_keys=True,indent=4)
write_to_file((to_dump), 'tweet-details.json','log_files')
else:
to_dump = "," + json.dumps(dict_to_write, sort_keys=True,indent=4).lstrip("{")
write_to_file((to_dump).encode('ascii') , 'tweet-details.json','log_files',mode='ab',set_pointer_position_from_end_and_truncate=2)
def feed_to_json(url: str):
# converts feedparser output to a json file for better readability. To better understand what fields to parse and tweet out when testing
from json import dumps
feed = feedparser.parse(url)
to_dump = dumps(feed, indent=1, sort_keys=True)
write_to_file(to_dump, 'feed.json', 'log_files', 'w')
def get_api_keys() -> tuple[str,str,str,str,str]:
from dotenv import load_dotenv
try:
# Get the necessary tokens from the ritter-bot.env file. To hardcode the API keys, substitute the os.getenv('') with a string of the API keys.
if check_if_exists_in_directory("ritter-bot.env"):
load_dotenv(r'ritter-bot.env')
else:
# Loads the env keys when using GitHub Actions, or hosting sites such as Heroku.
load_dotenv()
bearer_token = os.getenv('Bearer_Token')
api_key = os.getenv('API_Key')
api_key_secret = os.getenv('API_Key_Secret')
access_token = os.getenv('Access_Token')
access_token_secret = os.getenv('Access_Token_Secret')
return (bearer_token, api_key, api_key_secret,access_token,access_token_secret)
except ValueError:
print('Error with API Keys')
def read_rss_feed(feed_url: str) -> feedparser.util.FeedParserDict:
# read rss feed and returns a dictionary of the entries in the feed details.
feed = feedparser.parse(feed_url)
try:
if feed['status'] == 200:
fetch_error = False
else:
fetch_error = True
except KeyError:
fetch_error = True
try:
if variables.ignore_https:
if "certificate" in str(feed['bozo_exception']).lower():
import requests
print(f"{feed_url} has an SSL certificate issue. Trying to fetch without HTTPS")
response = requests.get(feed_url, verify=False)
html_text = response.text
feed = feedparser.parse(html_text)
if feed['bozo'] == False:
fetch_error = False
except KeyError:
pass
if not fetch_error:
return feed
else:
print(f"{feed_url} cannot be accessed.")
class TwitterUser:
def __init__(self) -> None:
bearer_token, api_key, api_key_secret, access_token, access_token_secret = get_api_keys()
self.authenticated_user = tweepy.Client(bearer_token, api_key, api_key_secret, access_token, access_token_secret,wait_on_rate_limit=True)
# All the below functions are tied to a user. They're supposed to be accessed by an authenticated user ( user.function() ), and are twitter specific.
def tweet_out(self, tweet_text: str = '', in_reply_to_id:int = 0) -> tuple:
if 0 < len(tweet_text) <= 280:
if not variables.testing_mode:
try:
if not in_reply_to_id:
tweeting = self.authenticated_user.create_tweet(text=tweet_text)
else:
tweeting = self.authenticated_user.create_tweet(text=tweet_text, in_reply_to_tweet_id=in_reply_to_id)
variables.tweet_count += 1
except Exception as e:
if "duplicate content" in str(e):
tweeting = (
{
'id': 0,
'text': tweet_text
},
["duplicate_content"]
)
else:
tweeting = (
{
'id': 0,
'text': tweet_text
},
[str(e)]
)
write_to_file(str(e) + "\n",'important-errors.txt')
raise e
else:
'''
tweeting is a list of lists returned by Client.create_tweet().
Replicating same format here when testing_mode==False
example format: Response(data={"id": "1517736731571007488", "text": "Researcher Releases PoC for Recent Java Cryptographic Vulnerability https://t.co/uOOs0awlIU"}, includes={}, errors=[], meta={})
'''
tweeting = (
{
'id': 0,
'text': tweet_text,
},
["testing_mode=True"]
)
return tweeting
else:
return self.tweet_out(shorten_tweet_text(tweet_text),in_reply_to_id)
def tweet_feed(self, feed, description_from_html:bool = False):
if variables.testing_mode:
if len(feed['entries']) > 2:
urls_to_tweet = 2
else:
urls_to_tweet = 1
else:
# runs loop for first range() articles in the feed
urls_to_tweet = len(feed['entries'])
for i in range(urls_to_tweet):
# # When feedparser parses a RSS feed, the output is in the format dict > list > dict > ["title"]
url = feed['entries'][i]['link']
if new_link_validator(url):
entry = feed['entries'][i]
(url, sentences_to_tweet, type_of_tweet,read_more) = what_to_tweet(entry, description_from_html)
tweet_ids=[0]
for sentence in sentences_to_tweet:
tweeting = self.tweet_out(sentence,in_reply_to_id=tweet_ids[-1])
tweet_ids.append(tweeting[0]['id'])
if read_more:
tweeting = self.tweet_out("Read more at " + url,in_reply_to_id=tweet_ids[-1])
tweet_ids.append(tweeting[0]['id'])
print(f"{tweeting[0]['id']} is the tweet ID for {url}")
log_to_file(tweet_ids, url, type_of_tweet, sentences_to_tweet)
time.sleep(variables.time_to_wait_after_a_tweet_in_seconds)
else:
#print(f"Already been tweeted about - {url}")
# Can also break, if the feed is sorted chronologically. If one URL has already been tweeted out, it means the all the next URLs in the feed have also been tweeted out
pass
def search_query(self, query="") -> dict:
# searches the given query on twitter
search_results = self.authenticated_user.search_recent_tweets(query, sort_order='relevancy', max_results=20)
return search_results
def delete_tweet(self, to_delete=None, all_ids_in_log_files:bool = False):
# can provide a list or tuple of tweet_ids, a single tweet_id:int or ask it to delete all tweet_ids in 'log_files\tweet-ids.txt' (can be used for deleting all tweets easily)
if isinstance(to_delete,int):
# deletes the tweet corresponding to the tweet_id given to the function as to_delete
time.sleep(1)
return ( self.authenticated_user.delete_tweet(to_delete) )
elif isinstance(to_delete,list) or isinstance(to_delete,tuple):
# deletes every tweet in the list to_delete supplied to the function
for tweet_id in to_delete:
self.authenticated_user.delete_tweet(tweet_id)
elif ( (all_ids_in_log_files) and (to_delete==None) ):
list_of_tweet_ids = read_file_and_ignore_comments('tweet-ids.txt','log_files')
self.delete_tweet(to_delete=list_of_tweet_ids)
else:
print(f"Wrong type of argument passed to delete_tweet(). to_delete must be an int or a list, but it currently is of type {type(to_delete)}")
'''
import schedule
'''
def job():
start_time = (time.ctime(),time.time())
print("Running job()")
if variables.reset_config_to_default:
move_or_clear_files(move_config=True)
elif variables.run_program:
user = TwitterUser()
for feed_url in variables.rss_feeds_to_fetch['description_from_rss_feed']:
feed = read_rss_feed(feed_url)
user.tweet_feed(feed)
for feed_url in variables.rss_feeds_to_fetch['description_from_html']:
feed = read_rss_feed(feed_url)
user.tweet_feed(feed,description_from_html=True)
else:
print(f"Program not running. Waiting for {variables.time_to_wait_when_not_running_in_seconds} seconds")
time.sleep(variables.time_to_wait_when_not_running_in_seconds)
if variables.move_log_files or variables.delete_old_log_files or variables.delete_important_errors:
move_or_clear_files(variables.delete_old_log_files,variables.move_log_files,variables.delete_important_errors)
# user.delete_tweet(all_ids_in_log_files=True)
'''
Other useful functions
user.delete_tweet(all_ids_in_log_files=True)
feed_to_json("https://www.secureworks.com/rss?feed=research&category=threat-analysis")
'''
end_time = (time.ctime(),time.time())
print(f"Tweeted out {variables.tweet_count} times. Job started at {start_time[0]} and finished in {end_time[1] - start_time[1]} seconds")
if variables.valid_config:
job()
if not variables.cron_job_mode:
while True:
if variables.valid_config:
job()
time.sleep(variables.run_program_every_x_seconds)
else:
variables.time_to_wait_when_not_running_in_seconds()
variables.read_config_and_set_variables()
'''
# Can also use schedule module instead of a cron job
import schedule
schedule.every(25).minutes.do.job()
time.sleep(2)
schedule.every(30).days.do(move_or_clear_files, move_log_files=True, delete_old_log_files=False)
schedule.every(90).days.do(move_or_clear_files, move_log_files=False, delete_old_log_files=True)
'''
# Simpler times: https://github.com/vishalnandagopal/ritter-bot/blob/c699e6d1814484ef64d8e5e7c3e562bad33ea90c/app.py
# This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
# (Copyright (c) 2022 Vishal N)