Created
July 29, 2020 11:30
-
-
Save nlitsme/6ac71d319be9090215f04803f73a1959 to your computer and use it in GitHub Desktop.
python scripts to show corona statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EMRO AF Afghanistan 647500 37171921 World;Asia;NonCKIR | |
EURO AL Albania 28748 2876591 World;Europe;NonEU | |
AFRO DZ Algeria 2381740 34178188 World;Africa;North Africa | |
WPRO AS American Samoa (United States) 199 55465 World;Oceania;Polynesia | |
EURO AD Andorra 468 77281 World;Europe;NonEU | |
AFRO AO Angola 1246700 12799293 World;Africa;Central Africa | |
AMRO AI Anguilla (United Kingdom) 91 14731 World;North America | |
AMRO AG Antigua and Barbuda 442 96286 World;North America | |
AMRO AR Argentina 2766890 44361150 World;South America | |
EURO AM Armenia 29743 2951745 World;Europe;NonEU | |
AMRO AW Aruba (Kingdom of the Netherlands) 180 105845 World;North America | |
WPRO ?? Ashmore and Cartier Islands (Australia) 199 0 World;Oceania;Australasia | |
WPRO AU Australia 7686850 24898152 World;Oceania;Australasia | |
EURO AT Austria 83858 8823054 World;Europe;EU | |
EURO AZ Azerbaijan 86600 10027874 World;Europe;NonEU | |
EMRO BH Bahrain 760 1569446 World;Asia;NonCKIR | |
SEARO BD Bangladesh 147570 161376708 World;Asia;NonCKIR | |
AMRO BB Barbados 430 286641 World;North America | |
EURO BY Belarus 207560 9504700 World;Europe;NonEU | |
EURO BE Belgium 30528 11358357 World;Europe;EU | |
AMRO BZ Belize 22966 383071 World;North America | |
AFRO BJ Benin 112620 8791832 World;Africa;West Africa | |
AMRO BM Bermuda (United Kingdom) 54 62756 World;North America | |
SEARO BT Bhutan 38394 754388 World;Asia;NonCKIR | |
AMRO BO Bolivia 1098580 11353142 World;South America | |
AMRO BQ Bonaire (Kingdom of the Netherlands) 294 12093 World;North America | |
EURO BA Bosnia and Herzegovina 51129 3531159 World;Europe;NonEU | |
AFRO BW Botswana 600370 1990876 World;Africa;Southern Africa | |
AMRO ?? Bouvet Island (Norway) 49 0 World;South America | |
AMRO BR Brazil 8514877 209469323 World;South America | |
AMRO VG British Virgin Islands (United Kingdom) 151 29802 World;North America | |
WPRO BN Brunei 5765 428963 World;Asia;NonCKIR | |
EURO BG Bulgaria 110910 7101859 World;Europe;EU | |
AFRO BF Burkina Faso 274200 15746232 World;Africa;West Africa | |
AFRO BI Burundi 27830 8988091 World;Africa;East Africa | |
WPRO KH Cambodia 181035 16249792 World;Asia;NonCKIR | |
AFRO CM Cameroon 475440 18879301 World;Africa;Central Africa | |
AMRO CA Canada 9984670 37064562 World;North America | |
AFRO ?? Canary Islands (Spain) 7492 2154905 World;Africa;North Africa | |
AFRO CV Cape Verde 4033 429474 World;Africa;West Africa | |
AMRO KY Cayman Islands (United Kingdom) 264 64174 World;North America | |
AFRO CF Central African Republic 622984 4511488 World;Africa;Central Africa | |
AFRO ?? Ceuta (Spain) 20 85107 World;Africa;North Africa | |
AFRO TD Chad 1284000 10329208 World;Africa;Central Africa | |
AMRO CL Chile 756950 18729160 World;South America | |
WPRO CN China (PRC) 9596961 1427647786 World;Asia | |
AMRO ?? Clipperton Island (France) 6 0 World;North America | |
AMRO CO Colombia 1141748 49661048 World;South America | |
AFRO KM Comoros 2170 752438 World;Africa;East Africa | |
WPRO CK Cook Islands (New Zealand) 240 17518 World;Oceania;Polynesia | |
WPRO ?? Coral Sea Islands (Australia) 10 4 World;Oceania;Australasia | |
AMRO CR Costa Rica 51100 4999441 World;North America | |
EURO HR Croatia 56542 4284889 World;Europe;EU | |
AMRO CU Cuba 109886 11338134 World;North America | |
AMRO CW Curaçao (Kingdom of the Netherlands) 444 162752 World;North America | |
EURO CY Cyprus 9251 1189265 World;Europe;EU | |
EURO CZ Czech Republic 78866 10610947 World;Europe;EU | |
AFRO CD Democratic Republic of the Congo 2345410 69575000 World;Africa;Central Africa | |
EURO DK Denmark 43094 5748796 World;Europe;EU | |
SHIP Diamond Princess 0.1 3711 World;Ships | |
EMRO DJ Djibouti 23000 828324 World;Africa;East Africa | |
AMRO DM Dominica 751 71625 World;North America | |
AMRO DO Dominican Republic 48671 10627141 World;North America | |
SEARO TL East Timor 14874 1267974 World;Asia;NonCKIR | |
WPRO ?? Easter Island (Chile) 164 5761 World;Oceania;Polynesia | |
AMRO EC Ecuador 283560 17084358 World;South America | |
EMRO EG Egypt 1001450 82868000 World;Africa;North Africa | |
AMRO SV El Salvador 21041 6420746 World;North America | |
AFRO GQ Equatorial Guinea 28051 633441 World;Africa;Central Africa | |
AFRO ER Eritrea 121320 5647168 World;Africa;East Africa | |
EURO EE Estonia 45226 1319133 World;Europe;EU | |
AFRO SZ Eswatini 17363 1123913 World;Africa;Southern Africa | |
AFRO ET Ethiopia 1127127 84320987 World;Africa;East Africa | |
AMRO FK Falkland Islands (United Kingdom) 12173 3234 World;South America | |
AMRO ?? Federal Dependencies of Venezuela (Venezuela) 342 2155 World;North America | |
WPRO FM Federated States of Micronesia 702 112640 World;Oceania;Micronesia | |
WPRO FJ Fiji 18270 883483 World;Oceania;Melanesia | |
EURO FI Finland 336593 5509717 World;Europe;EU | |
EURO FR France 547030 67348000 World;Europe;EU | |
AMRO GF French Guiana (France) 91000 282938 World;South America | |
WPRO PF French Polynesia (France) 4167 277679 World;Oceania;Polynesia | |
AFRO ?? French Southern and Antarctic Lands (France) 439781 100 World;Africa;East Africa | |
AFRO GA Gabon 267667 1514993 World;Africa;Central Africa | |
EURO GE Georgia 69700 3723464 World;Europe;NonEU | |
EURO DE Germany 357168 82800000 World;Europe;EU | |
AFRO GH Ghana 239460 23832495 World;Africa;West Africa | |
EURO GR Greece 131957 10768477 World;Europe;EU | |
EURO GL Greenland (Kingdom of Denmark) 2166086 56564 World;North America | |
AMRO GD Grenada 344 111454 World;North America | |
AMRO GP Guadeloupe (France) 1628 399848 World;North America | |
WPRO GU Guam (United States) 549 165768 World;Oceania;Micronesia | |
AMRO GT Guatemala 108889 17247849 World;North America | |
AFRO GN Guinea 245857 10057975 World;Africa;West Africa | |
AFRO GW Guinea-Bissau 36120 1533964 World;Africa;West Africa | |
AMRO GY Guyana 214999 779006 World;South America | |
AMRO HT Haiti 27750 11123178 World;North America | |
WPRO ?? Hawaii (United States) 16636 1360301 World;Oceania;Polynesia | |
AMRO HN Honduras 112492 9587522 World;North America | |
EURO HU Hungary 93030 9797561 World;Europe;EU | |
EURO IS Iceland 103000 350710 World;Europe;NonEU | |
SEARO IN India 3287263 1352642280 World;Asia;NonCKIR | |
SEARO ID Indonesia 1904569 267670543 World;Asia;NonCKIR | |
EMRO IR Iran 1648195 81800188 World;Asia | |
EMRO IQ Iraq 438317 38433600 World;Asia;NonCKIR | |
EURO IE Ireland 70280 4761865 World;Europe;EU | |
EURO IL Israel 20770 8381516 World;Asia;NonCKIR | |
EURO IT Italy 301338 60589445 World;Europe;EU | |
AFRO CI Ivory Coast 322460 20617068 World;Africa;West Africa | |
AMRO JM Jamaica 10991 2934847 World;North America | |
WPRO JP Japan 377915 127202192 World;Asia;NonCKIR | |
EMRO JO Jordan 89342 9965318 World;Asia;NonCKIR | |
EURO KZ Kazakhstan 2724900 18448600 World;Asia;NonCKIR | |
AFRO KE Kenya 582650 39002772 World;Africa;East Africa | |
WPRO KI Kiribati 811 115847 World;Oceania;Micronesia | |
EURO XK Kosovo 10887 1810463 World;Europe;NonEU | |
EMRO KW Kuwait 17818 4137312 World;Asia;NonCKIR | |
EURO KG Kyrgyzstan 199951 6304030 World;Asia;NonCKIR | |
WPRO LA Laos 236800 7061507 World;Asia;NonCKIR | |
EURO LV Latvia 64589 1925800 World;Europe;EU | |
EMRO LB Lebanon 10400 6859408 World;Asia;NonCKIR | |
AFRO LS Lesotho 30355 2130819 World;Africa;Southern Africa | |
AFRO LR Liberia 111370 3441790 World;Africa;West Africa | |
EMRO LY Libya 1759540 6310434 World;Africa;North Africa | |
EURO LI Liechtenstein 160 38111 World;Europe;NonEU | |
EURO LT Lithuania 65300 2800667 World;Europe;EU | |
EURO LU Luxembourg 2586 602005 World;Europe;EU | |
SHIPS MS Zaandam 0.08 1829 World;Ships | |
AFRO MG Madagascar 587040 20653556 World;Africa;East Africa | |
AFRO ?? Madeira (Portugal) 797 245000 World;Africa;North Africa | |
AFRO MW Malawi 118480 14268711 World;Africa;East Africa | |
WPRO MY Malaysia 329847 31528033 World;Asia;NonCKIR | |
SEARO MV Maldives 298 515696 World;Asia;NonCKIR | |
AFRO ML Mali 1240000 12666987 World;Africa;West Africa | |
EURO MT Malta 316 445426 World;Europe;EU | |
WPRO MH Marshall Islands 181 58413 World;Oceania;Micronesia | |
AMRO MQ Martinique (France) 1128 375673 World;North America | |
AFRO MR Mauritania 1030700 3129486 World;Africa;West Africa | |
AFRO MU Mauritius 2040 1284264 World;Africa;East Africa | |
AFRO YT Mayotte (France) 374 223765 World;Africa;East Africa | |
AFRO ?? Melilla (Spain) 12 85116 World;Africa;North Africa | |
AMRO MX Mexico 1964375 126190788 World;North America | |
EURO MD Moldova 33846 4434547 World;Europe;NonEU | |
EURO MC Monaco 2.02 38400 World;Europe;NonEU | |
WPRO MN Mongolia 1564116 3170216 World;Asia;NonCKIR | |
EURO ME Montenegro 13812 642550 World;Europe;NonEU | |
AMRO MS Montserrat (United Kingdom) 102 4993 World;North America | |
EMRO MA Morocco 446550 35740000 World;Africa;North Africa | |
AFRO MZ Mozambique 801590 21669278 World;Africa;East Africa | |
SEARO MM Myanmar 676578 53708320 World;Asia;NonCKIR | |
AFRO NA Namibia 825418 2108665 World;Africa;Southern Africa | |
WPRO NR Nauru 21 10670 World;Oceania;Micronesia | |
SEARO NP Nepal 147181 28095714 World;Asia;NonCKIR | |
EURO NL Netherlands 41543 17271990 World;Europe;EU | |
WPRO NC New Caledonia (France) 19060 279993 World;Oceania;Melanesia | |
WPRO NZ New Zealand 268680 4743131 World;Oceania;Australasia | |
AMRO NI Nicaragua 130373 6465501 World;North America | |
AFRO NE Niger 1267000 15306252 World;Africa;West Africa | |
AFRO NG Nigeria 923768 166629000 World;Africa;West Africa | |
WPRO NU Niue (New Zealand) 260 1620 World;Oceania;Polynesia | |
WPRO NF Norfolk Island (Australia) 35 2302 World;Oceania;Australasia | |
SEARO KP North Korea 120538 25549604 World;Asia;NonCKIR | |
EURO MK North Macedonia 25713 2103721 World;Europe;NonEU | |
WPRO MP Northern Mariana Islands (United States) 477 56882 World;Oceania;Micronesia | |
EURO NO Norway 385203 5295619 World;Europe;NonEU | |
AMRO ?? Nueva Esparta (Venezuela) 1151 491610 World;North America | |
EMRO OM Oman 309500 4829473 World;Asia;NonCKIR | |
EMRO PK Pakistan 881913 211103000 World;Asia;NonCKIR | |
WPRO PW Palau 458 17907 World;Oceania;Micronesia | |
EMRO PS Palestine 6220 4862979 World;Asia;NonCKIR | |
AMRO PA Panama 75417 4176869 World;North America | |
WPRO ?? Papua (Indonesia) 319036 3486432 World;Oceania;Melanesia | |
WPRO PG Papua New Guinea 462840 8606323 World;Oceania;Melanesia | |
AMRO PY Paraguay 406750 6956066 World;South America | |
AMRO PE Peru 1285220 31989260 World;South America | |
WPRO PH Philippines 343448 106651394 World;Asia;NonCKIR | |
WPRO PN Pitcairn Islands (United Kingdom) 47 47 World;Oceania;Polynesia | |
EURO PL Poland 312685 38422346 World;Europe;EU | |
EURO PT Portugal 92212 10379537 World;Europe;EU | |
AMRO PR Puerto Rico (United States) 8870 3039596 World;North America | |
EMRO QA Qatar 11586 2781682 World;Asia;NonCKIR | |
AFRO CG Republic of the Congo 342000 4012809 World;Africa;Central Africa | |
EURO RO Romania 238397 19638000 World;Europe;EU | |
EURO RU Russia 17098246 146745098 World;Europe;NonEU | |
AFRO RW Rwanda 26338 10473282 World;Africa;East Africa | |
AFRO RE Réunion (France) 2512 743981 World;Africa;East Africa | |
AMRO BQ Saba (Kingdom of the Netherlands) 13 1537 World;North America | |
AMRO BL Saint Barthélemy (France) 21 7448 World;North America | |
AFRO SH Saint Helena, Ascension and Tristan da Cunha (United Kingdom) 420 7728 World;Africa;West Africa | |
AMRO KN Saint Kitts and Nevis 261 52441 World;North America | |
AMRO LC Saint Lucia 539 181889 World;North America | |
AMRO MF Saint Martin (France) 54 29820 World;North America | |
AMRO PM Saint Pierre and Miquelon (France) 242 5849 World;North America | |
AMRO VC Saint Vincent and the Grenadines 389 110211 World;North America | |
WPRO WS Samoa 2944 196129 World;Oceania;Polynesia | |
AMRO ?? San Andrés and Providencia (Colombia) 53 77701 World;North America | |
EURO SM San Marino 61.2 33285 World;Europe;NonEU | |
EMRO SA Saudi Arabia 2149690 33702756 World;Asia;NonCKIR | |
AFRO SN Senegal 196190 13711597 World;Africa;West Africa | |
EURO RS Serbia 88361 7040272 World;Europe;NonEU | |
AFRO SC Seychelles 455 87476 World;Africa;East Africa | |
AFRO SL Sierra Leone 71740 6440053 World;Africa;West Africa | |
WPRO SG Singapore 697 5757499 World;Asia;NonCKIR | |
AMRO BQ Sint Eustatius (Kingdom of the Netherlands) 21 2739 World;North America | |
AMRO SX Sint Maarten (Kingdom of the Netherlands) 34 41940 World;North America | |
EURO SK Slovakia 49035 5435343 World;Europe;EU | |
EURO SI Slovenia 20273 2066880 World;Europe;EU | |
WPRO SB Solomon Islands 28450 652857 World;Oceania;Melanesia | |
EMRO SO Somalia 637657 9832017 World;Africa;East Africa | |
AFRO ZA South Africa 1219912 51770560 World;Africa;Southern Africa | |
AMRO ?? South Georgia and the South Sandwich Islands (United Kingdom) 3093 20 World;South America | |
WPRO KR South Korea 100210 51171706 World;Asia | |
AFRO SS South Sudan 619745 8260490 World;Africa;East Africa | |
EURO ES Spain 505990 46698151 World;Europe;EU | |
SEARO LK Sri Lanka 65610 21228763 World;Asia;NonCKIR | |
EMRO SD Sudan 1861484 30894000 World;Africa;North Africa | |
AMRO SR Suriname 163270 575990 World;South America | |
EURO SE Sweden 450295 10151588 World;Europe;EU | |
EURO CH Switzerland 41285 8401120 World;Europe;NonEU | |
EMRO SY Syria 185180 16945057 World;Asia;NonCKIR | |
AFRO ST São Tomé and Príncipe 1001 212679 World;Africa;Central Africa | |
WPRO ?? Taiwan (ROC) 36193 23726460 World;Asia;NonCKIR | |
EURO TJ Tajikistan 143100 9100835 World;Asia;NonCKIR | |
AFRO TZ Tanzania 945087 44929002 World;Africa;East Africa | |
SEARO TH Thailand 513120 69428453 World;Asia;NonCKIR | |
AMRO BS The Bahamas 13943 385637 World;North America | |
AFRO GM The Gambia 11300 1782893 World;Africa;West Africa | |
AFRO TG Togo 56785 6019877 World;Africa;West Africa | |
WPRO TK Tokelau (New Zealand) 10 1319 World;Oceania;Polynesia | |
WPRO TO Tonga 748 103197 World;Oceania;Polynesia | |
AMRO TT Trinidad and Tobago 5130 1389843 World;North America | |
EMRO TN Tunisia 163610 10486339 World;Africa;North Africa | |
EURO TR Turkey 783356 83154997 World;Europe;NonEU | |
EURO TM Turkmenistan 488100 5850901 World;Asia;NonCKIR | |
AMRO TC Turks and Caicos Islands (United Kingdom) 948 37665 World;North America | |
WPRO TV Tuvalu 26 11508 World;Oceania;Polynesia | |
AFRO UG Uganda 236040 32369558 World;Africa;East Africa | |
EURO UA Ukraine 603628 42418235 World;Europe;NonEU | |
EMRO AE United Arab Emirates 83600 9630959 World;Asia;NonCKIR | |
EURO GB United Kingdom 244820 66040229 World;Europe;NonEU | |
AMRO US United States 9629091 327096265 World;North America | |
AMRO VI United States Virgin Islands (United States) 347 104680 World;North America | |
AMRO UY Uruguay 176220 3449285 World;South America | |
EURO UZ Uzbekistan 447400 32476244 World;Asia;NonCKIR | |
WPRO VU Vanuatu 12200 292680 World;Oceania;Melanesia | |
EURO VA Vatican City 0.44 1000 World;Europe;NonEU | |
AMRO VE Venezuela 916445 28887118 World;South America | |
WPRO VN Vietnam 331212 95545962 World;Asia;NonCKIR | |
WPRO WK Wake Island (United States) 2 150 World;Oceania;Micronesia | |
WPRO WF Wallis and Futuna (France) 274 11661 World;Oceania;Polynesia | |
WPRO ?? West Papua (Indonesia) 140375 760855 World;Oceania;Melanesia | |
AFRO EH Western Sahara 266000 405210 World;Africa;North Africa | |
EMRO YE Yemen 527968 28498683 World;Asia;NonCKIR | |
AFRO ZM Zambia 752614 11862740 World;Africa;Southern Africa | |
AFRO ZW Zimbabwe 390580 11392629 World;Africa;Southern Africa | |
WPRO CC Cocos (Keeling) Islands 14 544 World;Oceania;Australasia | |
WPRO CX Christmas Island 135 1843 World;Oceania;Australasia | |
EURO FO Faroe Islands 1399 52110 World;Europe | |
EURO GG Guernsey 65 62792 World;Europe | |
EURO GI Gibraltar 6.7 34003 World;Europe | |
EURO IM Isle of Man 572 83314 World;Europe | |
EURO JE Jersey 118.2 107800 World;Europe | |
EURO SJ Svalbard and Jan Mayen Islands 61022 2939 World;Europe | |
AMRO AN Netherlands Antilles 999 304759 World;North America | |
WPRO JT Johnston Atoll 1 0 World;Oceania;Australasia | |
WPRO UM Midway Islands 66 40 World;Oceania;Australasia |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Show dutch rivm corona statistics | |
Author: Willem Hengeveld <[email protected]> | |
""" | |
from collections import defaultdict | |
import urllib.request | |
import urllib.parse | |
import json | |
import os | |
import os.path | |
import time | |
import hashlib | |
x = dict() | |
class WebCache: | |
def __init__(self): | |
self.cachedir = os.path.join(os.getenv("HOME"), ".pywebcache") | |
if not os.path.isdir(self.cachedir): | |
os.mkdir(self.cachedir) | |
def calccachepath(self, url): | |
urlhash = hashlib.sha1(url.encode('utf-8')).hexdigest() | |
return os.path.join(self.cachedir, urlhash) | |
def read(self, url): | |
cachefile = self.calccachepath(url) | |
try: | |
st = os.stat(cachefile) | |
if time.time() - st.st_mtime > 86400/2: | |
return | |
if st.st_size==0: | |
return | |
with open(cachefile, "rb") as fh: | |
return fh.read() | |
except: | |
return | |
def write(self, url, text): | |
cachefile = self.calccachepath(url) | |
with open(cachefile, "wb") as fh: | |
fh.write(text) | |
webcache = WebCache() | |
def loadjson(url): | |
global webcache | |
try: | |
text = webcache.read(url) | |
if text: | |
return json.loads(text) | |
except: | |
pass | |
with urllib.request.urlopen(url) as response: | |
text = response.read() | |
webcache.write(url, text) | |
return json.loads(text) | |
def converttables(): | |
def cv(x): return x[2:10] | |
global x | |
bydatebycity = defaultdict(dict) | |
cities = dict() | |
country = defaultdict(lambda:defaultdict(int)) | |
for item in x["gem"]: | |
t = cv(item["Date_of_report"]) | |
if item["Municipality_code"] is None: | |
bydatebycity[t][item["Province"]] = item | |
cities[item["Province"]] = item["Province"] | |
else: | |
bydatebycity[t][item["Municipality_code"]] = item | |
cities[item["Municipality_code"]] = item["Municipality_name"] | |
for key in ("Deceased", "Hospital_admission", "Total_reported"): | |
country[t][key] += item[key] | |
x["bycity"] =bydatebycity | |
x["city"] = cities | |
x["country"] = country | |
if "rval" in x: | |
rvalbydate = dict() | |
for item in x["rval"]: | |
rvalbydate[item["Date"]] = item | |
x["rbydate"] = rvalbydate | |
def showtables(dates, times, base, index, totals, key): | |
(w0,wp,w1,w2) = times | |
print("%-30s %8s" % ("", ""), end=" ") | |
for t in dates[:-w1:wp]: | |
print("%10s" % t, end=" ") | |
print() | |
for gemid, naam in index.items(): | |
t0 = dates[w0] | |
t1 = dates[w1] | |
t2 = dates[w2] | |
x0 = base[t0][gemid][key] | |
x1 = base[t1][gemid][key] | |
x2 = base[t2][gemid][key] | |
groei = 100*(x0-x1)/(x0-x2) if x0-x2 else 0 | |
print("%-30s %7.2f" % (naam, groei), end=" ") | |
for t0, t1 in zip(dates[::wp], dates[w1::wp]): | |
x0 = base[t0][gemid][key] | |
x1 = base[t1][gemid][key] | |
print("%10s" % (x0-x1), end=" ") | |
print() | |
print("--------") | |
t0 = dates[w0] | |
t1 = dates[w1] | |
t2 = dates[w2] | |
x0 = totals[t0][key] | |
x1 = totals[t1][key] | |
x2 = totals[t2][key] | |
groei = 100*(x0-x1)/(x0-x2) if x0-x2 else 0 | |
print("%-30s %7.2f" % ("Nederland", groei), end=" ") | |
for t0, t1 in zip(dates[::wp], dates[w1::wp]): | |
x0 = totals[t0][key] | |
x1 = totals[t1][key] | |
print("%10s" % (x0-x1), end=" ") | |
print() | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser(description='get rivm statistics') | |
parser.add_argument('--week', '-w', action='store_true', help='summarize per week') | |
parser.add_argument('--twoweeks', '-t', action='store_true', help='summarize per two weeks') | |
parser.add_argument('--sumdays', '-S', type=int, help='summarize per two weeks') | |
parser.add_argument('--printdays', '-P', type=int, help='output value every N days') | |
parser.add_argument('--delta', '-d', type=int, help='relative growth period', default=4) | |
args = parser.parse_args() | |
if args.week: | |
args.sumdays = 7 | |
args.printdays = 7 | |
elif args.twoweeks: | |
args.sumdays = 14 | |
args.printdays = 7 | |
else: | |
if args.sumdays is None: | |
args.sumdays = 1 | |
if args.printdays is None: | |
args.printdays = 1 | |
x["gem"] = loadjson("https://data.rivm.nl/covid-19/COVID-19_aantallen_gemeente_cumulatief.json") | |
#x["rval"] = loadjson("https://data.rivm.nl/covid-19/COVID-19_reproductiegetal.json") | |
converttables() | |
dates = list(sorted(x["bycity"].keys())) | |
dates = dates[::-1] | |
w0, wp, w1, w2 = 0, args.printdays, args.sumdays, args.delta*args.sumdays | |
for key in ("Deceased", "Hospital_admission", "Total_reported"): | |
print("==== %s" % key) | |
showtables(dates, (w0,wp,w1,w2), x["bycity"], x["city"], x["country"], key) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Show WHO corona statistics | |
Author: Willem Hengeveld <[email protected]> | |
""" | |
from collections import defaultdict | |
import urllib.request | |
import urllib.parse | |
import json | |
import datetime | |
import os | |
import os.path | |
import time | |
import hashlib | |
def readcountries(): | |
class Country: | |
def __init__(self, region, name, iso2, area, pop, groups): | |
self.region = region | |
self.name = name | |
self.iso2 = iso2 | |
self.area = float(area) | |
self.population = int(population) | |
self.groups = groups | |
# returns a dict of country => list of groups | |
with open("countries.txt", "r") as fh: | |
data = fh.read() | |
g = dict() | |
for line in data.split("\n"): | |
if not line: | |
continue | |
region, iso2, country, area, population, grouping = line.split("\t") | |
groups = grouping.split(";") | |
g[iso2] = Country(region, country, iso2, area, population, groups) | |
return g | |
class WebCache: | |
def __init__(self): | |
self.cachedir = os.path.join(os.getenv("HOME"), ".pywebcache") | |
if not os.path.isdir(self.cachedir): | |
os.mkdir(self.cachedir) | |
def calccachepath(self, url): | |
urlhash = hashlib.sha1(url.encode('utf-8')).hexdigest() | |
return os.path.join(self.cachedir, urlhash) | |
def read(self, url): | |
cachefile = self.calccachepath(url) | |
try: | |
st = os.stat(cachefile) | |
if time.time() - st.st_mtime > 3600: | |
return | |
if st.st_size==0: | |
return | |
with open(cachefile, "rb") as fh: | |
return fh.read() | |
except: | |
return | |
def write(self, url, text): | |
cachefile = self.calccachepath(url) | |
with open(cachefile, "wb") as fh: | |
fh.write(text) | |
webcache = WebCache() | |
def loadjson(url): | |
global webcache | |
try: | |
text = webcache.read(url) | |
if text: | |
return json.loads(text) | |
except: | |
pass | |
with urllib.request.urlopen(url) as response: | |
text = response.read() | |
webcache.write(url, text) | |
return json.loads(text) | |
def getitembymember(a, member): | |
""" | |
Get the first item from 'a' which has an element named 'member' | |
""" | |
for item in a: | |
if member in item: | |
return item | |
def getitem(d, *path): | |
""" | |
Traverse a nested python object, path items select which object is selected: | |
* a tuple: selects a dictionary from a list which contains the specified key | |
* an integer: select the specified item from a list. | |
* a string: select the specified item from a dictionary. | |
""" | |
for k in path: | |
if type(k) == tuple: | |
d = getitembymember(d, *k) | |
elif type(k) == int: | |
d = d[k] | |
else: | |
d = d.get(k) | |
if d is None: | |
return | |
return d | |
def cvt(t): | |
t = datetime.datetime.fromtimestamp(t/1000) | |
return "%02d-%02d" % (t.month, t.day) | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser(description='get who statistics') | |
parser.add_argument('--week', '-w', action='store_true', help='summarize per week') | |
parser.add_argument('--twoweeks', '-t', action='store_true', help='summarize per two weeks') | |
parser.add_argument('--sumdays', '-S', type=int, help='summarize per two weeks') | |
parser.add_argument('--printdays', '-P', type=int, help='output value every N days') | |
parser.add_argument('--delta', '-d', type=int, help='relative growth period', default=4) | |
parser.add_argument('--bypopulation', '-p', action='store_true', help='show by 100000 people') | |
parser.add_argument('--byarea', '-a', action='store_true', help='show by km2') | |
parser.add_argument('--yesterday', '-y', action='store_true', help='use data from yesterday') | |
args = parser.parse_args() | |
if args.week: | |
args.sumdays = 7 | |
args.printdays = 7 | |
elif args.twoweeks: | |
args.sumdays = 14 | |
args.printdays = 7 | |
else: | |
if args.sumdays is None: | |
args.sumdays = 1 | |
if args.printdays is None: | |
args.printdays = 1 | |
x = loadjson("https://covid19.who.int/page-data/index/page-data.json") | |
#y = loadjson("https://covid19.who.int/geo/who-world-map-topo-split-1.json") | |
byday = getitem(x, "result", "pageContext", "rawDataSets", "dayGroups") | |
bycty = getitem(x, "result", "pageContext", "rawDataSets", "countryGroups") | |
table = dict() | |
dates = set() | |
cinfo = readcountries() | |
#codes = { "XK":{"name":"Kosovo", "region":"EURO"}, " ":{"name":"CruiseShips", "region":"SHIP"} } | |
#or item in getitem(y, "objects", "countries", "geometries"): | |
# prop = item.get("properties") | |
# cc = prop.get("ISO_2_CODE") | |
# info = cinfo.get(cc) | |
# if not info: | |
# print("no info for %s" % cc) | |
# info = Country(prop.get("ADM0_VIZ_N"), 0, 0, "World") | |
# ccodes[cc] = { | |
# "name":prop.get("ADM0_VIZ_N"), | |
# "region":prop.get("WHO_REGION") , | |
# "area":info.area, | |
# "population":info.population, | |
# } | |
for data in bycty: | |
name = getitem(data, "value") | |
d = getitem(data, "data", "dimensions") | |
m = getitem(data, "data", "metrics") | |
table[name] = c = dict() | |
for row in getitem(data, "data", "rows"): | |
t = cvt(row[0]) | |
delta = 0 | |
if name == 'GB' and t >= '07-03': | |
delta = 30302 | |
c[t] = { "deaths": row[3], "cases": row[8]+delta } | |
dates.add(t) | |
dates = list(sorted(dates)) | |
dates = dates[::-1] | |
if args.yesterday: | |
# skip the partially incomplete statistics from today | |
dates = dates[1:] | |
w0, wp, w1, w2 = 0, args.printdays, args.sumdays, args.delta*args.sumdays | |
for key in ("deaths", "cases"): | |
print("==== %s" % key) | |
print("%-6s%-30s %8s" % ("", "", ""), end=" ") | |
for t in dates[:-w1:wp]: | |
print("%10s" % t, end=" ") | |
print() | |
for cc, row in table.items(): | |
country = cinfo[cc] | |
if args.byarea: | |
factor = 1.0 / country.area if country.area else 0 | |
elif args.bypopulation: | |
factor = 1.0 / country.population * 100000 if country.population else 0 | |
else: | |
factor = None | |
dt = 0 ; mark = ' ' | |
t0 = dates[w0] ; x0 = getitem(row, t0, key) | |
if x0 is None: | |
dt = 1 | |
mark = '+' | |
t0 = dates[w0+dt] ; x0 = getitem(row, t0, key) | |
t1 = dates[w1+dt] ; x1 = getitem(row, t1, key) | |
t2 = dates[w2+dt] ; x2 = getitem(row, t2, key) | |
groei = 100*(x0-x1)/(x0-x2) if not (x0 is None or x1 is None or x2 is None) and x0-x2!=0 else 0 | |
#groei -= 100*(w1-w0)/(w2-w0) | |
print("%-5s:%-30s %7.2f%s" % (country.region, country.name[:30], groei, mark), end=" ") | |
for t0, t1 in zip(dates[dt::wp], dates[dt+w1::wp]): | |
x0 = getitem(row, t0, key) | |
x1 = getitem(row, t1, key) | |
if x0 is None or x1 is None: | |
print("%10s" % "-", end=" ") | |
elif factor is None: | |
print("%10s" % (x0-x1), end=" ") | |
else: | |
print("%10.4f" % ((x0-x1)*factor), end=" ") | |
print() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment