-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_numbers.py
155 lines (131 loc) · 4.74 KB
/
generate_numbers.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
import argparse
from datetime import datetime
import logging
from logging.handlers import RotatingFileHandler
import os
from pathlib import Path
import time
from constants import (
WEBSITES, WGET_DOWNLOADS, WGET_DOWNLOADS_AFRO,
SELENIUM_DOWNLOADS, SELENIUM_DOWNLOADS_2, SELENIUM_DOWNLOADS_AFRO,
POSTS, CSVS, CSVS_AFRO_WHO, LOGGER_BACKUP_COUNT, GENERATED_NUMBERS_PATH
)
from helpers import send_to_numbers_csv, send_to_numbers_html
from pandas_csvs import Csv
from soup_posts import SoupPosts
from soup_wgets import SoupWgets
from soup_selenium import SoupSelenium
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from helpers import Filename
# Create logger
logger = logging.getLogger('generate_numbers')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
printout_handler = logging.StreamHandler()
writetofile_handler = RotatingFileHandler(
'logs/log_generate_numbers.txt', mode='a', maxBytes=5*1024*1024,
backupCount=LOGGER_BACKUP_COUNT, encoding=None, delay=0)
printout_handler.setLevel(logging.INFO)
printout_handler.setFormatter(formatter)
writetofile_handler.setLevel(logging.INFO)
writetofile_handler.setFormatter(formatter)
logger.addHandler(printout_handler)
logger.addHandler(writetofile_handler)
# Create browser
global browser
options = Options()
options.headless = True
browser = webdriver.Firefox(options=options)
SoupPosts.set_browser(browser)
# Argparser
parser = argparse.ArgumentParser(description='Generate numbers.json')
parser.add_argument('--later', type=str, default=None,
help="generate later than the date, format: '%Y-%m-%d_%H-%M-%S'")
def get_paths(country, later=None):
ps = sorted([
p for p in Path('data').iterdir()
if p.name.startswith(f'{country.lower()}_') and
len(p.name.split('.')) == 2
])
if later is not None:
for p in ps:
if Filename(p).later_than(later):
return ps[ps.index(p):]
return []
else:
return ps
if __name__ == '__main__':
args = parser.parse_args()
if args.later is None:
later = args.later
else:
later = datetime.strptime(args.later, '%Y-%m-%d_%H-%M-%S')
logger.info(later)
numbers_path = Path(GENERATED_NUMBERS_PATH)
with numbers_path.open('w') as f:
f.write('{}')
wget_websites = [k for k in WEBSITES if k in WGET_DOWNLOADS]
for country in wget_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_html(p, country, SoupWgets, logger)
except FileNotFoundError:
pass
wget_websites = [k for k in WEBSITES if k in WGET_DOWNLOADS_AFRO]
for country in wget_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_html(p, country, SoupWgets, logger)
except FileNotFoundError:
pass
selenium_websites = [k for k in WEBSITES if k in SELENIUM_DOWNLOADS]
for country in selenium_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_html(p, country, SoupSelenium, logger)
except FileNotFoundError:
pass
selenium_websites = [k for k in WEBSITES if k in SELENIUM_DOWNLOADS_2]
for country in selenium_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_html(p, country, SoupSelenium, logger)
except FileNotFoundError:
pass
selenium_websites = [k for k in WEBSITES if k in SELENIUM_DOWNLOADS_AFRO]
for country in selenium_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_html(p, country, SoupSelenium, logger)
except FileNotFoundError:
pass
posts_websites = [k for k in WEBSITES if k in POSTS]
for country in posts_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_html(p, country, SoupPosts, logger)
except FileNotFoundError:
pass
csvs_websites = [k for k in WEBSITES if k in CSVS]
for country in csvs_websites:
ps = get_paths(country, later)
for p in ps:
try:
send_to_numbers_csv(p, country, CSVS[country], Csv, logger)
except FileNotFoundError:
pass
for country in CSVS_AFRO_WHO:
print(country)
ps = get_paths('Chad', later)
for p in ps:
try:
send_to_numbers_csv(p, country, ',', Csv, logger)
except FileNotFoundError:
pass