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()