-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
54 lines (46 loc) · 1.82 KB
/
main.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
import importlib
import os
import requests
import yagmail
from bs4 import BeautifulSoup
from importlib import util
import urls
importlib.reload(urls)
from urls import URL_LIST
# loop through the URLs
for each_url in URL_LIST:
try:
# query each website and return html, parse the html using Beautiful Soup and store in variable 'soup'
page = requests.get(each_url)
soup = BeautifulSoup(page.content, 'html.parser')
# take out the <div> of name and get its value
product_name_box = soup.find('h1', attrs={'class': 'h1 product-name text-uppercase d-none d-sm-block large-devices'})
product_name = product_name_box.text.strip()
price_box = soup.find('span', attrs={'class': 'sup-hide'})
price = price_box.text.strip()
availability_box = soup.find('span', attrs={'class': 'availability-msg'})
availability = availability_box.text.strip()
status = 'Product Name: ' + product_name + '\n' + 'Price: ' + price + '\n' + 'Availability: ' + availability
except Exception:
continue
else:
# create a text file
with open ('/tmp/inventory.txt', 'a') as f:
f.write(status + '\n' + '\n')
def send_email():
FROM = 'george.davitiani@gmail.com'
TO = 'george.davitiani@hey.com'
subject = 'Inventory Report - GCP'
#contents = '/tmp/inventory.txt'
contents = [yagmail.inline('/tmp/inventory.txt')]
yag = yagmail.SMTP(FROM, 'zmytgndtkxdukwwk')
yag.send(TO, subject, contents)
# email if keywords are found in the text file
def hello_pubsub(event, context):
with open('/tmp/inventory.txt') as f:
if 'In Stock' in f.read():
send_email()
elif 'Backorder' in f.read():
send_email()
elif 'Select Styles for Availability' in f.read():
send_email()