-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfetch_data.py
More file actions
executable file
·1296 lines (1162 loc) · 46.3 KB
/
Copy pathfetch_data.py
File metadata and controls
executable file
·1296 lines (1162 loc) · 46.3 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
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
################################################################################
# Name: fetch_data.py
# Purpose: This Python 3 script fetches COVID-19 case data from the GitHub
# repository and REST API of Johns Hopkins CSSE, DXY, KCDC, and
# StatisticheCoronavirus, and creates GeoJSON and CSV files.
# Author: Huidae Cho
# Since: February 2, 2020
#
# Copyright (C) 2020, Huidae Cho <https://idea.isnew.info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
################################################################################
import requests
import io
import csv
import json
import datetime
import re
import os
import glob
import copy
import unicodedata
import traceback
import sys
import dic
import config
ts_confirmed_url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
daily_url_format = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/{date}.csv'
features_url = 'https://services9.arcgis.com/N9p5hsImWXAccRNI/arcgis/rest/services/Nc2JKvYFoAEOFCG5JSI6/FeatureServer/1/query?where=1%3D1&outFields=*&f=json'
kcdc_country_url = 'http://ncov.mohw.go.kr/bdBoardList_Real.do'
kcdc_country_re = '누적 확진자 현황.*?\(([0-9]+)\.([0-9]+).*?([0-9]+)시.*?기준\).*?<td>([0-9,]+)</td>\s*<td>([0-9,]+)</td>\s*<td>[0-9,]+</td>\s*<td>([0-9,]+)</td>'
kcdc_provinces_url = 'http://ncov.mohw.go.kr/bdBoardList_Real.do?brdGubun=13'
kcdc_provinces_re = '([0-9]+)\.([0-9]+)\.\s*([0-9]+)시.*?기준.*?<tr class="sumline">.*?</tr>.*?(<tr>.+?)</tbody>'
kcdc_provinces_subre = '>([^>]+)</th>.*?<[^>]+?s_type1[^>]+>\s*([0-9,]+)\s*<.+?s_type4[^>]+>\s*([0-9,]+)\s*<.+?s_type2[^>]+>\s*([0-9,]+)\s*<'
dxy_url = 'https://ncov.dxy.cn/ncovh5/view/pneumonia'
dxy_re = 'window\.getListByCountryTypeService2true.*?"createTime":([0-9]+),.*window\.getAreaStat = (.*?)\}catch\(e\)'
statistichecoronavirus_url = 'https://statistichecoronavirus.it/coronavirus-italia/'
statistichecoronavirus_re = '<tr[^>]*>\s*<td[^>]*>(?:<[^>]*>){2}([^<>]*?)<[^>]*></td>\s*<td[^>]*>[^<>]*?</td>\s*<td[^>]*>([^<>]*?)</td>\s*<td[^>]*>[^<>]*?</td>\s*<td[^>]*>[^<>]*?</td>\s*<td[^>]*>([^<>]*?)</td>\s*<td[^>]*>([^<>]*?)</td>\s*</tr>'
minsal_url = 'https://www.minsal.cl/nuevo-coronavirus-2019-ncov/casos-confirmados-en-chile-covid-19/'
minsal_re = '<tr[^>]*>.*?<td[^>]*>([^<>]+)</td>.*?<td[^>]*>([0-9.]+)</td>.*?(?:<td[^>]*>[0-9.]+</td>.*?){3}<td[^>]*>([0-9.]+)</td>.*?<td[^>]*>[0-9,.]+ *%</td>.*?</tr>'
minsal_total_re = '<tr[^>]*>.*?<td[^>]*><strong>Total</strong></td>.*?<td[^>]*><strong>([0-9.]+)</strong></td>.*?(?:<td[^>]*><strong>[0-9.]+</strong></td>.*?){3}<td[^>]*><strong>([0-9.]+)</strong></td>.*?<td[^>]*><strong>[0-9,.]+ *%</strong></td>.*?</tr>.*?<tr[^>]*>.*?<td[^>]*><strong>Casos recuperados a nivel nacional\s*</strong></td>.*?<td[^>]*><strong>([0-9.]+)</strong></td>.*?</tr>'
geocode_country_url = f'http://dev.virtualearth.net/REST/v1/Locations?countryRegion={{country}}&key={config.bing_maps_key}'
geocode_province_url = f'http://dev.virtualearth.net/REST/v1/Locations?countryRegion={{country}}&adminDistrict={{province}}&key={config.bing_maps_key}'
geocode_admin2_url = f'http://dev.virtualearth.net/REST/v1/Locations?countryRegion={{country}}&adminDistrict={{admin2}},{{province}}&key={config.bing_maps_key}'
geodata_json = 'geodata.json'
data_csv = 'data.csv'
# use this dictionary to avoid geocoding the same province multiple times
coors_json = 'coors.json'
dates = []
data = []
key2data = {}
has_countries_to_display = True if len(config.countries_to_display) else False
has_duplicate_data = []
total_days = 0
def geocode(country, province='', admin2='', latitude=None, longitude=None):
# https://docs.microsoft.com/en-us/bingmaps/rest-services/common-parameters-and-types/location-and-area-types
# XXX: adminDistrict2 doesn't work?
# adminDistrict=County,State works!
# read existing data
if os.path.exists(coors_json):
with open(coors_json) as f:
coors = json.load(f)
else:
coors = {}
if admin2:
location = f'{admin2}, {province}, {country}'
geocode_url = geocode_admin2_url.format(country=country,
province=province, admin2=admin2)
elif province:
location = f'{province}, {country}'
geocode_url = geocode_province_url.format(country=country,
province=province)
else:
location = country
geocode_url = geocode_country_url.format(country=country)
if location not in coors:
if config.bing_maps_key == 'BING_MAPS_KEY':
raise Exception('Please set up bing_maps_key in config.py')
if config.bing_maps_referer == 'BING_MAPS_REFERER':
raise Exception('Please set up bing_maps_referer in config.py')
res = requests.get(geocode_url, headers={
'referer': config.bing_maps_referer
})
ret = res.json()
resources = ret['resourceSets'][0]['resources']
if len(resources):
coor = resources[0]['geocodePoints'][0]['coordinates']
latitude = coor[0]
longitude = coor[1]
coors[location] = {'latitude': latitude, 'longitude': longitude}
if latitude is not None and longitude is not None:
with open(coors_json, 'w') as f:
f.write(json.dumps(coors))
else:
latitude = coors[location]['latitude']
longitude = coors[location]['longitude']
return latitude, longitude
def fetch_csse_csv():
global total_days
print('Fetching CSSE CSV...')
ts_confirmed_res = requests.get(ts_confirmed_url)
with io.StringIO(ts_confirmed_res.content.decode()) as ts_confirmed_f:
ts_confirmed_reader = csv.reader(ts_confirmed_f)
header = ts_confirmed_reader.__next__()
# reverse order to find more recent and fully populated data first
j = 0
for i in range(len(header) - 1, 3, -1):
date = header[i].split('/')
year = 2000 + int(date[2])
month = int(date[0])
day = int(date[1])
date = f'{year}-{month:02}-{day:02}'
dates.insert(0, date)
print(f'{date}...', end='', flush=True)
j += 1
if j % 5 == 0:
print('')
fetch_csse_daily_csv(year, month, day)
total_days += 1
if j % 5:
print('')
for rec in data:
for category in ('confirmed', 'recovered', 'deaths'):
i = 0
insert = {}
for x in rec[category]:
date = x['time'].strftime('%Y-%m-%d')
while i < total_days - 1 and dates[i] < date:
insert[i] = {
'time': datetime.datetime.fromisoformat(
f'{dates[i]} 23:59:59+00:00'),
'count': 0,
}
i += 1
i += 1
for key in sorted(insert.keys()):
rec[category].insert(key, insert[key])
index = len(rec[category]) - 1
while i < total_days:
# TODO: aggregate
rec[category].append({
'time': datetime.datetime.fromisoformat(
f'{dates[i]} 23:59:59+00:00'),
'count': rec[category][index]['count']
})
i += 1
print('Fetching CSSE CSV completed')
def generate_key(country, province, admin2):
if admin2:
key = f'{admin2}, {province}, {country}'
elif province:
key = f'{province}, {country}'
else:
key = country
return key
def read_key(key):
x = key.split(', ')
country = x.pop()
province = x.pop() if len(x) else ''
admin2 = x.pop() if len(x) else ''
return country, province, admin2
def fetch_csse_daily_csv(year, month, day):
date_iso = f'{year}-{month:02}-{day:02}'
date_csv = f'{month:02}-{day:02}-{year}'
url = daily_url_format.format(date=date_csv)
res = requests.get(url)
with io.StringIO(res.content.decode()) as f:
reader = csv.reader(f)
header = reader.__next__()
ncols = len(header)
for row in reader:
admin2 = latitude = longitude = ''
if ncols == 14 or ncols == 12:
# since 03-22-2020
# 0: FIPS
# 1: Admin2
# 2: Province_State
# 3: Country_Region
# 4: Last_Update
# 5: Lat
# 6: Long_
# 7: Confirmed
# 8: Deaths
# 9: Recovered
# 10: Active
# 11: Combined_Key
# since 05-29-2020
# 12: Incidence_Rate
# 13: Case-Fatality_Ratio
admin2 = '' if row[1].strip() == 'None' else row[1].strip()
province = '' if row[2].strip() == 'None' else row[2].strip()
country = row[3].strip()
# don't use last_updated; there can be duplicate entries with
# different counts
last_updated = row[4]
if row[5] and row[5] != '0':
latitude = float(row[5])
if row[6] and row[6] != '0':
longitude = float(row[6])
c = int(0 if row[7] == '' else row[7])
d = int(0 if row[8] == '' else row[8])
r = int(0 if row[9] == '' else row[9])
elif ncols == 8:
# since 03-01-2020
# 0: Province/State
# 1: Country/Region
# 2: Last Update
# 3: Confirmed
# 4: Deaths
# 5: Recovered
# 6: Latitude
# 7: Longitude
province = '' if row[0].strip() == 'None' else row[0].strip()
country = row[1].strip()
last_updated = row[2]
c = int(0 if row[3] == '' else row[3])
d = int(0 if row[4] == '' else row[4])
r = int(0 if row[5] == '' else row[5])
if row[6] and row[6] != '0':
latitude = float(row[6])
if row[7] and row[7] != '0':
longitude = float(row[7])
elif ncols == 6:
# since 01-22-2020
# 0: Province/State
# 1: Country/Region
# 2: Last Update
# 3: Confirmed
# 4: Deaths
# 5: Recovered
province = '' if row[0].strip() == 'None' else row[0].strip()
country = row[1].strip()
last_updated = row[2]
c = int(0 if row[3] == '' else row[3])
d = int(0 if row[4] == '' else row[4])
r = int(0 if row[5] == '' else row[5])
else:
raise Exception('Unexpected format for daily report '
f'{date_csv}')
if country in dic.co_names:
country = dic.co_names[country]
if ', ' in province and not admin2:
admin2, province = province.split(', ')
if ' County' in admin2:
admin2 = admin2.replace(' County', '')
elif ' Parish' in admin2:
admin2 = admin2.replace(' Parish', '')
if province in dic.us_states.keys():
province = dic.us_states[province]
if ',' in admin2:
raise Exception('Commas are not allowed in admin2 names: '
f'{admin2} in {date_csv}')
if ',' in province:
raise Exception('Commas are not allowed in province names: '
f'{province} in {date_csv}')
if ',' in country:
raise Exception('Commas are not allowed in country names: '
f'{country} in {date_csv}')
last_updated = datetime.datetime.fromisoformat(
f'{date_iso} 23:59:59+00:00')
key = generate_key(country, province, admin2)
if key in dic.keymap:
key = dic.keymap[key]
country, province, admin2 = read_key(key)
if key in dic.latlong:
latlong = dic.latlong[key]
latitude = latlong['latitude']
longitude = latlong['longitude']
if not latitude or not longitude:
latitude, longitude = geocode(country, province, admin2)
if not latitude or not longitude:
raise Exception('Latitude or longitude is not defined for '
f'{key} in {date_csv}')
if key not in key2data:
if total_days > 0 and \
(country != 'United States' or
province not in dic.us_states.values() or
admin2 == 'Unassigned'):
continue
# new record not in data
index = len(data)
key2data[key] = index
# create and populate three lists with time series data
confirmed = []
recovered = []
deaths = []
data.append({
'country': country,
'province': province,
'admin2': admin2,
'latitude': latitude,
'longitude': longitude,
'confirmed': confirmed,
'recovered': recovered,
'deaths': deaths
})
else:
# retrieve existing lists
index = key2data[key]
rec = data[index]
confirmed = rec['confirmed']
recovered = rec['recovered']
deaths = rec['deaths']
found = False
for i in range(0, len(confirmed)):
if rec['confirmed'][i]['time'] == last_updated:
if c > rec['confirmed'][i]['count']:
rec['confirmed'][i]['count'] = c
if c > rec['recovered'][i]['count']:
rec['recovered'][i]['count'] = c
if c > rec['deaths'][i]['count']:
rec['deaths'][i]['count'] = c
found = True
break
if found:
continue
confirmed.insert(0, {
'time': last_updated,
'count': c
})
recovered.insert(0, {
'time': last_updated,
'count': r
})
deaths.insert(0, {
'time': last_updated,
'count': d
})
def fetch_all_features(features_url):
count = 1000
offset = 0
features = []
while True:
url = f'{features_url}&resultRecordCount={count}&resultOffset={offset}'
if config.app_url == 'APP_URL':
raise Exception('Please set up app_url in config.py')
res = requests.get(url, headers={
'referer': config.app_url
})
res = json.loads(res.content.decode())
features.extend(res['features'])
if 'exceededTransferLimit' not in res or \
res['exceededTransferLimit'] == 'false':
break
offset += count
return features
def fetch_csse_rest():
global total_days
print('Fetching CSSE REST...')
features = fetch_all_features(features_url)
with open('data/csse_rest.json', 'w') as f:
f.write(json.dumps(features))
today_iso = datetime.datetime.utcnow().strftime('%Y-%m-%d 00:00:00+00:00')
today = datetime.datetime.fromisoformat(today_iso)
# try to find most up-to-date info from the REST server
for feature in features:
attr = feature['attributes']
c = int(attr['Confirmed'])
r = int(attr['Recovered'])
d = int(attr['Deaths'])
if c + r + d == 0:
continue
country = attr['Country_Region'].strip()
if country in dic.co_names:
country = dic.co_names[country]
province = attr['Province_State'].strip() \
if attr['Province_State'] else ''
admin2 = attr['Admin2'].strip() if attr['Admin2'] else ''
last_updated = datetime.datetime.fromtimestamp(
attr['Last_Update']/1000, tz=datetime.timezone.utc)
# sometimes, the last date in the CSV file is later than REST; in this
# case, let's use today's time at 00:00:00
if today > last_updated:
last_updated = today
if 'geometry' in feature:
latitude = feature['geometry']['y']
longitude = feature['geometry']['x']
key = generate_key(country, province, admin2)
if key in dic.keymap:
key = dic.keymap[key]
country, province, admin2 = read_key(key)
if key in dic.latlong:
latlong = dic.latlong[key]
latitude = latlong['latitude']
longitude = latlong['longitude']
if not latitude or not longitude:
latitude, longitude = geocode(country, province, admin2)
if not latitude or not longitude:
raise Exception('Latitude or longitude is not defined for '
f'{key} in {date_csv}')
if key not in key2data:
# new record not in data
index = len(data)
key2data[key] = index
# create and populate three lists with REST data
confirmed = copy.deepcopy(data[0]['confirmed'])
recovered = copy.deepcopy(data[0]['recovered'])
deaths = copy.deepcopy(data[0]['deaths'])
if len(confirmed) > total_days:
index = len(confirmed) - 1
del confirmed[index], recovered[index], deaths[index]
for i in range(0, total_days):
confirmed[i]['count'] = recovered[i]['count'] = \
deaths[i]['count'] = 0
data.append({
'country': country,
'province': province,
'admin2': admin2,
'latitude': latitude,
'longitude': longitude,
'confirmed': confirmed,
'recovered': recovered,
'deaths': deaths
})
if c:
print(f'REST confirmed: {admin2}, {province}, {country}, '
f'0 => {c}')
if r:
print(f'REST recovered: {admin2}, {province}, {country}, '
f'0 => {r}')
if d:
print(f'REST deaths : {admin2}, {province}, {country}, '
f'0 => {d}')
else:
# retrieve existing lists
index = key2data[key]
rec = data[index]
country = rec['country']
province = rec['province']
admin2 = rec['admin2']
confirmed = rec['confirmed']
recovered = rec['recovered']
deaths = rec['deaths']
time = confirmed[len(confirmed) - 1]['time']
# I found this case where a time from the spreadsheet is more
# recent than the last updated time from the REST server
if time > last_updated:
last_updated = time
index = len(confirmed) - 1
c = max(confirmed[index]['count'], c)
r = max(recovered[index]['count'], r)
d = max(deaths[index]['count'], d)
if c != confirmed[index]['count']:
print(f'REST confirmed: {admin2}, {province}, {country}, '
f'{confirmed[index]["count"]} => {c}')
if r != recovered[index]['count']:
print(f'REST recovered: {admin2}, {province}, {country}, '
f'{recovered[index]["count"]} => {r}')
if d != deaths[index]['count']:
print(f'REST deaths : {admin2}, {province}, {country}, '
f'{deaths[index]["count"]} => {d}')
if len(confirmed) == total_days + 1:
continue
confirmed.append({
'time': last_updated,
'count': c
}),
recovered.append({
'time': last_updated,
'count': r
}),
deaths.append({
'time': last_updated,
'count': d
})
dates.append(today_iso.split()[0])
total_days += 1
# oops! some provinces are missing from the REST data?
for rec in data:
confirmed = rec['confirmed']
recovered = rec['recovered']
deaths = rec['deaths']
index = len(confirmed) - 1
if index == total_days - 1:
continue
confirmed.append(confirmed[index])
recovered.append(recovered[index])
deaths.append(deaths[index])
print('Fetching CSSE REST completed')
def clean_us_data():
country = 'United States'
others_indices = []
n = len(data)
for i in range(0, n):
rec = data[i]
if rec['country'] != country:
continue
province = rec['province']
admin2 = rec['admin2']
if province not in dic.us_states.values():
# non-CONUS records
others_indices.append(i)
if not admin2:
rec['admin2'] = province
continue
elif admin2:
# CONUS admin2 records
continue
# state-wide records
confirmed = rec['confirmed']
recovered = rec['recovered']
deaths = rec['deaths']
admin2_indices = []
for j in range(0, n):
rec2 = data[j]
if rec2['country'] == country and \
rec2['province'] == province and \
rec2['admin2']:
admin2_indices.append(j)
if rec2['admin2'] == 'Unassigned':
rec2['latitude'] = rec['latitude']
rec2['longitude'] = rec['longitude']
# no admin2 records
if not len(admin2_indices):
continue
for j in range(0, len(confirmed)):
c = r = d = 0
for k in admin2_indices:
rec2 = data[k]
c += rec2['confirmed'][j]['count']
r += rec2['recovered'][j]['count']
d += rec2['deaths'][j]['count']
if c > confirmed[j]['count']:
print(f'US confirmed: {province}, {country}, {dates[j]}, '
f'{confirmed[j]["count"]} => {c}')
confirmed[j]['count'] = c
if r > recovered[j]['count']:
print(f'US recovered: {province}, {country}, {dates[j]}, '
f'{recovered[j]["count"]} => {r}')
recovered[j]['count'] = r
if d > deaths[j]['count']:
print(f'US deaths : {province}, {country}, {dates[j]}, '
f'{deaths[j]["count"]} => {d}')
deaths[j]['count'] = d
latitude, longitude = geocode(country)
if len(others_indices):
confirmed = []
recovered = []
deaths = []
for i in range(0, total_days):
c = r = d = 0
last_updated = None
for j in others_indices:
rec = data[j]
time = rec['confirmed'][i]['time']
if last_updated is None or time > last_updated:
last_updated = time
c += rec['confirmed'][i]['count']
r += rec['recovered'][i]['count']
d += rec['deaths'][i]['count']
confirmed.append({
'time': last_updated,
'count': c
})
recovered.append({
'time': last_updated,
'count': r
})
deaths.append({
'time': last_updated,
'count': d
})
province = 'Others'
print(f'US confirmed: {province}, {country}, {c}')
print(f'US recovered: {province}, {country}, {r}')
print(f'US deaths : {province}, {country}, {d}')
data.append({
'country': country,
'province': province,
'admin2': '',
'latitude': latitude,
'longitude': longitude,
'confirmed': confirmed,
'recovered': recovered,
'deaths': deaths
})
confirmed = []
recovered = []
deaths = []
for i in range(0, total_days):
c = r = d = 0
last_updated = None
for rec in data:
if rec['country'] == country and not rec['admin2']:
time = rec['confirmed'][i]['time']
if last_updated is None or time > last_updated:
last_updated = time
c += rec['confirmed'][i]['count']
r += rec['recovered'][i]['count']
d += rec['deaths'][i]['count']
confirmed.append({
'time': last_updated,
'count': c
})
recovered.append({
'time': last_updated,
'count': r
})
deaths.append({
'time': last_updated,
'count': d
})
print(f'US confirmed: {country}, {c}')
print(f'US recovered: {country}, {r}')
print(f'US deaths : {country}, {d}')
data.append({
'country': country,
'province': '',
'admin2': '',
'latitude': latitude,
'longitude': longitude,
'confirmed': confirmed,
'recovered': recovered,
'deaths': deaths
})
def get_data_filename(country, province=None):
return 'data/' + (province + ', ' if province else '') + country + '.csv'
def fetch_kcdc_country():
print('Fetching KCDC country...')
res = requests.get(kcdc_country_url).content.decode()
m = re.search(kcdc_country_re, res, re.DOTALL)
if not m:
raise Exception('Fetching KCDC country failed')
print('Fetching KCDC country matched')
year = 2020
month = int(m[1])
day = int(m[2])
hour = int(m[3])
confirmed = int(m[4].replace(',', ''))
recovered = int(m[5].replace(',', ''))
deaths = int(m[6].replace(',', ''))
last_updated_iso = f'{year}-{month:02}-{day:02} {hour:02}:00:00+09:00'
filename = get_data_filename('South Korea')
add_header = True
if os.path.exists(filename):
add_header = False
with open(filename) as f:
reader = csv.reader(f)
for row in reader:
pass
time = datetime.datetime.fromisoformat(row[0]).astimezone(
datetime.timezone.utc)
if time >= datetime.datetime.fromisoformat(last_updated_iso).\
astimezone(datetime.timezone.utc):
return
with open(filename, 'a') as f:
if add_header:
f.write('time,confirmed,recovered,deaths\n')
f.write(f'{last_updated_iso},{confirmed},{recovered},{deaths}\n')
print('Fetching KCDC country completed')
def fetch_kcdc_provinces():
print('Fetching KCDC provinces...')
if not kcdc_provinces_re:
print('Fetching KCDC provinces skipped')
return
res = requests.get(kcdc_provinces_url).content.decode()
m = re.search(kcdc_provinces_re, res, re.DOTALL)
if not m:
raise Exception('Fetching KCDC provinces 1/2 failed')
print('Fetching KCDC provinces 1/2 matched')
country = 'South Korea'
year = 2020
month = int(m[1])
day = int(m[2])
hour = int(m[3])
last_updated_iso = f'{year}-{month:02}-{day:02} {hour:02}:00:00+09:00'
matches = re.findall(kcdc_provinces_subre, m[4])
if not matches:
raise Exception('Fetching KCDC provinces 2/2 failed')
print('Fetching KCDC provinces 2/2 matched')
last_updated = None
for m in matches:
province = dic.en[m[0]]
confirmed = int(m[1].replace(',', ''))
recovered = int(m[2].replace(',', ''))
deaths = int(m[3].replace(',', ''))
filename = get_data_filename(country, province)
add_header = True
if os.path.exists(filename):
add_header = False
with open(filename) as f:
reader = csv.reader(f)
for row in reader:
pass
time = datetime.datetime.fromisoformat(row[0]).astimezone(
datetime.timezone.utc)
if time >= datetime.datetime.fromisoformat(last_updated_iso).\
astimezone(datetime.timezone.utc):
continue
with open(filename, 'a') as f:
if add_header:
f.write('time,confirmed,recovered,deaths\n')
f.write(f'{last_updated_iso},{confirmed},{recovered},{deaths}\n')
print('Fetching KCDC provinces completed')
def fetch_dxy():
print('Fetching DXY...')
res = requests.get(dxy_url).content.decode()
m = re.search(dxy_re, res, re.DOTALL)
if not m:
raise Exception('Fetching DXY failed')
print('Fetching DXY matched')
last_updated = datetime.datetime.fromtimestamp(int(m[1])/1000,
tz=datetime.timezone.utc)
last_updated_iso = last_updated.strftime('%Y-%m-%d %H:%M:%S+00:00')
for rec in json.loads(m[2]):
province = rec['provinceShortName']
if province not in dic.en:
return
province = dic.en[province]
confirmed = rec['confirmedCount']
recovered = rec['curedCount']
deaths = rec['deadCount']
country = 'China'
if province == 'Taiwan':
country = 'Taiwan'
province = ''
filename = get_data_filename(country, province)
add_header = True
if os.path.exists(filename):
add_header = False
with open(filename) as f:
reader = csv.reader(f)
for row in reader:
pass
time = datetime.datetime.fromisoformat(row[0]).astimezone(
datetime.timezone.utc)
if time >= last_updated:
continue
with open(filename, 'a') as f:
if add_header:
f.write('time,confirmed,recovered,deaths\n')
f.write(f'{last_updated_iso},{confirmed},{recovered},{deaths}\n')
print('Fetching DXY completed')
def update_fetched_data(country, province, confirmed, recovered, deaths):
now_iso = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S+00:00')
today = now_iso.split()[0]
filename = get_data_filename(country, province)
add_header = True
overwrite_row = 0
if os.path.exists(filename):
add_header = False
with open(filename) as f:
reader = csv.reader(f)
for row in reader:
overwrite_row += 1
time_iso = row[0]
time_date = time_iso.split()[0]
if time_date != today:
overwrite_row = 0
if overwrite_row:
with open(filename, 'r+') as f:
for i in range(0, overwrite_row - 1):
f.readline()
seek = f.tell()
f.seek(seek)
f.write(f'{now_iso},{confirmed},{recovered},{deaths}\n')
else:
with open(filename, 'a') as f:
if add_header:
f.write('time,confirmed,recovered,deaths\n')
f.write(f'{now_iso},{confirmed},{recovered},{deaths}\n')
def fetch_statistichecoronavirus():
print('Fetching StatisticheCoronavirus...')
res = requests.get(statistichecoronavirus_url).content.decode(
errors='replace')
matches = re.findall(statistichecoronavirus_re, res, re.DOTALL)
if not matches:
raise Exception('Fetching StatisticheCoronavirus failed')
print('Fetching StatisticheCoronavirus matched')
country = 'Italy'
for m in matches:
province = m[0]
confirmed = int(m[1].replace('.', ''))
recovered = int(m[3].replace('.', ''))
deaths = int(m[2].replace('.', ''))
update_fetched_data(country, province, confirmed, recovered, deaths)
print('Fetching StatisticheCoronavirus completed')
# https://stackoverflow.com/a/518232
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
def fetch_minsal():
print('Fetching Minsal...')
res = requests.get(minsal_url).content.decode()
matches = re.findall(minsal_re, res, re.DOTALL)
if not matches:
raise Exception('Fetching Minsal 1/2 failed')
print('Fetching Minsal 1/2 matched')
country = 'Chile'
for m in matches:
province = strip_accents(m[0].replace('’', ''))
confirmed = int(m[1].replace('.', ''))
recovered = 0
deaths = int(m[2].replace('.', ''))
update_fetched_data(country, province, confirmed, recovered, deaths)
m = re.search(minsal_total_re, res, re.DOTALL)
if not m:
raise Exception('Fetching Minsal 2/2 failed')
print('Fetching Minsal 2/2 matched')
province = None
confirmed = int(m[1].replace('.', ''))
recovered = int(m[3].replace('.', ''))
deaths = int(m[2].replace('.', ''))
update_fetched_data(country, province, confirmed, recovered, deaths)
print('Fetching Minsal completed')
def merge_local_data():
global total_days
for filename in glob.glob('data/*.csv'):
key = filename.replace('data/', '').replace('.csv', '')
if key.startswith('csse_'):
continue
country, province, admin2 = read_key(key)
found = False
for rec in data:
if country == rec['country'] and \
province == rec['province'] and \
admin2 == rec['admin2']:
found = True
break
if found:
confirmed = rec['confirmed']
recovered = rec['recovered']
deaths = rec['deaths']
index = len(confirmed) - 1
time = confirmed[index]['time']
with open(filename) as f:
reader = csv.reader(f)
for row in reader:
pass
last_updated = datetime.datetime.fromisoformat(row[0]).\
astimezone(datetime.timezone.utc)
if time > last_updated:
last_updated = time
c = int(row[1])
r = int(row[2])
d = int(row[3])
if c > confirmed[index]['count']:
print(f'data confirmed: {province}, {country}, '
f'{confirmed[index]["count"]} => {c}')
confirmed[index] = {
'time': last_updated,
'count': c
}
if r > recovered[index]['count']:
print(f'data recovered: {province}, {country}, '
f'{recovered[index]["count"]} => {r}')
recovered[index] = {
'time': last_updated,
'count': r
}
if d > deaths[index]['count']:
print(f'data deaths : {province}, {country}, '
f'{deaths[index]["count"]} => {d}')
deaths[index] = {
'time': last_updated,
'count': d
}
else:
if province and country not in has_duplicate_data:
has_duplicate_data.append(country)
latitude, longitude = geocode(country, province, admin2)
confirmed = []
recovered = []
deaths = []
with open(filename) as f:
reader = csv.reader(f)
reader.__next__()
for row in reader:
time = datetime.datetime.fromisoformat(row[0]).\
astimezone(datetime.timezone.utc)
if config.use_local_data_only:
date = time.strftime('%Y-%m-%d')
if date not in dates:
dates.append(date)