Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codewithrajranjan/3a5331369832c635da355276722a8ed2 to your computer and use it in GitHub Desktop.
Save codewithrajranjan/3a5331369832c635da355276722a8ed2 to your computer and use it in GitHub Desktop.
import requests
import datetime
import pytz
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
smtp_server = "smtp.gmail.com"
port = 587
context = ssl.create_default_context()
today_date = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
DATE_TO_SEARCH = "{:02d}-{:02d}-{}".format(today_date.day, today_date.month, today_date.year)
####### Config you need to set ##############
AGE_LIMIT_SEARCH = 18
PINCODE = 854301
TIME_INTERVAL_IN_MINUTES = 10
sender_email = ""
password = ""
if __name__ == '__main__':
count = 1
while 1 == 1:
print("Fetching {} ....".format(count))
count = count + 1
url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(
PINCODE, DATE_TO_SEARCH)
headers = {
'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
}
resp = requests.get(url, headers=headers)
centers = resp.json()["centers"]
available_centers = []
for center in centers:
sessions = center["sessions"]
for session in sessions:
if session["available_capacity"] > 0 and session['min_age_limit'] == AGE_LIMIT_SEARCH:
center_details = dict(
name=center['name'],
block_name=center['block_name'],
pincode=center['pincode'],
date=session['date'],
available_capacity=session['available_capacity'],
vaccine_name=session['vaccine']
)
available_centers.append(center_details)
if len(available_centers) == 0:
print("No availability from date {} and for age group more than {} in pincode {}".format(
DATE_TO_SEARCH, AGE_LIMIT_SEARCH, PINCODE
))
print("sleeping ...")
time.sleep(TIME_INTERVAL_IN_MINUTES * 60)
continue
message = "Subject: Vaccination Centers Available\n"
for available_center in available_centers:
current_center = "Date : {}, PinCode : {}, Center Name : {}, Block Name : {}, capacity: {}, vaccine : {}\n".format(
available_center['date'], available_center['pincode'], available_center['name'],
available_center['block_name'],
available_center['available_capacity'], available_center['vaccine_name']
)
message = message + current_center
print(message)
try:
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = "[email protected]"
msg['Subject'] = "Vaccination-Centers-Available"
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP(smtp_server, port)
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, "[email protected]", msg.as_string())
print("Email Sent")
except Exception as e:
print("email sent error")
print(e)
finally:
server.quit()
print("sleeping ...")
time.sleep(TIME_INTERVAL_IN_MINUTES * 60)
@mayur202529
Copy link

mayur202529 commented May 4, 2021

I have updated it at my repo, now this script can find available centers within a date range (today + next 10 days) within multiple pincodes.
As some metro cities have multiple pincodes.

@harshvladha
Copy link

Add user-agent header if getting 403 status code

@codewithrajranjan
Copy link
Author

Add user-agent header if getting 403 status code

Thanks @harshvladha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment