-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndian_cities.py
156 lines (110 loc) · 5.1 KB
/
Indian_cities.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import requests
import csv
import json
import pandas as pd
from pandas import DataFrame as df
from nameCorrection import name_correction
import os
JSON_URL = 'https://api.covid19india.org/v4/min/timeseries-MH.min.json'
URL = 'https://api.covid19india.org/v4/min/timeseries-'
EXT = '.min.json'
JSON_INDIA = 'https://api.covid19india.org/v4/min/timeseries.min.json'
state_name = []
req_date = requests.get(JSON_URL)
req_India = requests.get(JSON_INDIA)
stateNames = df(req_India.json())
statesData = stateNames.T
for st in statesData.index:
if not 'UN' in st:
state_name.append(st)
unwanted_st = {'UN', 'TT', 'Other State', 'Other Region', 'Other', 'Unknown'}
state_name = [ele for ele in state_name if ele not in unwanted_st]
dtcolNames =[]
covidData =[]
covidRec = []
covidDeath = []
# date_ = df(req_India.json()['TT'])
# for dt in date_.index:
# dtcolNames.append(dt + ",")
dates = df(req_date.json()['MH']['districts']['Mumbai'])
for dt in dates.index:
dtcolNames.append(dt + ",")
isdir = os.path.isdir('Indian_Cities_Combined')
if not isdir:
os.mkdir('Indian_Cities_Combined')
for st in state_name:
st_name = name_correction(st)
#print ('statename: ' + st)
dist_name = []
JSON_URL = URL + st + EXT
req = requests.get(JSON_URL)
distNames = df(req.json())
if 'districts' in distNames.index:
distNames = df(req.json()[st]['districts'])
distNames = distNames.T
for dis in distNames.index:
#print (st + " : " + dis)
dist_name.append(dis)
# items to be removed
unwanted_elem = {'Foreign Evacuees', 'Other State', 'Other Region', 'Others', 'Other', 'Unknown'}
dist_name = [ele for ele in dist_name if ele not in unwanted_elem]
for dist in dist_name:
i=0
covidData.append('\n')
covidData.append(st_name + "," + st + "," + dist + ",")
covidRec.append('\n')
covidRec.append(st_name + "," + st + "," + dist + ",")
covidDeath.append('\n')
covidDeath.append(st_name + "," + st + "," + dist + ",")
covid = df(req.json()[st]['districts'][dist])
for conf, dt in zip(covid.dates, covid.index):
dt = dt + ","
i+=1
index = dtcolNames.index(dt) + 1
if i!= index:
#print(st + ":" + dist + ":" + dt + ": Ind: " + str(index) + ":" + "i :" + str(i))
if i == 1:
for n in range(index-1):
covidData.append("0,")
covidRec.append("0,")
covidDeath.append("0,")
else:
for n in range(index-i+1):
covidData.append("0,")
covidRec.append("0,")
covidDeath.append("0,")
i = index
for t in conf.keys():
if 'total' in t:
if 'confirmed' in (conf['total'].keys()):
covidData.append(str(conf['total']['confirmed']) + ",")
if not 'confirmed' in (conf['total'].keys()):
covidData.append("0,")
if 'recovered' in (conf['total'].keys()):
covidRec.append(str(conf['total']['recovered']) + ",")
if not 'recovered' in (conf['total'].keys()):
covidRec.append("0,")
if 'deceased' in (conf['total'].keys()):
covidDeath.append(str(conf['total']['deceased']) + ",")
if not 'deceased' in (conf['total'].keys()):
covidDeath.append("0,")
if dt != dtcolNames[len(dtcolNames)-1]:
diff = len(dtcolNames) - dtcolNames.index(dt)
for n in range(diff):
covidData.append(covidData[len(covidData)-1])
covidDeath.append(covidDeath[len(covidRec)-1])
covidRec.append(covidRec[len(covidRec)-1])
else:
print("State without sistrict's data : "+ st)
with open('Indian_Cities_Combined/Indian_Cities_total_confirmed_cases.csv', 'w') as f:
f.writelines("State, State Code, Cities,")
f.writelines(dtcolNames)
f.writelines(covidData)
with open('Indian_Cities_Combined/Indian_Cities_total_recovered_cases.csv', 'w') as f:
f.writelines("State, State Code, Cities,")
f.writelines(dtcolNames)
f.writelines(covidRec)
with open('Indian_Cities_Combined/Indian_Cities_total_Death_toll.csv', 'w') as f:
f.writelines("State, State Code, Cities,")
f.writelines(dtcolNames)
f.writelines(covidDeath)