Last active
April 7, 2021 10:38
-
-
Save NSBum/a55463dc518a045d1faa88cfffc6879d to your computer and use it in GitHub Desktop.
MHLU vaccine notifier
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
#!/usr/bin/env python3 | |
try: | |
# Python 2 | |
from urllib2 import urlopen | |
except ImportError: | |
from urllib.request import urlopen, Request | |
from lxml import etree | |
import re | |
from py_imessage import imessage | |
import random | |
import datetime | |
import sqlite3 | |
import pprint | |
import time | |
db_path = 'path/to/database/script_flags.sqlite3' | |
conn = sqlite3.connect(db_path) | |
cursor = conn.cursor() | |
q = 'SELECT COUNT(*) FROM vaccine WHERE did_notify = 1' | |
cursor.execute(q) | |
rows = cursor.fetchall() | |
result = rows[0] | |
if result[0] > 0: | |
conn.close() | |
exit() | |
# check the time and decide whether to hit the site or not | |
# only check once an hour for hours 23,0-4 | |
# for hours 5-8 check on minutes 0,20,40 | |
# for other hours check every 10 minutes | |
now = datetime.datetime.now() | |
wants_check = False | |
hour = now.hour | |
minute = now.minute | |
if hour == 23 or (hour >= 0 and hour < 5): | |
if minute == 0: | |
wants_check = True | |
elif hour >= 5 and hour < 9 and minute % 20 == 0: | |
wants_check = True | |
elif hour >9 and hour < 23 and minute % 10 == 0: | |
wants_check = True | |
if wants_check == False: | |
conn.close() | |
exit() | |
notification_target = 'my_phone_number' | |
url = "https://www.healthunit.com/covid-19-vaccine-eligibility" | |
user_agents = [ | |
'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11', | |
'Opera/9.25 (Windows NT 5.1; U; en)', | |
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', | |
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)', | |
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19', | |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0', | |
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0.1) Gecko/20100101 Firefox/8.0.1', | |
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.151 Safari/535.19', | |
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246', | |
'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36' | |
] | |
randomint = random.randint(0,9) | |
headers = {'user-agent': user_agents[randomint]} | |
xpathselector = '//*[@id="colD"]/div[1]/div/table/tbody/tr[1]/td/p[1]/strong/span' | |
headers = {'user-agent': user_agents[randomint]} | |
try: | |
response = urlopen(Request(url, headers = headers)) | |
htmlparser = etree.HTMLParser() | |
tree = etree.parse(response, htmlparser) | |
except: | |
now = datetime.datetime.now() | |
q = f'INSERT INTO vaccine (timestamp, status) VALUES ("{now}", 1)' | |
cursor.execute(q) | |
conn.commit() | |
conn.close() | |
exit() | |
# try a couple different ways of looking for 55 | |
found = False | |
try: | |
ageinfo = tree.xpath(xpathselector) | |
m = re.search(r'55', ageinfo[0].text, re.M) | |
if m: | |
found = True | |
print(f'{ageinfo[0].text}') | |
else: | |
print('age 55 not found') | |
except: | |
pass | |
if found == False: | |
try: | |
yearinfo = tree.xpath('//*[@id="colD"]/div[1]/div/table/tbody/tr[1]/td/p[2]/span') | |
m = re.search(r'born\sin\s(\d{4})', yearinfo[0].text, re.M) | |
year = int(m[1]) | |
if year > 1964: | |
found = True | |
else: | |
print('birth year cutoff <= 1964') | |
except: | |
pass | |
if found == False: | |
try: | |
m = re.search(r'turning\s(\d{2})\s.+2021', yearinfo[0].text, re.M) | |
age = int(m[1]) | |
if age < 57: | |
found = True | |
else: | |
print('age cutoff > 56') | |
except: | |
pass | |
notified = False | |
if found: | |
guid = imessage.send(notification_target, "MLHU eligibility may be 55.") | |
if guid: | |
notified = True | |
q = f'INSERT INTO vaccine (timestamp, status, age, year, did_notify) VALUES ("{now}", 1, {age}, {year}, {notified})' | |
cursor.execute(q) | |
conn.commit() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment