From 32512f6720e387e8aedd367b4dd9ab25e297fdd2 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Thu, 14 Sep 2023 04:43:48 -0700 Subject: [PATCH] Price tracker working, only for best buy --- Python_Scripts/PriceTracker/PriceTracker.py | 37 +++++++++++ Python_Scripts/PriceTracker/PriceTracker2.py | 64 +++++++++++++++++++ Python_Scripts/PriceTracker/products.csv | 2 + Python_Scripts/PriceTracker/products.db | Bin 0 -> 12288 bytes 4 files changed, 103 insertions(+) create mode 100644 Python_Scripts/PriceTracker/PriceTracker.py create mode 100644 Python_Scripts/PriceTracker/PriceTracker2.py create mode 100644 Python_Scripts/PriceTracker/products.csv create mode 100644 Python_Scripts/PriceTracker/products.db diff --git a/Python_Scripts/PriceTracker/PriceTracker.py b/Python_Scripts/PriceTracker/PriceTracker.py new file mode 100644 index 0000000..2c9d10d --- /dev/null +++ b/Python_Scripts/PriceTracker/PriceTracker.py @@ -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() \ No newline at end of file diff --git a/Python_Scripts/PriceTracker/PriceTracker2.py b/Python_Scripts/PriceTracker/PriceTracker2.py new file mode 100644 index 0000000..02d4c7a --- /dev/null +++ b/Python_Scripts/PriceTracker/PriceTracker2.py @@ -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) diff --git a/Python_Scripts/PriceTracker/products.csv b/Python_Scripts/PriceTracker/products.csv new file mode 100644 index 0000000..caa784b --- /dev/null +++ b/Python_Scripts/PriceTracker/products.csv @@ -0,0 +1,2 @@ +url,alert_price +https://store.google.com/us/config/chromecast_google_tv?hl=en-US&selections=eyJwcm9kdWN0RmFtaWx5IjoiWTJoeWIyMWxZMkZ6ZEY5bmIyOW5iR1ZmZEhZPSIsImhlcm9Qcm9kdWN0cyI6W1siY0hKa1h6YzRNekpmTXprMU1nPT0iLDEsbnVsbF1dfQ%3D%3D,40 diff --git a/Python_Scripts/PriceTracker/products.db b/Python_Scripts/PriceTracker/products.db new file mode 100644 index 0000000000000000000000000000000000000000..9b0953e7df4826de74f239aece6e4eb2bc3105a7 GIT binary patch literal 12288 zcmeI#&r8EF6bJC66NQ04ZaeJq=xJla%g%yTgLJlCtr^mt1Lj}t(bv%l z+r^uQ@qLhnBrj?6>FK>ZdwFT-V^PeKk_O^RNGWcKgbyaDqPG1GOUu9M=vq`HrO~HuU*z>mYpGBVX0)MBszmOIt zDNQ!Xk}|RBfv=%mU;XAZyH#z@MLz-p5P$##AOHafKmY;|fB*y_0D&VGK>dHjKNnYn X00bZa0SG_<0uX=z1Rwwb2-E^UTOU}4 literal 0 HcmV?d00001