mirror of
https://github.com/Marketscrape/marketscrape-web.git
synced 2026-02-20 15:44:33 -05:00
🚧 Began integrating database lookup.
This commit is contained in:
47
database.py
Normal file
47
database.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import sqlite3
|
||||
import re
|
||||
|
||||
def initialize():
|
||||
con = sqlite3.connect('product_database.db')
|
||||
con.execute("PRAGMA foreign_keys = 2")
|
||||
cur = con.cursor()
|
||||
|
||||
cur.execute('''CREATE TABLE IF NOT EXISTS products
|
||||
(id TEXT PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
initialPrice INTEGER NOT NULL)''')
|
||||
|
||||
cur.execute('''CREATE TABLE IF NOT EXISTS ratings
|
||||
(id TEXT NOT NULL,
|
||||
sentimentRating REAL PRIMARY KEY,
|
||||
priceRating REAL NOT NULL,
|
||||
averageRating REAL NOT NULL,
|
||||
FOREIGN KEY (id) REFERENCES products (id))''')
|
||||
|
||||
cur.execute('''CREATE TABLE IF NOT EXISTS similar
|
||||
(id TEXT NOT NULL,
|
||||
median REAL PRIMARY KEY,
|
||||
lowerBound REAL NOT NULL,
|
||||
upperBound REAL NOT NULL,
|
||||
FOREIGN KEY (id) REFERENCES products (id))''')
|
||||
|
||||
con.commit()
|
||||
|
||||
def insert(url, title, initial_price, sentiment_rating, price_rating, average_rating, median, lower_bound, upper_bound):
|
||||
con = sqlite3.connect('product_database.db')
|
||||
cur = con.cursor()
|
||||
|
||||
# Find the ID of the product
|
||||
market_id = (re.search(r"\/item\/([0-9]*)", url)).group(1)
|
||||
|
||||
try:
|
||||
# Insert the product into the database
|
||||
cur.execute("INSERT INTO products VALUES (?, ?, ?)", (market_id, title, initial_price))
|
||||
# Insert the rating into the database
|
||||
cur.execute("INSERT INTO ratings VALUES (?, ?, ?, ?)", (market_id, sentiment_rating, price_rating, average_rating))
|
||||
# Insert the similar products into the database
|
||||
cur.execute("INSERT INTO similar VALUES (?, ?, ?, ?)", (market_id, median, lower_bound, upper_bound))
|
||||
con.commit()
|
||||
except sqlite3.IntegrityError:
|
||||
# If the product already exists, do nothing
|
||||
pass
|
||||
@@ -1,3 +1,6 @@
|
||||
# Database
|
||||
import database
|
||||
|
||||
# Web Scraping
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
@@ -212,6 +215,9 @@ def reject_outliers(data, m=1.5):
|
||||
return data[standard < m].tolist()
|
||||
|
||||
def main():
|
||||
# Initialize the database
|
||||
database.initialize()
|
||||
|
||||
# Get the URL of the Facebook Marketplace listing
|
||||
url = input("Enter URL: ")
|
||||
|
||||
@@ -239,6 +245,9 @@ def main():
|
||||
price_rating = price_difference_rating(initial_price, median)
|
||||
average_rating = statistics.mean([sentiment_rating, price_rating])
|
||||
|
||||
# Add the listing to the database
|
||||
database.insert(url, title, initial_price, sentiment_rating, price_rating, average_rating, median, lower_bound, upper_bound)
|
||||
|
||||
print("\n● Listing:")
|
||||
print(" ○ Product: {}".format(title))
|
||||
print(" ○ Price: ${:,.2f}".format(initial_price))
|
||||
|
||||
Reference in New Issue
Block a user