-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid.py
139 lines (122 loc) · 4.26 KB
/
covid.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
import json
import os
import sys
state = ""
dist = ""
def justIn():
print("Looks like you are new to this program. Please input your State and District data")
global state
state = input()
global dist
dist = input()
os.chdir("/tmp/")
try:
os.makedirs("pycovid")
except:
os.system("rm -rf /tmp/pycovid/")
os.chdir("/tmp")
os.makedirs("pycovid")
os.chdir("/tmp/pycovid/")
required_data = {"State": state, "District": dist}
fp = open("config.json",'w')
json.dump(required_data,fp)
fp.close()
def addAgain():
print("Looks like something went wrong with what you typed. Here are possible reasons:\n1. Keep spaces between state names.\n2. Please check if your locality is in the json files for local stats.")
os.system('rm -rf /tmp/pycovid')
print("Please enter your State and District again")
global state
state = input()
global dist
dist = input()
print(state, dist)
required_data = {"State": state, "District": dist}
os.chdir("/tmp/")
os.makedirs("pycovid")
os.chdir("/tmp/pycovid/")
fp = open("config.json",'w')
json.dump(required_data,fp)
fp.close()
def getStats(jsonFile, state, dist):
# try to open the json file, or download
try:
fp = open(jsonFile,'rb')
except:
if jsonFile == "state_district_wise.json":
os.system('wget --quiet https://api.covid19india.org/state_district_wise.json' )
elif jsonFile == "latest":
os.system('wget -O latest --quiet https://api.rootnet.in/covid19-in/stats/latest')
fp = open(jsonFile, 'rb')
local = json.loads(fp.read())
# delet old json files
try:
local['lastRefreshed'][:8] == (os.popen('date +%Y-%m-%d').readlines()[0].strip())
except:
fp.close()
try:
os.system('rm '+jsonFile)
except:
pass
os.system('wget --quiet https://api.covid19india.org/state_district_wise.json')
os.system('wget -O latest --quiet https://api.rootnet.in/covid19-in/stats/latest')
local = json.loads(open(jsonFile,'rb').read())
# save downloaded file for offline access
if jsonFile == "state_district_wise.json":
curr_date = os.popen('date +%Y-%m-%d').readlines()[0].strip()
local['lastRefreshed'] = curr_date
os.system('rm ' + jsonFile)
fp = open(jsonFile, 'w')
json.dump(local,fp)
fp.close()
if jsonFile == 'state_district_wise.json':
active = "NA"
try:
confirmed = local[state]['districtData'][dist]['confirmed']
except:
addAgain()
print("Please run the program again.")
sys.exit(0)
deceased = "NA"
if jsonFile == 'latest':
key = 0
for i in range(33):
if local['data']['regional'][i]['loc'] == state:
key = i
# api seems to think "Telangana" is spelled "Telengana"
if state == "Telangana":
key = 28
try:
confirmed = local['data']['regional'][i]['totalConfirmed']
active = confirmed - local['data']['regional'][i]['discharged']
deceased = local['data']['regional'][i]['deaths']
except:
addAgain()
print("Please run the program again")
sys.exit(0)
if jsonFile == 'latest' and state == "NA":
confirmed = local['data']['summary']['total']
active = confirmed - local['data']['summary']['discharged']
deceased = local['data']['summary']['deaths']
return active, confirmed, deceased
#get the local casefile from https://api.covid19india.org/state_district_wise.json
def main():
# Country and State data from https://api.rootnet.in/
try:
os.chdir("/tmp/pycovid/")
configs = json.loads(open("config.json",'rb').read())
except:
justIn()
configs = json.loads(open("config.json",'rb').read())
global state
state = configs['State']
global dist
dist = configs['District']
print("Coronavirus Statistics".center(80, '-'))
active, confirmed, deceased = getStats('latest', "NA", "NA")
print("Country Stats: ".ljust(20) + ("Confirmed: " + str(confirmed)).ljust(20) + (" Active: " + str(active)).ljust(20) + (" Deceased: " + str(deceased)).ljust(20) )
active, confirmed, deceased = getStats('latest', state, 'NA')
print("State Stats: ".ljust(20) + ("Confirmed: " + str(confirmed)).ljust(20) + (" Active: " + str(active)).ljust(20) + (" Deceased: " + str(deceased)).ljust(20) )
active, confirmed, deceased = getStats('state_district_wise.json', state, dist)
print("Local Stats: ".ljust(20) + ("Confirmed: " + str(confirmed)).ljust(20) + (" Active: " + str(active)).ljust(20) + (" Deceased: " + str(deceased)).ljust(20) )
if __name__ == "__main__":
main()