implemented the file and folder paths in the db

This commit is contained in:
lars 2022-10-30 20:30:59 +01:00
parent 6f2ecded4c
commit f16033b584
4 changed files with 48 additions and 24 deletions

View File

@ -1,6 +1,5 @@
import json import json
import os.path import os.path
import pandas as pd
import requests import requests
import logging import logging

View File

@ -148,11 +148,12 @@ SELECT DISTINCT
) )
) )
), ),
'id', track.id,
'musicbrainz_releasetrackid', track.id, 'musicbrainz_releasetrackid', track.id,
'musicbrainz_albumid', release_.id, 'musicbrainz_albumid', release_.id,
'track', track.track, 'title', track.track,
'isrc', track.isrc, 'isrc', track.isrc,
'title', release_.title, 'album', release_.title,
'copyright', release_.copyright, 'copyright', release_.copyright,
'album_status', release_.album_status, 'album_status', release_.album_status,
'language', release_.language, 'language', release_.language,
@ -164,7 +165,12 @@ SELECT DISTINCT
'albumsort', release_group.albumsort, 'albumsort', release_group.albumsort,
'musicbrainz_albumtype', release_group.musicbrainz_albumtype, 'musicbrainz_albumtype', release_group.musicbrainz_albumtype,
'compilation', release_group.compilation, '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 FROM track, release_, release_group,artist, artist_track
WHERE WHERE
@ -196,14 +202,41 @@ def get_tracks_without_isrc():
return get_custom_track(["track.isrc IS NULL"]) 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) init_db(cursor=cursor, connection=connection, reset_anyways=False)
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
# get_track(["track.downloaded == 0", "track.isrc IS NOT NULL"])
#
for track in get_tracks_without_isrc(): for track in get_tracks_without_isrc():
print(track['track'], [artist['name'] for artist in track['artists']]) print(track['track'], [artist['name'] for artist in track['artists']])
# print(get_track_metadata("a85d5ed5-20e5-4f95-8034-d204d81a36dd"))

View File

@ -47,7 +47,9 @@ CREATE TABLE track (
release_id TEXT NOT NULL, release_id TEXT NOT NULL,
track TEXT, track TEXT,
isrc TEXT, isrc TEXT,
filepath TEXT, genre TEXT,
path TEXT,
file TEXT,
url TEXT, url TEXT,
src TEXT src TEXT
); );

View File

@ -1,29 +1,19 @@
import os.path import os.path
import shlex
import pandas as pd
import json import json
from metadata import database
class UrlPath: class UrlPath:
def __init__(self, genre: str, temp: str = "temp", file: str = ".cache3.csv", step_two_file: str = ".cache2.csv"): def __init__(self, genre: str, temp: str = "temp", file: str = ".cache3.csv", step_two_file: str = ".cache2.csv"):
self.temp = temp self.temp = temp
self.file = file self.file = file
self.metadata = pd.read_csv(os.path.join(self.temp, step_two_file), index_col=0)
self.genre = genre self.genre = genre
new_metadata = [] for row in database.get_tracks_without_filepath():
for idx, row in self.metadata.iterrows():
file, path = self.get_path_from_row(row) file, path = self.get_path_from_row(row)
new_row = dict(row) database.set_filepath(row['id'], file, path, genre)
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))
def get_path_from_row(self, row): def get_path_from_row(self, row):
@ -45,7 +35,7 @@ class UrlPath:
return self.escape_part(row['album']) return self.escape_part(row['album'])
def get_artist(self, row): 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]) return self.escape_part(artists[0])
def get_song(self, row): def get_song(self, row):