-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCowin-Notify.py
284 lines (213 loc) · 10.8 KB
/
Cowin-Notify.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import sys
import requests
from requests.structures import CaseInsensitiveDict
import telebot
from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerUser, InputPeerChannel
from telethon import TelegramClient, sync, events
import time ,getopt
from sys import argv,exit
from datetime import datetime, timedelta
full_cmd_arguments = argv
argument_list = full_cmd_arguments[1:]
#Argument Description
short_options = "a:c:d:D:A:v:o:t:O"
long_options = ["api=", "cid=", "did=","date=","age=","vaccine=","dose=","threshold=","alternate"]
#Required Values
listOfDistrictID = []
bot_token = 0
bot_chatID = 0
#Default Values
threshold = 1
expireTime = 3
alternate = 0
doses=["1","2"]
vaccinesPreferred = ["COVAXIN","COVISHIELD","SPUTNIK"]
agePreferred = [18,45]
listOfDates = [datetime.today().strftime("%d-%m-%Y")]
#Extracting user args
try:
arguments, values = getopt.getopt(argument_list, short_options, long_options)
except getopt.error as err:
# Output error, and return with an error code
print (str(err))
exit(2)
for current_argument, current_value in arguments:
if current_argument in ("-a", "--api"):
bot_token = current_value
elif current_argument in ("-c", "--cid"):
bot_chatID = current_value
elif current_argument in ("-d", "--did"):
listOfDistrictID = list(map(int,current_value.split(",")))
elif current_argument in ("-D", "--date"):
listOfDates = current_value.split(",")
elif current_argument in ("-A", "--age"):
agePreferred = list(map(int,current_value.split(",")))
elif current_argument in ("-v", "--vaccine"):
vaccinesPreferred = current_value.split(",")
elif current_argument in ("-o", "--dose"):
doses = current_value.split(",")
elif current_argument in ("-e", "--expire"):
expireTime = int(current_value)
elif current_argument in ("-t", "--threshold"):
threshold = int(current_value)
elif current_argument in ("-O", "--alternate"):
alternate = 1
def sendtext(bot_message, bot_token, bot_chatID):
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
def requestInfo(dID,date):
url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id="+str(dID)+"&date="+ date
headers = CaseInsensitiveDict()
headers["accept"] = "application/json"
headers["Accept-Language"] = "en_US"
response = requests.get(url,headers=headers)
data = response.json()
return data
def requestInfoV2(dID,date):
url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id="+str(dID)+"&date="+ date
headers = CaseInsensitiveDict()
headers["accept"] = "application/json"
headers["Accept-Language"] = "en_US"
response = requests.get(url,headers=headers)
data = response.json()
return data
def removeExpired(expire_queue):
now = datetime.now()
topop = []
for i in expire_queue.keys():
if(expire_queue[i] < now ):
topop.append(i)
for i in topop:
expire_queue.pop(i)
#print("E Q",expire_queue)
return expire_queue
def infoV1Parse(bot_token, bot_chatID, listOfDistrictID, listOfDates, agePreferred, vaccinesPreferred, doses, expireTime):
expire_queue = {}
while(True):
try:
expire_queue = removeExpired(expire_queue)
time.sleep( 8*len(listOfDistrictID)*len(listOfDates) )
print("\n"*5)
print(" Checking ".center(50,"#"))
print()
print()
for date in listOfDates:
print()
print("{date}".format(date=date).center(50,"_"))
print("‾".center(50,"‾") )
#print()
for Did in listOfDistrictID:
data = requestInfo(Did,date)
listOfSessions = data["sessions"]
print()
print("DID {d}:".format(d=Did).ljust(50,"~") )
flag = 1
for session in listOfSessions:
if( (session["vaccine"] in vaccinesPreferred)
and ( (int(session["available_capacity_dose1"] ) >= threshold and "1" in doses) or (int(session["available_capacity_dose2"]) >= threshold and "2" in doses) )
and ( int(session["min_age_limit"]) in agePreferred)
):
message = """*{pincode}*\n*For {age}*\n*{date}*\n{hname}\n{address}\n*{vname}:*\n{doses1} doses available for dose 1 \n{doses2} doses available for dose 2\n[{slots}]
""".format(pincode = session["pincode"],
age = session['min_age_limit'],
hname = session['name'],
date = date,
address = session["address"],
vname=session["vaccine"],
doses1 = session["available_capacity_dose1"],
doses2 = session["available_capacity_dose2"],
slots = session["slots"],
)
endingmsg = "\n\n*last updated at:* \n{atime}\n\n\n".format(atime = datetime.now())
if message not in expire_queue.keys():
resp = sendtext(message+endingmsg,bot_token,bot_chatID)
print("Update:{hname})".format(hname=str(session['name'])).rjust(50," ") )
print("D1:{d1} D2:{d2}".format( d1= str(session["available_capacity_dose1"]) ,d2= str(session["available_capacity_dose2"]) ).rjust(50," ") )
expire_queue[message] = datetime.now()+ timedelta(minutes = expireTime )
print(expire_queue)
else:
#print("\n\Unchanged:{hname}\nD1:{d1} D2:{d2}\n\n".format(hname=session['name'] ,d1=session["available_capacity_dose1"],d2=session["available_capacity_dose2"] ))
pass
flag = 0
if(flag):
print("Not Found!".rjust(50," "))
print("Not Found!".rjust(50," "))
print("~".center(50,"~") )
print()
print("_".format(dt=datetime.now() ).center(50,"_") )
print("‾".format(dt=datetime.now() ).center(50,"‾") )
print()
print("\n"*3)
print(" {dt} ".format(dt=datetime.now() ).center(50,"#") )
except KeyboardInterrupt:
sys.exit(2)
except:
print("FAILED")
pass
def infoV2Parse():
expire_queue = {}
while True:
expire_queue = removeExpired(expire_queue)
time.sleep( 8*len(listOfDistrictID)*len(listOfDates) )
print("\n"*5)
print(" Checking ".center(50,"#"))
print("\n"*2)
try:
for date in listOfDates:
print()
print("Week: {date}".format(date=date).center(50,"_"))
print("‾".center(50,"‾") )
for districtID in listOfDistrictID:
data = requestInfoV2(districtID,date)
print()
print("DID {d}:".format(d=districtID).ljust(50,"~") )
flag = 1
for centre in data["centers"]:
listOfSessions = centre["sessions"]
for session in listOfSessions:
if( (session["vaccine"] in vaccinesPreferred)
and ( (int(session["available_capacity_dose1"] ) >= threshold and "1" in doses) or (int(session["available_capacity_dose2"]) >= threshold and "2" in doses) )
and ( int(session["min_age_limit"]) in agePreferred)
):
message = """*{pincode}*\n*For {age}*\n*{date}*\n{hname}\n{address}\n*{vname}:*\n{doses1} doses available for dose 1 \n{doses2} doses available for dose 2\n[{slots}]
""".format(pincode = centre["pincode"],
age = session['min_age_limit'],
hname = centre['name'],
date = session['date'],
address = centre["address"],
vname = session["vaccine"],
doses1 = session["available_capacity_dose1"],
doses2 = session["available_capacity_dose2"],
slots = session["slots"],
)
endingmsg = "\n\n*last updated at:* \n{atime}\n\n\n".format(atime = datetime.now())
if message not in expire_queue.keys():
resp = sendtext(message+endingmsg,bot_token,bot_chatID)
print("Update:{hname})".format(hname=str(centre['name'])).rjust(50," ") )
print("D1:{d1} D2:{d2}".format( d1= str(session["available_capacity_dose1"]) ,d2= str(session["available_capacity_dose2"]) ).rjust(50," ") )
expire_queue[message] = datetime.now()+ timedelta(minutes = expireTime )
print(expire_queue)
else:
#print("\n\Unchanged:{hname}\nD1:{d1} D2:{d2}\n\n".format(hname=session['name'] ,d1=session["available_capacity_dose1"],d2=session["available_capacity_dose2"] ))
pass
flag = 0
print("Waiting..")
time.sleep(5)
if(flag):
print("Not Found!".rjust(50," "))
print("Not Found!".rjust(50," "))
print("~".center(50,"~") )
print()
print("_".format(dt=datetime.now() ).center(50,"_") )
print("‾".format(dt=datetime.now() ).center(50,"‾") )
print()
print("\n"*3)
print(" {dt} ".format(dt=datetime.now() ).center(50,"#") )
except KeyboardInterrupt:
sys.exit(2)
if alternate:
infoV2Parse()
else:
infoV1Parse(bot_token, bot_chatID, listOfDistrictID, listOfDates, agePreferred, vaccinesPreferred, doses, expireTime)