Skip to content

Instantly share code, notes, and snippets.

@abhianair
Last active June 11, 2021 17:08
Show Gist options
  • Save abhianair/016b2efe6468e52278f3485546db3e7f to your computer and use it in GitHub Desktop.
Save abhianair/016b2efe6468e52278f3485546db3e7f to your computer and use it in GitHub Desktop.
Covid vaccine request - python - A python script to retrieve available hospital name at kozhikode if 18+ vaccine doses(dose 1) are available up to 5 days and result will be send to the email
#vaccine crons
*/30 * * * * /bin/bash -l -c 'cd /Users/username/path/to/file && python3 main.py'
#end
import requests
import json
from datetime import datetime
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# date = datetime.today().strftime('%d-%m-%Y')
base = datetime.datetime.today()
for x in range(0, 5):
cdate = base + datetime.timedelta(days=x)
date = cdate.strftime('%d-%m-%Y')
print(date)
# print(base + datetime.timedelta(days=x))
url_string = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=305&date=' + date
r =requests.get(url_string)
response_body = r.json()
centers = response_body['centers']
adult_hospitals = []
for center in centers:
for session in center['sessions']:
if session['min_age_limit'] == 18 and session['available_capacity_dose1'] > 0:
adult_hospitals.append(center['center_id'])
if len(adult_hospitals) > 0:
hospital_names = []
print('email sending')
for center in centers:
for hospital in adult_hospitals:
if hospital == center['center_id']:
hospital_names.append(center['name'])
mail_content = 'Vaccines are available at: ' + ','.join(hospital_names)
#The mail addresses and password
sender_address = 'sender@gmail.com'
sender_pass = 'pasword'
receiver_address = 'reciever@gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'Vaccines available :' + date #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
else:
print('empty hospitals')
# print(obj.centers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment