Last active
June 11, 2021 17:08
-
-
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
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
#vaccine crons | |
*/30 * * * * /bin/bash -l -c 'cd /Users/username/path/to/file && python3 main.py' | |
#end |
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 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 = '[email protected]' | |
sender_pass = 'pasword' | |
receiver_address = '[email protected]' | |
#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