-
Notifications
You must be signed in to change notification settings - Fork 1
/
cowin.py
453 lines (351 loc) · 14.3 KB
/
cowin.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import re
import ssl
import time
import hashlib
import smtplib
import datetime
import requests
import PySimpleGUI as sg
from pprint import pprint
from email.message import Message
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
data = {
'state_id': '',
'district_id': '',
'pin': ''
}
cases = {
'1': {
'url': 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByDistrict',
'publicurl': 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict',
'params': {
'district_id': '',
'secret': "U2FsdGVkX18oMx2/93IvSXwHjB2227hFgh0uF09S/7bmvn5EJSIu5PsuQUKMSL+t6xMkqVANHqFfyF3+8Stq1A==",
'date': datetime.date.today().strftime("%d-%m-%Y")
}
},
'2': {
'url': 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByPin',
'publicurl': 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin',
'params': {
'secret': "U2FsdGVkX18oMx2/93IvSXwHjB2227hFgh0uF09S/7bmvn5EJSIu5PsuQUKMSL+t6xMkqVANHqFfyF3+8Stq1A==",
'pincode': '',
'date': datetime.date.today().strftime("%d-%m-%Y")
}
}
}
head = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
}
def test():
string = 'jayesh'
print(hashlib.sha256(string.encode()).hexdigest())
def get_city_code():
state_resp = requests.get(
'https://cdn-api.co-vin.in/api/v2/admin/location/states', headers=head)
states = state_resp.json()['states']
for state in states:
print(state['state_id'], state['state_name'])
state_id = input("\nSelect state code: ")
data['state_id'] = state_id
city_resp = requests.get(
'https://cdn-api.co-vin.in/api/v2/admin/location/districts/'+state_id, headers=head)
cities = city_resp.json()['districts']
for city in cities:
print(city['district_id'], city['district_name'])
district_id = input("\nSelect city code: ")
data['district_id'] = district_id
cases['1']['params']['district_id'] = district_id
return district_id
def login(mobile):
mobiledata = {
'mobile': mobile,
'secret': "U2FsdGVkX18oMx2/93IvSXwHjB2227hFgh0uF09S/7bmvn5EJSIu5PsuQUKMSL+t6xMkqVANHqFfyF3+8Stq1A=="
}
try:
txn_resp = requests.post(
'https://cdn-api.co-vin.in/api/v2/auth/generateMobileOTP', json=mobiledata, headers=head)
# print(resp)
txn_id = txn_resp.json()['txnId']
print("txn: ", txn_id)
otp = input("\nEnter Otp : ")
try:
headerr = {
'accept': 'application/json, text/plain',
'content-type': 'application/json',
'dnt': '1',
'origin': 'https://selfregistration.cowin.gov.in',
'referer': 'https://selfregistration.cowin.gov.in/',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
}
login_resp = requests.post('https://cdn-api.co-vin.in/api/v2/auth/validateMobileOtp', json={
'otp': hashlib.sha256(otp.encode()).hexdigest(), 'txnId': txn_id}, headers=headerr)
print(login_resp)
if login_resp.status_code == 200:
print('\n\nLogin Successful !\n', login_resp.json())
token = login_resp.json()['token']
return token
else:
print("Error")
exit()
except Exception as e:
print(e.with_traceback())
print("Please Try Again")
exit()
except Exception as ex:
print(ex)
print("Please Try Again")
exit()
def get_benif(token):
headr = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'Authorization': 'Bearer '+token
}
try:
benif_response = requests.get(
'https://cdn-api.co-vin.in/api/v2/appointment/beneficiaries', headers=headr)
if benif_response.status_code == 200:
# print(benif_response.headers)
# pprint(benif_response.json())
print(benif_response)
beneficiaries = benif_response.json()
for i in range(len(list(beneficiaries['beneficiaries']))):
print(i, '-> ', beneficiaries['beneficiaries'][i]['name'],
beneficiaries['beneficiaries'][i]['beneficiary_reference_id'])
index = int(input('Select Person by Index : '))
print(list(range(len(list(beneficiaries['beneficiaries'])))))
if index in list(range(len(list(beneficiaries['beneficiaries'])))):
print(
'Returning ', beneficiaries['beneficiaries'][index]['beneficiary_reference_id'])
return beneficiaries['beneficiaries'][index]['beneficiary_reference_id']
except Exception as ex:
print(ex.with_traceback())
# print("Please Try Again")
exit()
def get_captcha(token):
captcha_headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'Authorization': 'Bearer '+token
}
try:
print('\nGetting Captcha\n')
cap_resp = requests.post(
'https://cdn-api.co-vin.in/api/v2/auth/getRecaptcha', headers=captcha_headers)
if cap_resp.status_code == 200:
with open('captcha.svg', 'w') as f:
f.write(
re.sub('(<path d=)(.*?)(fill=\"none\"/>)', '', cap_resp.json()['captcha']))
drawing = svg2rlg('captcha.svg')
renderPM.drawToFile(drawing, "captcha.png", fmt="PNG")
layout = [[sg.Image('captcha.png')],
[sg.Text("Enter Captcha Below")],
[sg.Input()],
[sg.Button('Submit', bind_return_key=True)]]
window = sg.Window('Enter Captcha', layout)
event, values = window.read()
window.close()
return values[1]
else:
print(cap_resp)
print(cap_resp.text)
except Exception as ex:
print(ex)
# print("Please Try Again")
exit()
def schedule(session_id, phone):
print("Scheduling")
token = login(phone)
benifId = get_benif(token)
sched_headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'Authorization': 'Bearer '+token
}
print(benifId)
schedule_details = {
"dose": 1,
"session_id": session_id,
"slot": "FORENOON",
"beneficiaries": [benifId
],
"captcha": ''
}
try:
schedule_details['captcha'] = get_captcha(token)
print('scheduling')
pprint(schedule_details)
print('Trying to schedule')
schedule_response = requests.post(
'https://cdn-api.co-vin.in/api/v2/appointment/schedule', json=schedule_details, headers=sched_headers)
if schedule_response.status_code == 200:
# print(benif_response.headers)
# pprint(benif_response.json())
print(schedule_response)
print(schedule_response.text)
sched = schedule_response.json()
print(sched)
print('Slot Booked Successfully !')
else:
print(schedule_response)
print(schedule_response.text)
except Exception as ex:
print(ex)
# print("Please Try Again")
exit()
def cancel(phone, appo_id):
token = login(phone)
benifId = get_benif(token)
cancel_headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'Authorization': 'Bearer '+token
}
print(benifId)
cancel_details = {
"appointment_id": appo_id,
"beneficiariesToCancel": [
benifId
]
}
try:
#cancel_details['captcha'] = get_captcha(token)
print('scheduling')
pprint(cancel_details)
print('Trying to schedule')
cancel_response = requests.post(
'https://cdn-api.co-vin.in/api/v2/appointment/cancel', json=cancel_details, headers=cancel_headers)
if cancel_response.status_code == 200:
# print(benif_response.headers)
# pprint(benif_response.json())
print(cancel_response)
print(cancel_response.text)
sched = cancel_response.json()
print(sched)
print('Slot Booked Successfully !')
else:
print(cancel_response)
print(cancel_response.text)
sched = cancel_response.json()
print(sched)
except Exception as ex:
print(ex)
# print("Please Try Again")
exit()
def mail(email):
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "chingchongmadafakka@gmail.com" # Enter your address
receiver_email = email # Enter receiver address
password = "chingchongmf"
m = Message()
m['From'] = 'Ching'
m['To'] = email
m['X-Priority'] = '1'
m['Subject'] = 'URGENT!'
m.set_payload('VACCINE AVAILABLE BOOK SOON')
try:
print('Emailing')
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, m.as_string())
print('Email Sent !')
except Exception as e:
print(e)
def search(email, phone, case, token):
preffered_pincodes = []
unselected_sessions = []
head = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
'Authorization': 'Bearer '+token
}
if case == '1':
district_id = get_city_code()
preffered_pincodes = input(
"Enter your Preffered Pincodes (Separate by space) : ").strip().split(' ')
else:
cases['2']['params']['pincode'] = input("Enter PIN Code: ")
preffered_pincodes.append(cases['2']['params']['pincode'])
print(data)
print(preffered_pincodes)
print("\nSeeking Slots. Will Notify as soon as the slots are available\n")
while True:
available_centers = []
try:
#print('url', cases[case]['url'])
#print('params', cases[case]['params'])
centers_resp = requests.get(
cases[case]['url'], params=cases[case]['params'], headers=head)
if centers_resp.status_code == 401:
print('401: Trying Public Server')
centers_resp = requests.get(
cases[case]['publicurl'], params=cases[case]['params'], headers=head)
print(centers_resp)
centers = centers_resp.json()['centers']
for center in centers:
for session in center['sessions']:
# available = True
if (session['available_capacity'] != 0) and (str(center['pincode']) in preffered_pincodes) and (session['session_id'] not in unselected_sessions):
# print('yoooo')
available_centers.append({
"center_id": center['center_id'],
"name": center['name'],
"address": center['address'],
"state_name": center['state_name'],
"district_name": center['district_name'],
"block_name": center['block_name'],
"pincode": center['pincode'],
"fee_type": center['fee_type'],
"date": session['date'],
"vaccine": session['vaccine'],
"session_id": session['session_id'],
"min_age_limit": session['min_age_limit']})
if len(available_centers) != 0:
mail(email)
print("VACCINE SLOT AVAILABLE !!! BOOK FASTA !")
# pprint(available_data)
for i in range(len(available_centers)):
print('\nindex: ', i)
print(available_centers[i]['center_id'], available_centers[i]['name'],
available_centers[i]['block_name'], available_centers[i]['district_name'])
print('pin: ', available_centers[i]['pincode'])
print('date: ', available_centers[i]['date'])
print('vaccine: ', available_centers[i]['vaccine'])
print('age limit: ',
available_centers[i]['min_age_limit'], '+')
print(available_centers[i]['session_id'])
pygame.init()
soundObj = pygame.mixer.Sound('beep.wav')
soundObj.play(-1)
index = int(input('\n\nSelect the index to schedule (-1 if none of above) : '))
soundObj.stop()
if index < 0:
for i in range(len(available_centers)):
unselected_sessions.append(available_centers[i]['session_id'])
print('Unselected Centers = ', unselected_sessions)
continue
print()
print('Scheduling ', available_centers[index]['session_id'])
schedule(available_centers[index]['session_id'], phone)
break
except Exception as e:
#print('error: ', e)
pass
time.sleep(5)
def engine():
email = input("Enter your email: ")
phone = input("Enter your phone number: ")
token = login(phone)
print("1 -> Search By City")
print("2 -> Search By Pin")
print('cases', list(cases.keys()))
switch = input("Enter : ")
if switch in list(cases.keys()):
search(email, phone, switch, token)
else:
print("Enter a valid value")
if __name__ == "__main__":
# test()
engine()