Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mayur202529/fa21173e82827321b8be81ee885e9592 to your computer and use it in GitHub Desktop.
Save mayur202529/fa21173e82827321b8be81ee885e9592 to your computer and use it in GitHub Desktop.
import sys
import requests
import datetime
import pytz
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
####### Config you need to set ##############
NUMBER_OF_DAYS_TO_SEARCH = 10
AGE_LIMIT_SEARCH = 18
PINCODES = [302004, 302015, 302017, 302029, 302005, 302001, 302005, 302010, 302019, 302011, 302020, 301001]
TIME_INTERVAL_IN_MINUTES = 10
sender_email = "[email protected]"
password = "******"
today_date = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
DATE_TO_SEARCH = "{:02d}-{:02d}-{}".format(today_date.day, today_date.month, today_date.year)
DATES = []
for i in range(0, NUMBER_OF_DAYS_TO_SEARCH):
tempDate = datetime.datetime.strptime(DATE_TO_SEARCH, '%d-%m-%Y') + datetime.timedelta(days=i)
DATES.append("{:02d}-{:02d}-{}".format(tempDate.day, tempDate.month, tempDate.year))
debug = {'verbose': sys.stderr}
user_agent = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
if __name__ == '__main__':
count = 1
while 1 == 1:
print("Fetching {} ....".format(count))
count = count + 1
available_centers = []
for eachDate in DATES:
for eachPincode in PINCODES:
print("Searching for date {} in pincode {}".format(
eachDate, eachPincode
))
url = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(
eachPincode, eachDate)
resp = requests.get(url, headers=user_agent)
centers = resp.json()["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']
)
print("Availability on date {} in pincode {}".format(
DATE_TO_SEARCH, eachPincode
))
available_centers.append(center_details)
if len(available_centers) == 0:
# print("No availability found and for age group more than {} in given pincodes".format(
# AGE_LIMIT_SEARCH
# ))
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)
smtp_server = "smtp.gmail.com"
port = 587
context = ssl.create_default_context()
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment