-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper_runner.py
More file actions
120 lines (106 loc) · 4.63 KB
/
scraper_runner.py
File metadata and controls
120 lines (106 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import sqlite3
import time
import random
import sys
from datetime import datetime, timezone
from scraper_amazon import scrape_multiple_urls as scrape_amazon_urls
from scraper_flipkart import scrape_multiple_urls as scrape_flipkart_urls
from telegram_notify import send_telegram_message
DB_PATH = "price_tracker.db"
def fetch_products(conn):
cur = conn.cursor()
cur.execute("SELECT id, name, url, source, target_price FROM Products")
return cur.fetchall()
def most_recent_price(conn, product_id):
cur = conn.cursor()
cur.execute("""
SELECT price FROM PriceHistory
WHERE product_id = ?
ORDER BY datetime(date) DESC
LIMIT 1
""", (product_id,))
row = cur.fetchone()
return row[0] if row else None
def insert_price(conn, product_id, price, source):
cur = conn.cursor()
cur.execute("""
INSERT INTO PriceHistory (product_id, date, price, source)
VALUES (?, ?, ?, ?)
""", (product_id, datetime.now(timezone.utc).isoformat(timespec="seconds"), price, source))
conn.commit()
def run():
conn = sqlite3.connect(DB_PATH)
products = fetch_products(conn)
if not products:
print("No products found. Insert rows into Products first.")
conn.close()
return 1
amazon_products = [(pid, name, url, target) for pid, name, url, source, target in products if source.lower() == "amazon"]
flipkart_products = [(pid, name, url, target) for pid, name, url, source, target in products if source.lower() == "flipkart"]
if amazon_products:
urls = [url for _, _, url, _ in amazon_products]
scraped_data = scrape_amazon_urls(urls)
for idx, data in enumerate(scraped_data):
pid, name, target = amazon_products[idx][0], amazon_products[idx][1], amazon_products[idx][3]
price, title = data["price"], data["title"]
if price is None:
print(f"Amazon: Price not found for {data['url']}")
continue
insert_price(conn, pid, price, "amazon")
print(f"Amazon: {title or name} --> {price:,.0f} INR")
prev_price = most_recent_price(conn, pid)
if prev_price and price < prev_price:
print(f"Price dropped from {prev_price:,.0f} --> {price:,.0f}")
msg = (
f"Price dropped for {title or name} (Amazon):\n"
f"Previous: ₹{prev_price:,.0f}\n"
f"Current: ₹{price:,.0f}\n"
f"URL: {urls[idx]}"
)
send_telegram_message(msg)
if target and price <= float(target):
print(f"Target reached: target {target:,.0f}, now {price:,.0f}")
msg = (
f"Target reached for {title or name} (Amazon):\n"
f"Target: ₹{target:,.0f}\n"
f"Current: ₹{price:,.0f}\n"
f"URL: {urls[idx]}"
)
send_telegram_message(msg)
time.sleep(random.uniform(2, 4))
if flipkart_products:
urls = [url for _, _, url, _ in flipkart_products]
scraped_data = scrape_flipkart_urls(urls)
for idx, data in enumerate(scraped_data):
pid, name, target = flipkart_products[idx][0], flipkart_products[idx][1], flipkart_products[idx][3]
price, title = data["price"], data["title"]
if price is None:
print(f"Flipkart: Price not found for {data['url']}")
continue
insert_price(conn, pid, price, "flipkart")
print(f"Flipkart: {title or name} --> {price:,.0f} INR")
prev_price = most_recent_price(conn, pid)
if prev_price and price < prev_price:
print(f"Price dropped from {prev_price:,.0f} --> {price:,.0f}")
msg = (
f"Price dropped for {title or name} (Flipkart):\n"
f"Previous: ₹{prev_price:,.0f}\n"
f"Current: ₹{price:,.0f}\n"
f"URL: {urls[idx]}"
)
send_telegram_message(msg)
if target and price <= float(target):
print(f"Target reached: target {target:,.0f}, now {price:,.0f}")
msg = (
f"Target reached for {title or name} (Flipkart):\n"
f"Target: ₹{target:,.0f}\n"
f"Current: ₹{price:,.0f}\n"
f"URL: {urls[idx]}"
)
send_telegram_message(msg)
time.sleep(random.uniform(2, 4))
conn.close()
print("\nAll done.")
return 0
if __name__ == "__main__":
sys.exit(run())