Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# DISCLAIMER:
#
# This script is for infomation purposes only.
# * I'm not responsible for your usage of this script, thermonuclear war, or you getting vilified by the government.
# * YOU are choosing to use this script and the CoWIN API, and if you point the finger at me for messing up, I will laugh at you.
#
import sys
import json
import requests
# FILL THIS
access_token = ""
beneficiary = "" # can you can get this form decoding the JWT access token at jwt.io
district = '395' # mumbai
required_hospital = '' # incase you want to book a certain hospital (not recommened since it'll decrease your chances anyways)
# FILL THIS
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Origin': 'https://selfregistration.cowin.gov.in',
'Authorization': f'Bearer {access_token}',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://selfregistration.cowin.gov.in/',
'Sec-GPC': '1',
'TE': 'Trailers',
}
params = (
('district_id', district), # 395 is Mumbai
('date', datetime.now(IST).strftime("%d-%m-%Y")), # todays date
)
response = requests.get('https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByDistrict', headers=headers, params=params)
required_hospital = required_hospital.lower()
center_id = ''
session_id = ''
slot = ''
if response.ok:
print('Sessions available')
resp_json = response.json()
# pprint(resp_json)
for center in resp_json.get('centers'):
for session in center.get('sessions'):
if session.get('available_capacity') > 0 and session.get('min_age_limit') == 18:
print(center)
if required_hospital in center.get('name').lower():
print(center)
print(f"Booking slot in {center.get('name')}")
center_id = center.get('center_id')
session_id = session.get('session_id')
slot = session.get('slots')[0]
break
else:
print("Available Non Matching Center")
print(center)
else: # crutial to ensure you're not blocked
print("Response Failed")
print(response.reason)
print(response.text)
sys.exit(1)
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
'Origin': 'https://selfregistration.cowin.gov.in',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://selfregistration.cowin.gov.in/',
'Sec-GPC': '1',
'TE': 'Trailers',
}
print('Center ID: {}'.format(center_id))
print(session_id)
print(slot)
data = {
"center_id": center_id,
"session_id": session_id,
"beneficiaries":[f"{beneficiary}"],
"slot": slot,
"dose":1
}
data = json.dumps(data)
data = data.replace(' ', '')
print(data)
if center_id != '':
print("Sending POST request to book session")
response = requests.post('https://cdn-api.co-vin.in/api/v2/appointment/schedule', headers=headers, data=data)
if response.ok:
print("Response Succeeded")
print(response)
try:
print(response.json())
except:
pass
else: # crutial to realize you're not blocked
print("Response Failed")
print(response.reason)
print(response.text)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment