Created
November 9, 2021 08:09
-
-
Save debdutgoswami/13cd562ae6a674a407a9d03a3c067e95 to your computer and use it in GitHub Desktop.
Script to send email to the recipients when ever there is slot available for vaccine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime as dt | |
import requests | |
from smtplib import SMTP | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
import time | |
import pandas as pd | |
from requests.sessions import session | |
SENDER_EMAIL = "[email protected]" | |
SENDER_PASS = "your-app-secret-password" | |
def get_data(): | |
data = list() | |
date = (dt.datetime.now() + dt.timedelta(hours=5, minutes=30)).strftime("%d-%m-%Y") | |
pincodes = ["721", "725"] # Added Howrah, Kolkata (id of the respective district) | |
for pincode in pincodes: | |
url = f"http://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id={pincode}&date={date}" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", | |
"authority": "cdn-api.co-vin.in", | |
} | |
r = requests.get(url, headers=headers) | |
if r.status_code != 200: | |
return data | |
try: | |
d = r.json() | |
for center in d["centers"]: | |
name = center["name"] | |
[ | |
data.append([pincode, name, session["date"], session["vaccine"]]) | |
for session in center["sessions"] | |
if (session["min_age_limit"] == 18 and session["available_capacity_dose2"]>0 and center["fee_type"] == "Free" and session["vaccine"] == "COVISHIELD") | |
] | |
except Exception as e: | |
print(f"exception occured with the type {e}") | |
return data | |
def create_df(data): | |
headers = ["pincode", "name", "date", "vaccine"] | |
df = pd.DataFrame(data, columns=headers) | |
df.index += 1 | |
return df | |
def send_email(recipients, df): | |
message = MIMEMultipart() | |
message["Subject"] = "[IMPORTANT] Slots available for COVID-19 Vaccine!" | |
message["From"] = SENDER_EMAIL | |
message.attach(MIMEText(df.to_html(), "html")) | |
message["To"] = ", ".join(recipients) | |
msg_body = message.as_string() | |
server = SMTP("smtp.gmail.com", 587) | |
server.starttls() | |
server.login(message["From"], SENDER_PASS) | |
server.sendmail(message["From"], message["To"], msg_body) | |
server.quit() | |
print("Email sent!!") | |
while True: | |
try: | |
data = get_data() | |
if len(data): | |
df = create_df(data) | |
send_email(["[email protected]"], df) | |
time.sleep(4.28) | |
except KeyboardInterrupt: | |
print("\nEnded search\n") | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment