-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththirdRockDatabase.py
More file actions
241 lines (208 loc) · 7.25 KB
/
thirdRockDatabase.py
File metadata and controls
241 lines (208 loc) · 7.25 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import sqlite3
import logging
dir_path = os.getcwd()
database_path = os.path.join(dir_path, "data/thirdRockRadio.db")
logger = logging.getLogger(__name__)
def add_entry(song: str, band: str, broadcast_epoch: int):
"""Add song, band, and time song was broadcast to sqlite database
Args:
song (str): Artist's song
band (str): Artist/band name
broadcast_epoch (int): When song was broadcast as unix epoch
"""
band_id = add_band(band)
song_id = add_song(song, band_id)
add_broadcast(broadcast_epoch, song_id)
def setup_sqlite():
"""Setup database with DDL queries if database doesn't exist."""
con = sqlite3.connect(database_path)
cur = con.cursor()
# Create band table
cur.execute(
"""CREATE TABLE IF NOT EXISTS band(
band_id INTEGER PRIMARY KEY,
band_name TEXT NOT NULL UNIQUE
)"""
)
# Create song table
cur.execute(
"""CREATE TABLE IF NOT EXISTS song(
song_id INTEGER PRIMARY KEY,
song_name TEXT NOT NULL,
band_id INTEGER NOT NULL,
UNIQUE(song_name, band_id),
FOREIGN KEY(band_id) REFERENCES band(band_id)
)"""
)
# Create broadcast
cur.execute(
"""CREATE TABLE IF NOT EXISTS broadcast(
broadcast_id INTEGER PRIMARY KEY,
broadcast_datetime INT NOT NULL UNIQUE
)"""
)
# Create broadcast_song table, M:M table
cur.execute(
"""CREATE TABLE IF NOT EXISTS song_broadcast(
song_id INTEGER,
broadcast_id INTEGER,
PRIMARY KEY (song_id, broadcast_id),
FOREIGN KEY (song_id) REFERENCES song(song_id),
FOREIGN KEY (broadcast_id) REFERENCES broadcast(broadcast_id)
)"""
)
con.commit()
def add_band(name: str) -> int:
"""Add band by name to database, does nothing if band already added.
Args:
name (str): band/artist name
Returns:
int: band id
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
name = name.replace("'", "''")
res = cur.execute(f"SELECT band_id FROM band WHERE band_name = '{name}'")
band_id = res.fetchall()
# If band id is null, add band
if len(band_id) == 0:
logger.debug(f'Didn\'t find band "{name}" in database, inserting it.')
res = cur.execute(
f"INSERT INTO band (band_name) VALUES ('{name}') ON CONFLICT DO NOTHING RETURNING band_id"
)
band_id = res.fetchall()
band_id = band_id[0][0]
con.commit()
return band_id
def add_song(name: str, band_id: int) -> int:
"""Add song by name to database, does nothing if song already added.
Args:
name (str): Song name
band_id (int): band's database id
Returns:
int: song database id
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
# TODO Might be a better way to sanatize this
name = name.replace("'", "''")
res = cur.execute(
f"SELECT song_id FROM song WHERE song.song_name = '{name}' AND band_id = {band_id};"
)
song_id = res.fetchall()
# If song id is null, add song
if len(song_id) == 0:
logger.debug(
f'Didn\'t find song "{name}" for band id ({band_id}) in database, inserting it.'
)
res = cur.execute(
f"INSERT INTO song (song_name, band_id) VALUES ('{name}', {band_id}) ON CONFLICT DO NOTHING RETURNING song_id"
)
song_id = res.fetchall()
song_id = song_id[0][0]
con.commit()
return song_id
def add_broadcast(broadcast_time: int, song_id: int):
"""Add song broadcast to database, does nothing if broadcast already recorded.
Args:
broadcast_time (int): broadcast unix epoch
song_id (int): song's database id
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
res = cur.execute(
f"INSERT INTO broadcast (broadcast_datetime) VALUES ({broadcast_time}) ON CONFLICT DO NOTHING RETURNING broadcast_id"
)
broadcast_id = res.fetchall()
if len(broadcast_id) > 0:
broadcast_id = broadcast_id[0][0]
logger.debug(
f"Didn't find broadcast epoch {broadcast_time} for song_id ({song_id}) in database, inserting it."
)
cur.execute(
f"INSERT INTO song_broadcast (song_id, broadcast_id) VALUES ({song_id}, {broadcast_id}) ON CONFLICT DO NOTHING"
)
con.commit()
def get_most_common_song(limit: int = 12) -> list:
"""Get most common song, the song with the most total plays
Args:
limit (int): number of songs to return
Returns:
list: list of songs
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
res = cur.execute(
f"""SELECT song_name, band_name, count(*) as count
FROM song INNER
JOIN song_broadcast ON song.song_id = song_broadcast.song_id
JOIN band ON band.band_id = song.band_id
GROUP BY 1, 2
ORDER BY 3 DESC
LIMIT {limit}"""
)
songs = res.fetchall()
con.commit()
return songs
def get_least_common_song(limit: int = 10) -> list:
"""Get least common song, the song with the least total plays
Args:
limit (int): number of songs to return
Returns:
list: list of songs
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
res = cur.execute(
f"""SELECT song_name, band_name, count(*) as count
FROM song INNER
JOIN song_broadcast ON song.song_id = song_broadcast.song_id
JOIN band ON band.band_id = song.band_id
GROUP BY 1, 2
ORDER BY 3 ASC
LIMIT {limit}"""
)
songs = res.fetchall()
con.commit()
return songs
def get_most_common_artist(limit: int = 12) -> list:
"""Get most common artist, the artist with the most total songs played
Args:
limit (int): number of artists to return
Returns:
list: list of artists
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
res = cur.execute(
f"""SELECT band_name, count(*) as count
FROM band INNER
JOIN song ON song.band_id = band.band_id
GROUP BY 1
ORDER BY 2 DESC
LIMIT {limit};"""
)
songs = res.fetchall()
con.commit()
return songs
def get_least_common_artist(limit: int = 10) -> list:
"""Get least common artist, the artist with the least total songs played
Args:
limit (int): number of artists to return
Returns:
list: list of artists
"""
con = sqlite3.connect(database_path)
cur = con.cursor()
res = cur.execute(
f"""SELECT band_name, count(*) as count
FROM band INNER
JOIN song ON song.band_id = band.band_id
GROUP BY 1
ORDER BY 2 ASC
LIMIT {limit};"""
)
songs = res.fetchall()
con.commit()
return songs