-
Notifications
You must be signed in to change notification settings - Fork 6
/
vaccine_availability.py
149 lines (121 loc) · 5.27 KB
/
vaccine_availability.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
"""
python vaccine_availability.py
"""
# standard imports
import requests
import datetime
import json
# import pandas as pd
import smtplib
def logger(line):
with open('log.txt', 'a+') as f:
f.write(line+"\n")
"""
To get the state code
for state_code in range(1,40):
# print("State code: ", state_code)
logger("State code: "+ str(state_code))
response = requests.get(
"https://cdn-api.co-vin.in/api/v2/admin/location/districts/{}".format(state_code))
json_data = json.loads(response.text)
for i in json_data["districts"]:
# print(i["district_id"],'\t', i["district_name"])
logger(str(i["district_id"])+'\t'+str(i["district_name"]))
# print("\n")
logger("\n")
"""
DIST_ID = 446
numdays = 20
age = 19
# Print available centre description (y/n)?
print_flag = 'y'
base = datetime.datetime.today()
date_list = [base + datetime.timedelta(days=x) for x in range(numdays)]
date_str = [x.strftime("%d-%m-%Y") for x in date_list]
def getSlots(DIST_ID=446, numdays=20, age=19):
flag_available = False
Available_Slots = []
for INP_DATE in date_str:
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id={}&date={}".format(
DIST_ID, INP_DATE)
response = requests.get(URL)
if response.ok:
resp_json = response.json()
# print(json.dumps(resp_json, indent = 2))
if resp_json["centers"]:
# print("Available on: {}".format(INP_DATE))
logger("Checking on: {}".format(INP_DATE))
if(print_flag == 'y' or print_flag == 'Y'):
for center in resp_json["centers"]:
for session in center["sessions"]:
if not int(session["available_capacity"]) == 0:
if session["min_age_limit"] <= age:
flag_available = True
dict_to_add = {"Date": INP_DATE,
"Name": center["name"],
"Block Name": center["block_name"],
"Fee Type": center["fee_type"],
"Available Capacity": session["available_capacity"],
"Vaccine": session["vaccine"]}
# print(dict_to_add)
Available_Slots.append(dict_to_add)
# print(Available_Slots)
logger("\t" + str(center["name"]))
logger("\t" + str(center["block_name"]))
logger("\t Price: " +
str(center["fee_type"]))
logger("\t Available Capacity: " +
str(session["available_capacity"]))
"""
print("\t", center["name"])
print("\t", center["block_name"])
print("\t Price: ", center["fee_type"])
print("\t Available Capacity: ",
session["available_capacity"])
"""
if(session["vaccine"] != ''):
logger("\t Vaccine: " +
str(session["vaccine"]))
# print("\n\n")
logger("\n\n")
return flag_available, Available_Slots
"""
if flag_available == False:
logger("No available slots on {}".format(INP_DATE))
return flag_available, Available_Slots
else:
# print("No available slots on {}".format(INP_DATE))
logger("No available slots on {}".format(INP_DATE))
return flag_available, Available_Slots
"""
def send_mail(body, receiver_email='[email protected]', subject='VACCINE AVAILABILITY NOTIFICATION'):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
sender_email = '[email protected]'
server.login(sender_email, 'machinelearning@#$000')
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
sender_email,
receiver_email,
msg
)
# print('Email has been sent !')
server.quit()
def convert_to_str(Available_Slots):
string = ""
for slots in Available_Slots:
for key in slots:
val = slots[key]
string += str(key) + " : " + str(val) + "\n"
string += "\n\n"
return string
if __name__ == '__main__':
flag_available, Available_Slots = getSlots(DIST_ID=255, numdays=20, age=21)
msg = "No available slots found"
body = convert_to_str(Available_Slots) if len(Available_Slots) > 0 else msg
MAILS = ['[email protected]', '[email protected]']
for mail in MAILS:
send_mail(body, receiver_email=mail,
subject='VACCINE AVAILABILITY NOTIFICATION')