-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdb.py
More file actions
78 lines (61 loc) · 1.89 KB
/
db.py
File metadata and controls
78 lines (61 loc) · 1.89 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
import os
from dotenv import load_dotenv
from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError
load_dotenv("api.env")
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017")
MONGO_DB_NAME = os.getenv("MONGO_DB_NAME", "cars_db")
client = MongoClient(MONGO_URI)
db = client[MONGO_DB_NAME]
cars_collection = db["cars_listings"]
chat_debug_collection = db["chat_debug"]
scraped_pages_collection = db["scraped_pages"]
cars_collection.create_index("details_url", unique=True)
scraped_pages_collection.create_index("url", unique=True)
def upsert_cars(cars_docs):
"""
Upserts scraped car documents into MongoDB.
cars_docs: list[dict]
"""
if not cars_docs:
return 0
count = 0
for doc in cars_docs:
url = doc.get("details_url")
if not url:
continue
cars_collection.update_one(
{"details_url": url},
{"$set": doc},
upsert=True,
)
count += 1
return count
def find_cars(max_price=None, city=None, keyword=None, limit=20):
"""
Fetches filtered cars from MongoDB.
Optional:
- max_price
- city
- keyword (brand or any text, e.g. 'bmw')
"""
query = {}
if max_price is not None:
query["price_numeric"] = {"$lte": max_price}
if city:
query["location"] = {"$regex": city, "$options": "i"}
if keyword:
query["title"] = {"$regex": keyword, "$options": "i"}
return list(cars_collection.find(query).limit(limit))
def check_db_health():
"""
MongoDB / DB health check.
Returns: (ok: bool, message: str)
"""
try:
client.admin.command("ping")
return True, "DB connected (MongoDB ping OK)"
except ServerSelectionTimeoutError as e:
return False, f"DB connection failed (timeout): {e}"
except Exception as e:
return False, f"DB error: {e}"