-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracker.py
54 lines (46 loc) · 1.46 KB
/
tracker.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 smtplib
import pandas as pd
import requests
from bs4 import BeautifulSoup
from price_parser import Price
PRODUCT_URL_CSV = "products.csv"
SAVE_TO_CSV = True
PRICES_CSV = "prices.csv"
SEND_MAIL = True
def get_urls(csv_file):
df = pd.read_csv(csv_file)
return df
def get_response(url):
response = requests.get(url)
return response.text
def get_price(html):
soup = BeautifulSoup(html, "lxml")
el = soup.select_one(".price_color")
price = Price.fromstring(el.text)
return price.amount_float
def process_products(df):
updated_products = []
for product in df.to_dict("records"):
html = get_response(product["url"])
product["price"] = get_price(html)
product["alert"] = product["price"] < product["alert_price"]
updated_products.append(product)
return pd.DataFrame(updated_products)
def get_mail(df):
subject = "Price Drop Alert"
body = df[df["alert"]].to_string()
subject_and_message = f"Subject:{subject}\n\n{body}"
return subject_and_message
def send_mail(df):
message_text = get_mail(df)
with smtplib.SMTP("smtp.server.address", 587) as smtp:
smtp.starttls()
smtp.login(mail_user, mail_pass)
smtp.sendmail(mail_user, mail_to, message_text)
def main():
df = get_urls(PRODUCT_URL_CSV)
df_updated = process_products(df)
if SAVE_TO_CSV:
df_updated.to_csv(PRICES_CSV, index=False, mode="a")
if SEND_MAIL:
send_mail(df_updated)