-
Notifications
You must be signed in to change notification settings - Fork 4
/
dao.py
77 lines (59 loc) · 2.24 KB
/
dao.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from typing import List
import sqlite3
from datamodels import car
def setupNewDB(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)
if os.path.isfile(dirpath + "/newdata.db"):
os.remove(dirpath + "/newdata.db")
newdb = sqlite3.connect(dirpath + "/newdata.db")
try:
newdb.execute(
"CREATE TABLE cars(id TEXT, title TEXT, url TEXT, price TEXT, img TEXT, cdata TEXT)"
)
except sqlite3.OperationalError:
print("Error setting up the database")
newdb.close()
quit()
return newdb
def insertResults(db, results):
for res in results:
db.execute(
"INSERT INTO cars VALUES (?,?,?,?,?,?)",
(res.listing_id, res.title, res.url, res.price, res.img, res.data),
)
db.commit()
def findChanges(dirpath, results: List[car]) -> List[car]:
changes = []
newIDs = list(map(lambda newresult: newresult.listing_id, results))
if not os.path.isfile(dirpath + "/data.db"):
changes = list(map(lambda item: item.with_change_reasons("new"), results))
else:
olddb = sqlite3.connect(dirpath + "/data.db")
for currentCar in results:
oldres = olddb.execute(
"SELECT * from cars WHERE id=?", [currentCar.listing_id]
).fetchone()
if oldres is not None:
oldcar = car(*oldres)
if oldcar != currentCar:
changes.append(
currentCar.with_change_reasons(
'changed',
currentCar.diffFromOld(oldcar),
)
)
else:
changes.append(currentCar.with_change_reasons('new'))
oldCarData = olddb.execute("SELECT * from cars").fetchall()
oldCars = list(map(lambda tpl: car(*tpl), oldCarData))
for oldCar in oldCars:
if oldCar.listing_id not in newIDs:
changes.append(oldCar.with_change_reasons("deleted"))
olddb.close()
return changes
def archiveDatabase(dirpath):
if os.path.isfile(dirpath + "/data.db"):
os.remove(dirpath + "/data.db")
os.rename(dirpath + "/newdata.db", dirpath + "/data.db")