-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
94 lines (76 loc) · 2.68 KB
/
Copy pathdatabase.py
File metadata and controls
94 lines (76 loc) · 2.68 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
import sqlite3
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
logger = logging.getLogger(__name__)
class DatabaseManager:
def __init__(self):
self.conn = sqlite3.connect("listings.db")
self.cursor = self.conn.cursor()
self.initialize()
def initialize(self):
self.cursor.execute("""
CREATE TABLE IF NOT EXISTS listings (
id TEXT PRIMARY KEY,
title TEXT,
price TEXT,
url TEXT,
location TEXT,
date TEXT,
description TEXT
)""")
self.cursor.execute( """ CREATE TABLE IF NOT EXISTS conversations (
conv_id TEXT PRIMARY KEY,
listing_id TEXT NOT NULL,
FOREIGN KEY (listing_id) REFERENCES listings(id)
);
""")
self.conn.commit()
def save_listing(self, listing):
self.cursor.execute("""
INSERT OR REPLACE INTO listings (id, title, price, url, location, date, description)
VALUES (?, ?, ?, ?, ?, ?, ?)
""", (
listing["id"],
listing["title"],
listing["price"],
listing["url"],
listing["location"],
listing["date"],
listing['description']
))
self.conn.commit()
def retrieve_listing(self, listing_id):
self.cursor.execute("SELECT * FROM listings WHERE id = ? LIMIT 1", (listing_id,))
return self.cursor.fetchone()
def save_conversation(self, conv):
self.cursor.execute("""
INSERT OR REPLACE INTO conversations (conv_id, listing_id)
VALUES (?, ?)
""", (
conv["conv_id"],
conv["listing_id"],
))
self.conn.commit()
def exists(self, listing_id):
self.cursor.execute("SELECT 1 FROM listings WHERE id = ? LIMIT 1", (listing_id,))
return self.cursor.fetchone() is not None
def get_listing_id(self, conv_id):
self.cursor.execute("SELECT listing_id FROM conversations WHERE conv_id = ? LIMIT 1", (conv_id,))
return self.cursor.fetchone()
if __name__ == '__main__':
listing = {
"id": "123456789",
"title": "MacBook Pro 2021",
"price": "1200 €",
"url": "https://www.leboncoin.fr/offre/informatique/123456789",
"location": "Paris",
"date": "2025-10-05",
"description": "hello"
}
database_manager = DatabaseManager()
database_manager.save_listing(listing)
logger.info("✅ listing saved locally!")