/
COVID-19_India_New_API.py
185 lines (138 loc) · 8.9 KB
/
COVID-19_India_New_API.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'''Using the COVID-19 India API - https://api.covid19india.org/v3/min/data.min.json and fetching the required Data.'''
try:
import os
import bs4 as bs
import csv
import requests
import json
from prettytable import PrettyTable
#import Slack_Push_Notification as Slack
#COVID-19 API - Has all India's Data
url = "https://api.covid19india.org/v3/min/data.min.json"
r = requests.get(url).text
#Slack Notification ON - 1; OFF - 0
slack_notify = 0
#This Dictionary stores the data of all the dates till now as
d = json.loads(r)
#Dictionary to store the State Codes(Used in the API) and State Names
#UN - UnAssigned and TT - Total India Cases are NOT present in this dictionary
state_code = {'AN': 'Andaman and Nicobar', 'AP': 'Andhra Pradesh', 'AR': 'Arunachal Pradesh', 'AS': 'Assam', 'BR': 'Bihar',
'CH': 'Chandigarh', 'CT': 'Chattisgarh', 'DL': 'Delhi', 'DN': 'Dadra and Nagar Haveli', 'GA': 'Goa', 'GJ': 'Gujarat',
'HP': 'Himachal Pradesh', 'HR': 'Haryana', 'JH': 'Jharkhand', 'JK': 'Jammu and Kashmir', 'KA': 'Karnataka', 'KL': 'Kerala',
'LA': 'Ladakh', 'LD': 'Lakshadweep', 'MH': 'Maharashtra', 'ML': 'Meghalaya', 'MN': 'Manipur', 'MP': 'Madhya Pradesh',
'MZ': 'Mizoram', 'NL': 'Nagaland', 'OR': 'Odisha', 'PB': 'Punjab', 'PY': 'Puducherry', 'RJ': 'Rajasthan', 'SK': 'Sikkim',
'TG': 'Telangana', 'TN': 'Tamil Nadu', 'TR': 'Tripura', 'UP': 'Uttar Pradesh', 'UT': 'Uttarakhand', 'WB': 'West Bengal'}
except Exception as e:
print(f'EXCEPTION in Importing Modules: {e}')
def getIndiaData():
try:
'''Prints the statewise details of Corona Cases across India.'''
head = ['State/UT', 'Total', 'Active', 'Recovered', 'Deceased', 'Tests Done']
ind_state_table = PrettyTable(head)
ind_stats = PrettyTable(['Recovery', 'Deaths', 'Positivity Rate'])
summ_head = ['Total', 'Active', 'Recovered', 'Deaths', 'Testing Done']
country_data = d['TT']
#Country's Timestamp - Denotes the time of Updation of Data
country_ts = country_data['meta']['last_updated'].split('T')[0] + ' ' + country_data['meta']['last_updated'].split('T')[1][:5] + ' IST'
for i in state_code:
if 'total' in d[i]:
confirmed = d[i]['total'].get('confirmed', 0)
deceased = d[i]['total'].get('deceased', 0)
recovered = d[i]['total'].get('recovered', 0)
tested = d[i]['total'].get('tested', 0)
active = confirmed - (deceased + recovered)
ind_state_table.add_row([state_code[i], confirmed, active, recovered, deceased, tested])
else:
continue
'''Extracting India Data.'''
ind_table = PrettyTable(summ_head)
confirmed = country_data['total'].get('confirmed', 0)
recovered = country_data['total'].get('recovered', 0)
deceased = country_data['total'].get('deceased', 0)
tested = country_data['total'].get('tested', 0)
delta_confirmed = str(confirmed) + '(+' + str(country_data['delta'].get('confirmed', 0)) + ')' if 'delta' in country_data else confirmed
delta_recovered = str(recovered) + '(+' + str(country_data['delta'].get('recovered', 0)) + ')' if 'delta' in country_data else recovered
delta_deceased = str(deceased) + '(+' + str(country_data['delta'].get('deceased', 0)) + ')' if 'delta' in country_data else deceased
delta_tested = str(tested) + '(+' + str(country_data['delta'].get('tested', 0)) + ')'
active = confirmed - (recovered + deceased)
ind_table.add_row([delta_confirmed, active, delta_recovered, delta_deceased, delta_tested])
ind_stats.add_row([f'{round((recovered/confirmed)*100,2)}%',
f'{round((deceased/confirmed)*100,2)}%',
f'{round((confirmed/tested)*100,2)}%'])
print(f'{ind_state_table}\n')
print(f' India Cases Summary (as on {country_ts}) '.center(70,'*'))
print(f'{ind_table}\n')
print(f' India Statistics '.center(60,'*'))
print(ind_stats)
#Sending SLACK Notification - Country's Data
india_msg = f"***** INDIA Cases ******\nDate: {country_ts}\nTotal: {confirmed}\nActive: {active}\nRecovered: {recovered}\nTests Done: {tested}\nDeaths: {deceased}"
if slack_notify:
Slack.slack_message(india_msg, __file__)
except Exception as e :
print(f'EXCEPTION in "getIndiaData()" Function: {e}')
def getStateData(state):
'''Prints the District Level Data of a given State, State Summary and India Summary of Corona Cases.'''
try:
#District Summary Table
head = ['District', 'Total', 'Active', 'Recovered', 'Deceased']
dist_table = PrettyTable(head)
try:
state_data = d[state]
except:
print(f'EXCEPTION: No State Found with this Code')
#CSV Filename and Path
#os.chdir('C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python38-32')
state_filename = f'COVID-19_{state_code[state]}_Districts_Data.csv'
#Writing the Header to the file
with open(state_filename, 'w') as f:
writer = csv.writer(f, delimiter=',', lineterminator='\n')
writer.writerow([x.upper() for x in head])
#State Timestamp - Denotes the time of Updation
state_ts = state_data['meta']['last_updated'].split('T')[0] + ' ' + state_data['meta']['last_updated'].split('T')[1][:5] + ' IST'
summ_head = ['Total', 'Active', 'Recovered', 'Deaths', 'Testing Done']
summ_table = PrettyTable(summ_head)
#Table that shows the Percentages of categories
state_stats = PrettyTable(['Recovery', 'Deaths', 'Positivity Rate'])
'''Extracting District wise Data of a State'''
with open(state_filename, 'a') as f:
writer = csv.writer(f, delimiter=",", lineterminator="\n")
for i in d.get(state, 'Not Found').get('districts', 'Not Found'):
for j in d[state]['districts'][i]['total']:
confirmed = int(d[state]['districts'][i]['total'].get('confirmed', 0))
recovered = int(d[state]['districts'][i]['total'].get('recovered', 0))
deceased = int(d[state]['districts'][i]['total'].get('deceased', 0))
active = confirmed - (recovered + deceased)
active = -1 * active if active < 0 else active
#Adding the data to the Table
dist_table.add_row([i, confirmed, active, recovered, deceased])
writer.writerow([i, confirmed, active, recovered, deceased])
print(f'\nFile Created Successfully. <<< {state_filename} >>>\n')
'''Extracting State's Data.'''
confirmed = state_data['total'].get('confirmed', 0)
recovered = state_data['total'].get('recovered', 0)
deceased = state_data['total'].get('deceased', 0)
tested = state_data['total'].get('tested', 0)
#Adding the New Cases If any.
delta_confirmed = str(confirmed) + '(+' + str(state_data['delta'].get('confirmed', 0)) + ')' if 'delta' in state_data else confirmed
delta_recovered = str(recovered) + '(+' + str(state_data['delta'].get('recovered', 0)) + ')' if 'delta' in state_data else recovered
delta_deceased = str(deceased) + '(+' + str(state_data['delta'].get('deceased', 0)) + ')' if 'delta' in state_data else deceased
active = confirmed - (recovered + deceased)
summ_table.add_row([delta_confirmed, active, delta_recovered, delta_deceased, tested])
print(f" Cases across {state_code.get(state, 'Unknown')} (District wise details) ".center(70,'*'))
print(f'{dist_table}\n')
print(f' {state_code[state].title()} Cases Summary (as on {state_ts})'.center(70,'*'))
print(f'{summ_table}\n')
print(f" {state_code.get(state, 'Unknown').upper()} Statistics ".center(70,'*'))
state_stats.add_row([f'{round((recovered/confirmed)*100,2)}%',
f'{round((deceased/confirmed)*100,2)}%',
f'{round((confirmed/tested)*100,2)}%'])
print(f'{state_stats}\n')
#Sending SLACK Notification - State's Data
state_msg = f"***** {state_code.get(state, 'Unknown')} Cases ******\nDate: {state_ts}\nTotal: {confirmed}\nActive: {active}\nRecovered: {recovered}\nTests Done: {tested}\nDeaths: {deceased}"
if slack_notify:
Slack.slack_message(state_msg, __file__)
except Exception as e:
print(f'EXCEPTION in "getStateData()" Function: {e}')
#Driver Code - Pass the State Code as the argument.
getStateData('TG')
getIndiaData()