-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
60 lines (41 loc) · 1.42 KB
/
db.py
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
import sqlite3
import datetime
class DB:
def __init__(self, path):
self.conn = sqlite3.connect(path)
self.curr = self.conn.cursor()
def migrate_up(self):
self.curr.execute("""
CREATE TABLE websites (
url STRING PRIMARY KEY,
score REAL,
timestamp STRING
);
""")
self.conn.commit()
def migrate_down(self):
self.curr.execute("""
DROP TABLE websites;
""")
self.conn.commit()
def reset(self):
self.migrate_down()
self.migrate_up()
def put_website(self, url: str, score: float):
ts = datetime.datetime.now().isoformat()
# Utiliser INSERT OR REPLACE pour mettre à jour si l'entrée existe déjà
self.curr.execute("""
INSERT OR REPLACE INTO websites (url, score, timestamp)
VALUES (?, ?, ?)
""", (url, score, ts))
self.conn.commit()
def fetch_website(self, url):
self.curr.execute("SELECT score, timestamp FROM websites WHERE url = ?", (url,))
result = self.curr.fetchone()
if result is not None:
score, ts = result
return score, datetime.datetime.fromisoformat(ts)
return None
if __name__ == '__main__':
url: str = 'https://hackyeah.pl/'
print(DB(path='./dd.db').fetch_website(url))