Created
May 16, 2021 14:26
-
-
Save am17an/af3ac29f9c89a6b90210012dd4211037 to your computer and use it in GitHub Desktop.
CheckSlots.py - check COWIN slots for 18+ and send 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
| import os,json | |
| import subprocess | |
| from email.mime.text import MIMEText | |
| from email.mime.multipart import MIMEMultipart | |
| import smtplib | |
| import sys | |
| from datetime import datetime,timedelta | |
| import time | |
| #change this your email/password or any random email password. You have to enable less secure apps for this to work (i.e. send emails from your email) | |
| #DO THIS AT YOUR OWN RISK, ideally turn off when you have a slot | |
| #https://myaccount.google.com/lesssecureapps | |
| GMAIL_USERNAME= "abc@gmail.com" | |
| GMAIL_PASS= "password123" | |
| #district ids are given here | |
| #https://gist.github.com/am17an/37196eb0d582d387718f79912ee2156f | |
| #put interested: [list of email ids] | |
| INTERESTED = { | |
| '149': ['info@example.com', 'info@example2.com'], | |
| '395': ['info@example.com'] | |
| } | |
| #Other params | |
| MIN_AGE_LIMIT=18 | |
| CHECK_INTERVAL_IN_SEC=60 | |
| EMAIL_THROTTLE_SEC=300 | |
| MIN_AVAILABLE = 1 | |
| #since vaccine centres close at 5pm, we check for next day from 4pm onwards | |
| DATE= (datetime.now() +timedelta(hours=10)).strftime("%d-%m-%Y") | |
| gmail_user = GMAIL_USERNAME | |
| gmail_password = GMAIL_PASS | |
| lastCheck = datetime.now() - timedelta(minutes=30) | |
| lastEmailSent = datetime.now() - timedelta(minutes=30) | |
| while True: | |
| c = datetime.now() - lastCheck | |
| if c.total_seconds() < CHECK_INTERVAL_IN_SEC: | |
| time.sleep(CHECK_INTERVAL_IN_SEC/2) | |
| continue | |
| lastCheck = datetime.now() | |
| for interested in INTERESTED: | |
| cmd="""curl 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=%s&date=%s' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:88.0) Gecko/20100101 Firefox/88.0' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Origin: https://www.cowin.gov.in' -H 'Connection: keep-alive' -H 'Referer: https://www.cowin.gov.in/' -H 'If-None-Match: W/"e8c3-lZERtR/ECiVk/88nB/flhM7amfY"'"""%(interested, DATE) | |
| print("Checking servers...", cmd) | |
| jsonFile = subprocess.check_output(cmd, shell=True) | |
| #sometimes the curl can spuriously fail, keep going | |
| try: | |
| blob = json.loads(jsonFile) | |
| except: | |
| continue | |
| available_slots = [] | |
| #check availability of dose1, change to _dose2 to check for dose2 | |
| for centre in blob['centers']: | |
| for session in centre['sessions']: | |
| if session['available_capacity_dose1'] >= MIN_AVAILABLE and session['min_age_limit'] <= MIN_AGE_LIMIT: | |
| available_slots.append({'name': centre['name'], 'address': centre['address'], 'center_id': centre['center_id'], 'session': session}) | |
| if len(available_slots) > 0: | |
| diff = datetime.now() - lastEmailSent | |
| if diff.total_seconds() < EMAIL_THROTTLE_SEC: | |
| print(diff.total_seconds(), "not sending email") | |
| continue | |
| lastEmailSent = datetime.now() | |
| #try: | |
| server = smtplib.SMTP_SSL('smtp.gmail.com', 465) | |
| server.ehlo() | |
| server.login(gmail_user, gmail_password) | |
| msg = MIMEMultipart('alternative') | |
| msg['From'] = gmail_user | |
| msg['To'] = ','.join(INTERESTED[interested]) | |
| msg['Subject'] = 'Slot Available in District %s'%(interested) | |
| body = json.dumps(available_slots, indent=2) | |
| msg.attach(MIMEText(body, 'plain')) | |
| server.send_message(msg) | |
| server.close() | |
| print('Email sent!', available_slots, " as of ", datetime.now()) | |
| # except: | |
| # print('Something went wrong...', available_slots, datetime.now()) | |
| else: | |
| print("no slots available...", datetime.now()) | |
| sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment