From f16033b584058fe666c53ad3aabf1edce2012132 Mon Sep 17 00:00:00 2001 From: lars Date: Sun, 30 Oct 2022 20:30:59 +0100 Subject: [PATCH] implemented the file and folder paths in the db --- src/download_links.py | 1 - src/metadata/database.py | 47 ++++++++++++++++++++++++----- src/metadata/database_structure.sql | 4 ++- src/url_to_path.py | 20 +++--------- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/download_links.py b/src/download_links.py index 22baec6..1e196d2 100644 --- a/src/download_links.py +++ b/src/download_links.py @@ -1,6 +1,5 @@ import json import os.path -import pandas as pd import requests import logging diff --git a/src/metadata/database.py b/src/metadata/database.py index c101ded..9c42439 100644 --- a/src/metadata/database.py +++ b/src/metadata/database.py @@ -148,11 +148,12 @@ SELECT DISTINCT ) ) ), + 'id', track.id, 'musicbrainz_releasetrackid', track.id, 'musicbrainz_albumid', release_.id, - 'track', track.track, + 'title', track.track, 'isrc', track.isrc, - 'title', release_.title, + 'album', release_.title, 'copyright', release_.copyright, 'album_status', release_.album_status, 'language', release_.language, @@ -164,7 +165,12 @@ SELECT DISTINCT 'albumsort', release_group.albumsort, 'musicbrainz_albumtype', release_group.musicbrainz_albumtype, 'compilation', release_group.compilation, - 'album_artist_id', release_group.album_artist_id + 'album_artist_id', release_group.album_artist_id, + 'path', track.path, + 'file', track.file, + 'genre', track.genre, + 'url', track.url, + 'src', track.src ) FROM track, release_, release_group,artist, artist_track WHERE @@ -196,14 +202,41 @@ def get_tracks_without_isrc(): return get_custom_track(["track.isrc IS NULL"]) +def get_tracks_without_filepath(): + return get_custom_track(["(track.file IS NULL OR track.path IS NULL OR track.genre IS NULL)"]) + + +def update_download_status(track_id: str): + pass + + +def set_download_data(track_id: str, url: str, src: str): + query = f""" +UPDATE track +SET url = ?, + src = ? +WHERE '{track_id}' == id; + """ + cursor.execute(query, (url, src)) + connection.commit() + + +def set_filepath(track_id: str, file: str, path: str, genre: str): + query = f""" +UPDATE track +SET file = ?, + path = ?, + genre = ? +WHERE '{track_id}' == id; + """ + cursor.execute(query, (file, path, genre)) + connection.commit() + + init_db(cursor=cursor, connection=connection, reset_anyways=False) if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) - # get_track(["track.downloaded == 0", "track.isrc IS NOT NULL"]) - # for track in get_tracks_without_isrc(): print(track['track'], [artist['name'] for artist in track['artists']]) - - # print(get_track_metadata("a85d5ed5-20e5-4f95-8034-d204d81a36dd")) diff --git a/src/metadata/database_structure.sql b/src/metadata/database_structure.sql index 9f7fc29..d4d0e9f 100644 --- a/src/metadata/database_structure.sql +++ b/src/metadata/database_structure.sql @@ -47,7 +47,9 @@ CREATE TABLE track ( release_id TEXT NOT NULL, track TEXT, isrc TEXT, - filepath TEXT, + genre TEXT, + path TEXT, + file TEXT, url TEXT, src TEXT ); diff --git a/src/url_to_path.py b/src/url_to_path.py index 301741c..3862df5 100644 --- a/src/url_to_path.py +++ b/src/url_to_path.py @@ -1,29 +1,19 @@ import os.path -import shlex -import pandas as pd import json +from metadata import database + class UrlPath: def __init__(self, genre: str, temp: str = "temp", file: str = ".cache3.csv", step_two_file: str = ".cache2.csv"): self.temp = temp self.file = file - self.metadata = pd.read_csv(os.path.join(self.temp, step_two_file), index_col=0) self.genre = genre - new_metadata = [] - - for idx, row in self.metadata.iterrows(): + for row in database.get_tracks_without_filepath(): file, path = self.get_path_from_row(row) - new_row = dict(row) - new_row['path'] = path - new_row['file'] = file - new_row['genre'] = self.genre - new_metadata.append(new_row) - - new_df = pd.DataFrame(new_metadata) - new_df.to_csv(os.path.join(self.temp, self.file)) + database.set_filepath(row['id'], file, path, genre) def get_path_from_row(self, row): @@ -45,7 +35,7 @@ class UrlPath: return self.escape_part(row['album']) def get_artist(self, row): - artists = json.loads(row['artist'].replace("'", '"')) + artists = [artist['name'] for artist in row['artists']] return self.escape_part(artists[0]) def get_song(self, row):