forked from gdancik/VaccineFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaccine.py
140 lines (101 loc) · 3.91 KB
/
vaccine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# coding: utf-8
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time
class vaxfinder :
""" Object to store results of vaccine search, which includes:
now: the time.time() when the search was done
skips: dictionary of current skip locations
msg: the message to send out based on the search
"""
def __init__(self, now, skips, msg):
self.now = now
self.skips = skips
self.msg = msg
def __str__(self) :
return str(self.skips) + '\n' + self.msg
def check_availability(myzip, skips) :
"""Checks vaccine availability from United Way 211.
myzip: the zip code to check availability against
skips: a dictionary of locations to skip, with key being the location
and the value a time in seconds. Locations are reported when
the current time is at least 10 minutes later than this value
Returns a vaxfinder object
"""
# the value of "User-Agent" should be set appropriately
headers = {"User-Agent": "VinnyVaccineFinder/1.0"}
myurl = 'https://www.211ct.org/search?page=1&location='+myzip+'&taxonomy_code=11172&service_area=connecticut'
#page = requests.get(myurl, headers, timeout=None)
driver = webdriver.Firefox()
driver.get(myurl)
time.sleep(4)
page = driver.page_source
now = time.time()
## Selenium can't handle http errors
## if driver.status_code != requests.codes.ok :
## print("Request was not successful, status code:", page.status_code)
## print("Hit enter to continue...")
## input()
## return vaxfinder(now, skips, '')
# Parse page using BeautifulSoup
soup = BeautifulSoup(page, 'html.parser')
#close selenium
driver.close()
print('\n\nPage title:', soup.title.string.strip())
tt = soup.find_all("div",class_="search-result card-highlight")
if not tt :
print('no results found')
return vaxfinder(now, skips, '')
print('skips:')
for sk in skips :
diff = round((10*60 - (now - skips[sk])) / 60,2)
if diff > 700000 :
diff = 'IGNORE'
else :
diff = str(diff) + ' minutes remaining'
print(sk + ': ' + diff)
msg = ''
count = 0
for result in tt:
count += 1
loc = result.find("h3", class_ = "name pointer resource-name")
loc = loc.get_text(strip=True)
print(loc)
dist = result.find("span", class_ = "distance")
dist = dist.get_text(strip=True)
print(dist)
if loc in skips :
if now - skips[loc] < 10*60 :
break
skips[loc] = time.time()
if count < 3:
msg += loc + ', ' + dist + '\n'
if count >= 3 :
msg += '(and more)'
if msg != '' :
msg += '\n' + myurl
return vaxfinder(now, skips, msg)
def send_msg(sender, receiver, msg, password) :
""" Sends an sms message from 'sender' to 'receiver' from the gmail sender account """
if msg == '' :
return
# Establish a secure session with gmail's outgoing SMTP server using your gmail account
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( sender, password )
body = msg
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = ', '.join(receiver)
# Make sure you add a new line in the subject
msg['Subject'] = "Vaccine alert"
# Make sure you also add new lines to your body
# and then attach that body furthermore you can also send html content.
msg.attach(MIMEText(body, 'plain'))
sms = msg.as_string()
server.sendmail(sender,receiver,sms)