-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_manager.py
More file actions
197 lines (165 loc) · 5.9 KB
/
database_manager.py
File metadata and controls
197 lines (165 loc) · 5.9 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
import sqlite3
from song import Song
class DatabaseManager:
def __init__(self, db_path="database.db"):
self.db_path = db_path
def get_connection(self):
"""Get a database connection"""
return sqlite3.connect(self.db_path)
def get_all_songs(self):
"""Retrieve all songs from the database"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("SELECT songID, songName, artist, genre, albumName, duration FROM songs")
rows = cursor.fetchall()
songs = []
for row in rows:
# Create Song objects with available data
song = Song(
track_id=row[0],
title=row[1],
artist_name=row[2],
genre=row[3],
mood_tags=[], # Not in current DB schema
valence=0.5, # Default values for missing attributes
energy=0.5,
danceability=0.5,
tempo=120,
duration=row[5],
preview_url="",
cover_image="",
album_name=row[4],
release_date=""
)
songs.append(song)
conn.close()
return songs
def get_songs_by_genre(self, genre):
"""Get songs filtered by genre"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("SELECT songID, songName, artist, genre, albumName, duration FROM songs WHERE genre = ?", (genre,))
rows = cursor.fetchall()
songs = []
for row in rows:
song = Song(
track_id=row[0],
title=row[1],
artist_name=row[2],
genre=row[3],
mood_tags=[],
valence=0.5,
energy=0.5,
danceability=0.5,
tempo=120,
duration=row[5],
preview_url="",
cover_image="",
album_name=row[4],
release_date=""
)
songs.append(song)
conn.close()
return songs
def get_all_genres(self):
"""Get list of all unique genres"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("SELECT DISTINCT genre FROM songs ORDER BY genre")
rows = cursor.fetchall()
conn.close()
return [row[0] for row in rows]
def get_all_albums(self):
"""Get list of all unique albums with song counts"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("""
SELECT albumName, COUNT(*) as song_count, genre, artist
FROM songs
GROUP BY albumName, artist
ORDER BY albumName
""")
rows = cursor.fetchall()
conn.close()
return rows
def get_songs_by_album(self, album_name):
"""Get all songs from a specific album"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("SELECT songID, songName, artist, genre, albumName, duration FROM songs WHERE albumName = ?", (album_name,))
rows = cursor.fetchall()
songs = []
for row in rows:
song = Song(
track_id=row[0],
title=row[1],
artist_name=row[2],
genre=row[3],
mood_tags=[],
valence=0.5,
energy=0.5,
danceability=0.5,
tempo=120,
duration=row[5],
preview_url="",
cover_image="",
album_name=row[4],
release_date=""
)
songs.append(song)
conn.close()
return songs
def get_song_data_dict(self):
"""Get a dictionary mapping song names to artists"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("SELECT songName, artist FROM songs")
rows = cursor.fetchall()
conn.close()
return {row[0]: row[1] for row in rows}
def get_album_data_dict(self):
"""Get a dictionary mapping album names to lists of songs"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute("SELECT albumName, songName FROM songs ORDER BY albumName, songName")
rows = cursor.fetchall()
album_dict = {}
for album, song in rows:
if album not in album_dict:
album_dict[album] = []
album_dict[album].append(song)
conn.close()
return album_dict
def search_songs(self, query):
"""Search for songs by name, artist, or album"""
conn = self.get_connection()
cursor = conn.cursor()
search_pattern = f"%{query}%"
cursor.execute("""
SELECT songID, songName, artist, genre, albumName, duration
FROM songs
WHERE songName LIKE ? OR artist LIKE ? OR albumName LIKE ?
ORDER BY songName
""", (search_pattern, search_pattern, search_pattern))
rows = cursor.fetchall()
songs = []
for row in rows:
song = Song(
track_id=row[0],
title=row[1],
artist_name=row[2],
genre=row[3],
mood_tags=[],
valence=0.5,
energy=0.5,
danceability=0.5,
tempo=120,
duration=row[5],
preview_url="",
cover_image="",
album_name=row[4],
release_date=""
)
songs.append(song)
conn.close()
return songs