Last active
June 22, 2021 23:56
-
-
Save phiekl/47e09fe16cd531333cf4164117694170 to your computer and use it in GitHub Desktop.
./swecovid.py -p 7,14,21
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
| #!/usr/bin/python3 | |
| import argparse | |
| import json | |
| import requests | |
| import datetime | |
| def arg_parse(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "-p", | |
| "--period", | |
| default="14", | |
| help="How many days to use for average count (comma separated for multiple).", | |
| ) | |
| return parser.parse_args() | |
| def fetch(url): | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" | |
| } | |
| return requests.get(url, headers=headers) | |
| if __name__ == "__main__": | |
| opts = arg_parse() | |
| url = "https://services5.arcgis.com/fsYDFeRKu1hELJJs/arcgis/rest/services/FOHM_Covid_19_FME_1/FeatureServer/1/query?f=geojson&where=1%3D1&outFields=*&orderByFields=Statistikdatum" | |
| data = json.loads(requests.get(url).text) | |
| population = 10343403 | |
| periods = [int(x) for x in opts.period.split(",")] | |
| last = [] | |
| for ent in data["features"]: | |
| count = ent["properties"]["Totalt_antal_fall"] | |
| last.append(count) | |
| avgs = [] | |
| for period in periods: | |
| if len(last) < period: | |
| period = len(last) | |
| avg = int(round(sum(last[-period:]) / period, 0)) | |
| avgs.append("%7s" % ("%s=%s" % (period, avg))) | |
| pcapita = int(round(sum(last[-14:]) / population * 100000, 0)) | |
| ts = int(ent["properties"]["Statistikdatum"] / 1000) | |
| ts = datetime.datetime.utcfromtimestamp(ts).strftime("%Y-%m-%d") | |
| print("%s %7s %8s %s" % (ts, "n=%s" % count, "pc=%s" % pcapita, " ".join(avgs))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment