-
Notifications
You must be signed in to change notification settings - Fork 0
/
womparser.py
executable file
·338 lines (273 loc) · 10.4 KB
/
womparser.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, json, datetime, re, urllib, time, argparse
import requests
from lxml import html
class ParserData():
def __init__(self, **values):
self.__values = values
self.__values['type'] = self.type
for attr in values:
setattr(self, attr, values[attr])
def __setattr__(self, attr, value):
if not attr.startswith('_'):
self.__values[attr] = value
super().__setattr__(attr, value)
def tojson(self):
return json.dumps(self.__values, sort_keys=True)
def __str__(self):
return str(self.__values)
class CountryData(ParserData):
type = 'countrydata'
class DetailData(ParserData):
type = 'detaildata'
class PopulationData(ParserData):
type = 'populationdata'
class WOMParser():
def __init__(self, url):
self.url = url
def parsenumber(self, text):
if text is None:
return text
try:
return int(text.replace(',',''))
except ValueError:
return text
def parsecountries(self):
r = requests.get(self.url)
r.raise_for_status()
page = html.fromstring(r.content)
#table = page.xpath('//*[@id="main_table_countries_today"]/tbody[1]')[0]
table = page.xpath('//*[@id="main_table_countries_yesterday"]/tbody[1]')[0]
#country_element = table.xpath("//td[contains(., 'USA')]")[0]
#print(html.tostring(country_element))
#row = country_element.xpath("./..")[0]
for row in table.xpath("tr"):
#print(html.tostring(row))
if row.attrib.get("class") in ["total_row_world row_continent", "total_row_world"]:
continue
links = row.xpath("td/a")
if not links:
continue
country = links[0].text
data = [self.parsenumber(td.text) for td in row.xpath("td")]
baseidx = 1
total_cases = data[baseidx+1]
new_cases = data[baseidx+2]
total_deaths = data[baseidx+3]
new_deaths = data[baseidx+4]
total_recovered = data[baseidx+5]
active_cases = data[baseidx+6]
new_recovered = data[baseidx+7]
serious_critical = data[baseidx+8]
total_per_1m = data[baseidx+9]
total_deaths_per_1m = data[baseidx+10]
total_tests = data[baseidx+11]
total_tests_per_1m = data[baseidx+12]
continent = data[baseidx+14]
if len(links) > 1:
population = self.parsenumber(links[1].text)
else:
population = data[baseidx+13]
if False:
print("Country: ", country)
print("Total cases: ", total_cases)
print("New cases: ", new_cases)
print("Total deaths: ", total_deaths)
print("New deaths: ", new_deaths)
print("Active cases: ", active_cases)
print("Total recovered: ", total_recovered)
print("Serious, critical cases: ", serious_critical)
countrydata = CountryData(
country=country,
total_cases = total_cases,
new_cases = new_cases,
total_deaths = total_deaths,
new_deaths = new_deaths,
active_cases = active_cases,
total_recovered = total_recovered,
#new_recovered = new_recovered,
serious_critical = serious_critical,
total_per_1m = total_per_1m,
total_deaths_per_1m = total_deaths_per_1m,
total_tests = total_tests,
total_tests_per_1m = total_tests_per_1m,
date = str(datetime.date.today()-datetime.timedelta(days=1)),
continent = continent,
population = population,
#data = str(data),
)
yield countrydata
def parsepopulation(self):
r = requests.get(self.url)
r.raise_for_status()
page = html.fromstring(r.content)
table = page.xpath('//*[@id="example2"]/tbody[1]')[0]
for row in table.xpath("tr"):
#print(html.tostring(row))
if row.attrib.get("class") in ["total_row_world row_continent", "total_row_world"]:
continue
country_elem = row.xpath("td/a")
if not country_elem:
continue
country = country_elem[0].text
data = [self.parsenumber(td.text) for td in row.xpath("td")]
population = data[2]
if False:
print("Country: ", country)
print("Population: ", population)
popdata = PopulationData(
country=country,
population=data[2]
)
yield popdata
def parsedetails(self):
r = requests.get(self.url)
r.raise_for_status()
page = html.fromstring(r.content)
table = page.xpath('//*[@id="main_table_countries_yesterday"]/tbody[1]')[0]
for row in table.xpath("tr"):
if row.attrib.get("class") in ["total_row_world row_continent", "total_row_world"]:
continue
country_elem = row.xpath("td/a")
if not country_elem:
continue
country = country_elem[0].text
url = country_elem[0].attrib.get('href')
(cases, deaths, active) = self.parsecountry(country, url)
detaildata = DetailData(
country=country,
cases = cases,
deaths = deaths,
active = active,
)
yield detaildata
def parsecountry(self, country, url):
r = requests.get(urllib.parse.urljoin(self.url, url))
page = html.fromstring(r.content)
if False:
script = page.xpath('//div[@id="graph-cases-daily"]/following-sibling::script[1]')
if script:
cases = self.parsescript(script[0], "Daily Cases")
else:
cases = []
script = page.xpath('//div[@id="graph-deaths-daily"]/following-sibling::script[1]')
if script:
deaths = self.parsescript(script[0], "Daily Deaths")
else:
deaths = []
else:
script = page.xpath('//div[@class="tabbable-panel-cases"]/following-sibling::script[1]')
if script:
cases = self.parsescript(script[0], "Cases")
else:
cases = []
script = page.xpath('//div[@class="tabbable-panel-deaths"]/following-sibling::script[1]')
if script:
deaths = self.parsescript(script[0], "Deaths")
else:
deaths = []
script = page.xpath('//div[@id="graph-active-cases-total"]/following-sibling::script[1]')
if script:
active = self.parsescript(script[0], "Currently Infected")
else:
active = []
return cases, deaths, active
def parsescript(self, elem, attribute):
lines = elem.text.split('\n')
try:
xaxis = lines.index(' xAxis: {')
except ValueError:
xaxis = lines.index(' xAxis: {')
datevalues = self.parsevalues(lines[xaxis+1])
dates = self.parsedates(datevalues)
try:
vidx = lines.index(" name: '%s'," % attribute)
except ValueError:
vidx = lines.index(" name: '%s'," % attribute)
values = self.parsevalues(lines[vidx+3])
#print(dates)
#print(values)
return list(zip(dates, values))
def parsevalues(self, line):
r = re.compile(r".*(\[.*?\]).*")
v = r.match(line)
parsed = json.loads(v.group(1))
return parsed
def parsedates(self, datevalues):
dates = []
baseyear = 2020
prevd = None
for text in datevalues:
ts = time.strptime(text, '%b %d')
d = datetime.date(baseyear, ts.tm_mon, ts.tm_mday)
if prevd and prevd > d:
baseyear += 1
d = datetime.date(baseyear, ts.tm_mon, ts.tm_mday)
prevd = d
dates.append(d.strftime('%Y-%m-%d'))
return dates
def main():
p = argparse.ArgumentParser()
p.add_argument(
"-s",
"--stdout",
action="store_true",
dest="write_stdout",
default=False,
help="output to stdout",
)
p.add_argument(
"-C", "--csv", action="store_true", dest="csv", default=False, help="csv mode"
)
p.add_argument(
"-f",
"--outputfile",
action="store",
dest="outputfile",
default=None,
help="outputfile name",
)
p.add_argument(
"-o",
"--overwrite",
action="store_true",
dest="overwrite",
default=False,
help="overwrite existing outputfile",
)
p.add_argument("dataset", choices=['countries', 'details', 'population'])
options = p.parse_args()
dataset = options.dataset
cov_url = 'https://www.worldometers.info/coronavirus/'
pop_url = 'https://www.worldometers.info/world-population/population-by-country/'
if options.outputfile:
outputfile = options.outputfile
else:
datestr = datetime.date.today().strftime("%Y%m%d")
outputfile = "%s-%s.json" % (dataset, datestr)
if dataset == 'countries':
parser = WOMParser(cov_url)
parsermethod = parser.parsecountries
elif dataset == 'details':
parser = WOMParser(cov_url)
parsermethod = parser.parsedetails
elif dataset == 'population':
parser = WOMParser(pop_url)
parsermethod = parser.parsepopulation
else:
usage()
return
if parsermethod:
if options.write_stdout:
for event in parsermethod():
print(event.tojson())
else:
if os.path.exists(outputfile) and os.path.getsize(outputfile) > 0 and not options.overwrite:
print("%s exists" % outputfile)
return
with open(outputfile, 'w') as fp:
for event in parsermethod():
print(event.tojson(), file=fp)
if __name__ == "__main__":
main()