forked from baraolt/food-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
70 lines (61 loc) · 2.84 KB
/
script.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
#!/usr/bin/python3
import bs4
import requests
import smtplib
import logging
import configparser
import http.client
logging.basicConfig(filename='log.log', filemode='w', level=logging.DEBUG)
pushover_conn = http.client.HTTPSConnection('api.pushover.net:443')
#ConfigParser configuration
config = configparser.ConfigParser()
config.read('/app/config.ini')
#ConfigParser variables import
notify_email = config.getboolean('notification', 'email')
notify_pushover = config.getboolean('notification', 'pushover')
toAddress = config.get('email', 'toAddress')
fromAddress = config.get('email', 'fromAddress')
fromPassword = config.get('email', 'fromPassword')
cssselector = config.get('css', 'cssselector')
searchterm = config.get('searchterm', 'keyword')
webpages = config.items('webpages')
pushover_user = config.get('pushover.net', 'PushoverUser')
pushover_token = config.get('pushover.net', 'PushoverToken')
def main():
for key, website in webpages:
getPage = requests.get(website)
getPage.raise_for_status() #if error it will stop the program
#Parse text for keyword
menu = bs4.BeautifulSoup(getPage.text, 'html.parser')
keywords = menu.select(cssselector)
flength = len(searchterm)
available = False
for keyword in keywords:
for i in range(len(keyword.text)):
chunk = keyword.text[i:i+flength].lower()
if chunk == searchterm:
available = True
if available == True:
if notify_email == True:
conn = smtplib.SMTP('smtp.gmail.com', 587) # smtp address and port
conn.ehlo() # call this to start the connection
conn.starttls() # starts tls encryption. When we send our password it will be encrypted.
conn.login(fromAddress, fromPassword)
conn.sendmail(fromAddress, toAddress, 'Subject: python3-webscraper-notifier alert!\n\nAttention!\n\n ' + (searchterm) + ' is available today!\n\nVisit ' + (website) + ' to see it\n\npython3-webscraper-notifier V1.0')
conn.quit()
print('Sent notification e-mails for the following recipients:\n')
for i in range(len(toAddress)):
print(toAddress[i])
print('')
if notify_pushover == True:
r = requests.post("https://api.pushover.net/1/messages.json", data = {
"token": pushover_token,
"user": pushover_user,
"message": "Attention!\n\n " + (searchterm) + " is available today!\n\nVisit " + (website) + " to see it\n\npython3-webscraper-notifier V1.0"
})
print('Sent Pushover push notification')
else:
print('Your favorite item is not available today.')
# process main method call
if __name__ == '__main__':
main()