-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from kupad95/kupad95-patch-1
add India state-level data
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os, sys | ||
import csv | ||
import json | ||
import requests | ||
import numpy as np | ||
|
||
from collections import defaultdict | ||
from datetime import datetime | ||
from .utils import write_tsv | ||
|
||
# ------------------------------------------------------------------------ | ||
# Globals | ||
|
||
URL = "https://api.rootnet.in/covid19-in/stats/daily" | ||
LOC = "case-counts/Asia/Southern Asia/India" | ||
cols = ['time', 'cases', 'deaths', 'hospitalized', 'ICU', 'recovered'] | ||
|
||
# ------------------------------------------------------------------------ | ||
# Functions | ||
|
||
def sorted_date(s): | ||
return sorted(s, key=lambda d: datetime.strptime(d[cols.index('time')], "%Y-%m-%d")) | ||
|
||
# ------------------------------------------------------------------------ | ||
# Main point of entry | ||
|
||
def parse(): | ||
r = requests.get(URL) | ||
if not r.ok: | ||
print(f"Failed to fetch {URL}", file=sys.stderr) | ||
exit(1) | ||
r.close() | ||
|
||
dbindia = json.loads(r.text)['data'] | ||
r.close() | ||
|
||
# Convert to ready made TSVs | ||
states = defaultdict(list) | ||
for row in dbindia: | ||
dates = row["day"] | ||
for i in row['regional']: | ||
confirmedCases = i["confirmedCasesIndian"] + i["confirmedCasesForeign"] | ||
deaths = i["deaths"] | ||
locations = i['loc'] | ||
elt = [ dates, confirmedCases, deaths, None, None, None ] | ||
states[locations].append(elt) | ||
|
||
for cntry, data in states.items(): | ||
states[cntry] = sorted_date(states[cntry]) | ||
|
||
for state, data in states.items(): | ||
write_tsv(f"{LOC}/{state}.tsv", cols, data, "india") |
This file contains 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
This file contains 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