-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (46 loc) · 2.03 KB
/
main.py
File metadata and controls
55 lines (46 loc) · 2.03 KB
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
from datetime import datetime
from into_database import *
from into_csv import *
from my_settings import INPUT, OUTPUT
class Problem:
def __init__(self):
self.file_keyword_lst = INPUT
self.file_database = OUTPUT
def _read_keywords(self) -> [str]:
try:
# Return a list of keywords saved in the folder of this project.
with open(self.file_keyword_lst, 'r') as file:
# removes blanks at the end (e.g.: ' python ' -> ' python')
keyword_lst = [line.rstrip() for line in file]
except IOError as e:
# [ERROR] No such file or directory: keywords.txt
print('[ERROR] {}: {}'.format(e.args[1], self.file_keyword_lst))
exit()
return keyword_lst
@staticmethod
def _get_current_timestamp():
now = datetime.utcnow()
# Since we will be fetching data N times a day, minutes and seconds are not so important
current_time_grained_to_hours = datetime(now.year, now.month, now.day, now.hour, 0, 0)
return int(current_time_grained_to_hours.strftime("%Y%m%d%H")) # e.g.: 2021021224
def iterate_keywords(self):
keyword_lst = self._read_keywords()
# UNCOMMENT FOR DATABASE USAGE
# database = Database(self.file_database)
# database.create_tables() # if not exist
# We need to know WHEN a tweet had N likes
timestamp = self._get_current_timestamp()
if not keyword_lst:
print('[ERROR] Empty list! Empty file?')
exit()
for keyword in keyword_lst:
# todo: code a decision maker for database or table
# UNCOMMENT FOR DATABASE USAGE
# database.fetch_and_insert_into_tables(keyword, timestamp)
# UNCOMMENT FOR TABLES USAGE
fetch_and_insert_into_csv(keyword, timestamp)
if __name__ == '__main__':
backend = Problem()
Problem.iterate_keywords(backend)
# test = Database(OUTPUT)
# test.select_all()