-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
92 lines (78 loc) · 3.13 KB
/
database.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
import psycopg2
import json
import os
class Database:
def __init__(self):
db = 'music_clustering'
host = 'localhost'
port = '5432'
user = 'postgres'
# load password from json
with open('db_pw.json') as json_file:
pw = json.load(json_file)['password']
conn_str = (r"dbname='"+db+"' host='"+host+"' port='"+port+"' user='"+user+"' password='"+pw+"'")
# print(f"Connection string: {conn_str}")
dbh = psycopg2.connect(conn_str)
self.dbh = dbh
def add_song(self, info):
cur = self.dbh.cursor()
# check if artist exists
cur.execute(f"SELECT * from artists WHERE name='{info['artist']}'")
records = cur.fetchall()
if len(records) == 0:
cur.execute(f"INSERT INTO artists (name, src_url) VALUES ('{info['artist']}', '{info['src_url']}')")
cur.execute(f"INSERT INTO all_songs (main_genre, artist, orig_file, proc_file_0, song_name) VALUES ('{info['main_genre']}', '{info['artist']}', '{info['orig_file']}', '{info['proc_file_0']}', '{info['song_name']}')")
self.dbh.commit()
cur.close()
def check_song(self, songname):
cur = self.dbh.cursor()
cur.execute(f"SELECT * from all_songs WHERE song_name='{songname}'")
records = cur.fetchall()
cur.close()
return len(records) > 0
def get_artist_by_song_name(self, songname):
cur = self.dbh.cursor()
cur.execute(f"SELECT * from all_songs WHERE song_name='{songname}'")
records = cur.fetchall()
cur.close()
return records[0][2]
def get_file_by_song_name(self, songname):
cur = self.dbh.cursor()
cur.execute(f"SELECT * from all_songs WHERE song_name='{songname}'")
records = cur.fetchall()
cur.close()
return records[0][3]
def add_preprocessed_file(self, songname, filepath):
cur = self.dbh.cursor()
cur.execute(f"UPDATE all_songs SET proc_file_1='{filepath}' WHERE song_name='{songname}'")
self.dbh.commit()
cur.close()
def remove_song(self, songname):
cur = self.dbh.cursor()
cur.execute(f"SELECT * from all_songs WHERE song_name='{songname}'")
records = cur.fetchall()
for record in records:
try:
os.remove(record[3])
os.remove(record[4]+".npy")
except FileNotFoundError:
print("File not found")
cur.execute(f"DELETE FROM all_songs WHERE song_name='{songname}'")
self.dbh.commit()
cur.close()
def remove_artist(self, artist_name):
cur = self.dbh.cursor()
cur.execute(f"DELETE FROM artists WHERE name='{artist_name}'")
self.dbh.commit()
cur.close()
if __name__ == "__main__":
db = Database()
db.remove_song("Common Ground")
# remove all songs where the reference file doesn't exist
# cur = db.dbh.cursor()
# cur.execute("SELECT * FROM all_songs")
# records = cur.fetchall()
# cur.close()
# for record in records:
# if not os.path.exists(record[3]):
# db.remove_song(record[5])