Price tracker working, only for best buy

This commit is contained in:
Jacob Delgado 2023-09-14 04:43:48 -07:00
parent 2c97267e4a
commit 32512f6720
4 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import requests
from bs4 import BeautifulSoup
from pony import orm
from datetime import datetime
db = orm.Database()
# Create sqlite database if it doesn't exist
db.bind(provider='sqlite', filename='products.db',create_db=True)
class Product(db.Entity):
name = orm.Required(str)
price = orm.Required(float)
created_data = orm.Required(datetime)
# Create the tables within the database
db.generate_mapping(create_tables=True)
def BestBuy(session):
url = "https://www.bestbuy.com/site/chromecast-with-google-tv-4k-snow/6425976.p?skuId=6425976"
response = session.get(url)
soup = BeautifulSoup(response.text, "html.parser")
data = (
"BestBuy",
float(soup.select_one("div.row.p-none.m-none div.priceView-hero-price.priceView-customer-price").text.replace("Your price for this item is $49.99", "").removeprefix("$")),
)
return data
def main():
session = requests.Session()
session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
})
print(BestBuy(session))
main()

View File

@ -0,0 +1,64 @@
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)

View File

@ -0,0 +1,2 @@
url,alert_price
https://store.google.com/us/config/chromecast_google_tv?hl=en-US&selections=eyJwcm9kdWN0RmFtaWx5IjoiWTJoeWIyMWxZMkZ6ZEY5bmIyOW5iR1ZmZEhZPSIsImhlcm9Qcm9kdWN0cyI6W1siY0hKa1h6YzRNekpmTXprMU1nPT0iLDEsbnVsbF1dfQ%3D%3D,40
1 url alert_price
2 https://store.google.com/us/config/chromecast_google_tv?hl=en-US&selections=eyJwcm9kdWN0RmFtaWx5IjoiWTJoeWIyMWxZMkZ6ZEY5bmIyOW5iR1ZmZEhZPSIsImhlcm9Qcm9kdWN0cyI6W1siY0hKa1h6YzRNekpmTXprMU1nPT0iLDEsbnVsbF1dfQ%3D%3D 40

Binary file not shown.