65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
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 Process_Products(df):
|
|
for product in df.to_dict("records"): # product["url"] is the URL
|
|
pass
|
|
|
|
|
|
def Get_Response(url):
|
|
response = requests.get(url)
|
|
return response.text
|
|
|
|
|
|
def Get_Price(html):
|
|
soup = BeautifulSoup(html, "lmxl")
|
|
el = soup.select_one(".price_color")
|
|
price = price.fromstring(el.text)
|
|
return price.amount_float
|
|
|
|
# Overload the function
|
|
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_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)
|