-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbengine.py
192 lines (166 loc) · 6.05 KB
/
dbengine.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
import threading
from typing import Any, Dict, Generator, Type
from venues.abstract_venue import AbstractVenue, IncorrectVenueImplementation
dbname = "bandevents.db"
def init_db() -> None:
sql_schema = ""
with open("schema.sql", mode="r", encoding="utf-8") as f:
sql_schema = f.readlines()
with sqlite3.connect(dbname) as conn:
cur = conn.cursor()
for stmt in sql_schema:
cur.execute(stmt)
conn.commit()
cur.close()
class DBEngine(object):
def __init__(self) -> None:
self.conn = None
self.__first_run()
self.lock = threading.Lock()
def __first_run(self) -> None:
if self.conn is None:
self.conn = sqlite3.connect(dbname)
def close(self) -> None:
with self.lock:
if self.conn:
self.conn.close()
def pluginCreateVenueEntity(self, venue: Dict[str, str]) -> None:
"""
Create needed venue entries.
Parameter venue is Dogshome.eventSQLentity(), i.e.
"""
cols = ", ".join(venue.keys())
placeholders = ":" + ", :".join(venue.keys())
q = "INSERT OR IGNORE INTO venue (%s) VALUES (%s);" \
% (cols, placeholders)
with self.lock:
cur = None
try:
cur = self.conn.cursor()
cur.execute(q, venue)
self.conn.commit()
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
def insertVenueEvents(self, venue: AbstractVenue, events: Dict[str, Any]) -> None:
"""
Insert parsed events from a venue into the database.
"""
with self.lock:
cur = None
try:
cur = self.conn.cursor()
for event in events:
venue_id = self.getVenueByName(
event["venue"],
venue.get_city(),
venue.get_country())[0]
event["venueid"] = venue_id
event.pop("venue") # venue -> venueid to match sql implementation
cols = ", ".join(event.keys())
placeholders = ":" + ", :".join(event.keys())
q = f"INSERT OR IGNORE INTO event ({cols}) VALUES ({placeholders});"
cur.execute(q, event)
except Exception as e:
print(f"Failed with error message: {e}")
finally:
self.conn.commit()
cur.close()
def insertLastFMartists(self, artist: str, playcount: int) -> None:
q = "INSERT OR REPLACE INTO artist (name, playcount) VALUES (?, ?);"
with self.lock:
cur = None
try:
cur = self.conn.cursor()
cur.execute(q, [artist, playcount])
self.conn.commit()
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
def getVenues(self) -> Dict[str, str]:
q = "SELECT id, name, city, country FROM venue"
cur = None
results = dict()
try:
cur = self.conn.cursor()
res = cur.conn.execute(q)
results = res.fetchall()
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
return results
def getVenueByName(self, vname: str, city: str, country: str) -> str:
q = "SELECT id, name, city, country FROM venue " \
+ "WHERE name = ? AND city = ? AND country = ? LIMIT 1;"
venue_name = str()
cur = None
try:
cur = self.conn.cursor()
results = cur.execute(q, [vname, city, country])
venue_name = results.fetchone()
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
return venue_name
def getAllGigs(self) -> Dict[str, str]:
q = "SELECT DISTINCT e.date, v.name, v.city, e.name " \
+ "FROM event AS e INNER JOIN venue AS v ON e.venueid = v.id " \
+ "GROUP BY e.date, v.name ORDER BY e.date ASC;"
gigs = dict()
cur = None
try:
cur = self.conn.cursor()
results = cur.execute(q)
gigs = results.fetchall()
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
return gigs
def getArtists(self) -> Generator[Dict[str, str], None, None]:
q = "SELECT name, playcount FROM artist;"
cur = None
try:
cur = self.conn.cursor()
results = cur.execute(q)
for artist, playcount in results.fetchall():
yield {"artist": artist,
"playcount": playcount}
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
def getArtist(self, name: str) -> Generator[Dict[str, str], None, None]:
q = "SELECT name, playcount FROM artist " \
+ "WHERE name = ? LIMIT 5;"
cur = None
try:
cur = self.conn.cursor()
results = cur.execute(q, [name])
for artist, playcount in results.fetchall():
yield {"artist": artist,
"playcount": playcount}
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()
def purgeOldEvents(self) -> None:
q = "DELETE FROM event " \
+ "WHERE strftime('%Y-%m-%d', date) < date('now');"
with self.lock:
cur = None
try:
cur = self.conn.cursor()
cur.execute(q)
self.conn.commit()
except Exception as e:
print(f"Failed with error message: {e}")
finally:
cur.close()