-
Notifications
You must be signed in to change notification settings - Fork 0
/
soup_posts.py
232 lines (209 loc) · 7.88 KB
/
soup_posts.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
from inspect import classify_class_attrs
import logging
from logging.handlers import RotatingFileHandler
import re
import unicodedata
from bs4 import BeautifulSoup
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
from helpers import wait_until_xpath
from constants import LOGGER_BACKUP_COUNT
# Create logger
logger = logging.getLogger('soup_posts')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
printout_handler = logging.StreamHandler()
writetofile_handler = RotatingFileHandler(
'logs/log_posts.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)
class SoupPosts:
@classmethod
def set_browser(cls, browser):
cls.browser = browser
@classmethod
def monaco(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'Covid-19' in tag.get('title', '') and \
' cas ' in tag.get('title', '')
def post_condition(tag):
if tag is None:
return False
return tag.name == 'p' and \
len(tag.contents) == 1 and \
'personnes guéries s’élève' in tag.contents[0]
tag = soup.find(condition)
url = 'https://www.gouv.mc' + tag['href']
# timed_out = wait_until_xpath(
# cls.browser, url,
# "//*[contains(text(), 'personnes guéries s’élève')]",
# logger)
# if timed_out:
# return tag, None
# post_source = cls.browser.page_source
# post_soup = BeautifulSoup(post_source, 'html.parser')
# post_tag = post_soup.find(post_condition)
# logger.debug('Monaco post tag: %s', post_tag)
# cases = re.search(
# 'personnes guéries s’élève [a-zàâçéèêëîïôûùüÿñæœ .-]*([0-9]+)\.',
# post_tag.contents[0]
# ).group(1)
return tag, url
@classmethod
def san_marino(cls, soup):
def condition(tag):
if tag is None:
return False
keywords = ['aggiornamento', 'epidemia', 'covid-19', 'campagna', 'vaccinale']
return tag.name == 'a' and \
all([kw in tag.get('title', '').lower() for kw in keywords])
def post_condition(tag):
if tag is None:
return False
return tag.name == 'p' and \
len(tag.contents) == 1 and \
'Il numero totale di persone contagiate' in tag.contents[0] and \
'di ieri è di' in tag.contents[0]
tag = soup.find(condition)
url = 'https://www.iss.sm' + tag['href']
# timed_out = wait_until_xpath(
# cls.browser, url,
# # Changed 2021-01-12
# # "//*[contains(text(), 'Il numero totale di persone "
# # "contagiate individuate dall’inizio della pandemia "
# # "fino alla mezzanotte di ieri è di')]",
# "//*[contains(text(), 'Il numero totale di persone contagiate')]",
# logger)
# if timed_out:
# cls.browser.close()
# cls.browser = webdriver.Firefox(options=options)
# logger.info('SoupPosts browser reloaded')
# return tag, None
# post_source = cls.browser.page_source
# post_soup = BeautifulSoup(post_source, 'html.parser')
# post_tag = post_soup.find(post_condition)
# cases = re.search(
# # Changed 2021-01-12
# # 'Il numero totale di persone contagiate individuate '
# # 'dall’inizio della pandemia fino alla mezzanotte '
# # 'di ieri è di ([0-9.]+)',
# 'Il numero totale di persone contagiate .* di ieri è di ([0-9.]+)',
# post_tag.contents[0]
# ).group(1).replace('.', '')
return url, None
@classmethod
def algeria(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'Coronavirus:' in tag.contents[0]
tag = soup.find(condition)
url = 'https://www.aps.dz' + tag['href']
return tag, url
@classmethod
def angola(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'h2' and \
'COVID-19' in tag.contents[0]
tag = soup.find(condition).parent.parent.parent.parent
url = 'https://governo.gov.ao' + tag['href']
return tag, url
@classmethod
def burkina_faso(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'Maladie à Coronavirus' in tag.get('title', '')
tag = soup.find(condition)
url = tag['href']
return tag, url
@classmethod
def comoros(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'h5' and \
'card-title' in tag.get('class', []) and \
'Pandémie COVID' in tag.text
tag = soup.find(condition).parent
url = 'https://stopcoronavirus.km/' + tag.a['href']
return tag, url
@classmethod
def cote_divoire_two(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'Point de la Situation de la COVID-19' in tag.text
tag = soup.find(condition)
url = tag['href']
return tag, url
@classmethod
def kenya(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'Press Statement on Covid-19' in tag.text
tag = soup.find(condition)
url = tag['href']
return tag, url
@classmethod
def madagascar(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'COVID-19' in unicodedata.normalize(
'NFKC', tag.text) and \
'Situation épidémiologique' in unicodedata.normalize(
'NFKC', tag.text)
tag = soup.find(condition)
url = tag['href']
return tag, url
@classmethod
def senegal(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'a' and \
'Coronavirus : Communiqué de Presse' in tag.text
tag = soup.find(condition)
url = 'https://www.sante.gouv.sn' + tag['href']
return tag, url
@classmethod
def south_africa_one(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'h2' and \
'blog-shortcode-post-title entry-title' in ' '.join(tag.get('class', [])) and \
'Update on Covid-19' in tag.text
tag = soup.find(condition)
url = tag.a['href']
return tag, url
@classmethod
def south_africa_two(cls, soup):
def condition(tag):
if tag is None:
return False
return tag.name == 'h3' and \
'elementor-post__title' in tag.get('class', []) and \
'LATEST CONFIRMED CASES OF COVID-19 IN SOUTH AFRICA' in tag.text
tag = soup.find(condition)
url = tag.a['href']
return tag, url