rewrote sqlite querry to return directly a json object
This commit is contained in:
parent
ba60d9bbc1
commit
b808595d1f
@ -1,6 +1,7 @@
|
||||
import sqlite3
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
|
||||
|
||||
def get_temp_dir():
|
||||
@ -13,14 +14,14 @@ def get_temp_dir():
|
||||
return temp_dir
|
||||
|
||||
|
||||
# DATABASE_STRUCTURE_FILE = "database_structure.sql"
|
||||
DATABASE_STRUCTURE_FILE = "src/metadata/database_structure.sql"
|
||||
DATABASE_STRUCTURE_FILE = "database_structure.sql"
|
||||
# DATABASE_STRUCTURE_FILE = "src/metadata/database_structure.sql"
|
||||
TEMP_DIR = get_temp_dir()
|
||||
DATABASE_FILE = "metadata.db"
|
||||
db_path = os.path.join(TEMP_DIR, DATABASE_FILE)
|
||||
|
||||
connection = sqlite3.connect(db_path)
|
||||
connection.row_factory = sqlite3.Row
|
||||
# connection.row_factory = sqlite3.Row
|
||||
cursor = connection.cursor()
|
||||
|
||||
|
||||
@ -125,6 +126,7 @@ def add_track(
|
||||
cursor.execute(query, values)
|
||||
connection.commit()
|
||||
|
||||
|
||||
def get_custom_track_querry(custom_where: list) -> str:
|
||||
where_args = [
|
||||
"track.release_id == release_.id",
|
||||
@ -135,33 +137,36 @@ def get_custom_track_querry(custom_where: list) -> str:
|
||||
where_arg = " AND ".join(where_args)
|
||||
query = f"""
|
||||
SELECT DISTINCT
|
||||
track.id as musicbrainz_releasetrackid,
|
||||
release_.id as musicbrainz_albumid,
|
||||
release_group.id as musicbrainz_releasegroupid,
|
||||
track.track as track,
|
||||
track.isrc as isrc,
|
||||
release_.title as title,
|
||||
release_.copyright as copyright,
|
||||
release_.album_status as album_status,
|
||||
release_.language as language,
|
||||
release_.year as year,
|
||||
release_.date as date,
|
||||
release_.country as country,
|
||||
release_.barcode as barcode,
|
||||
release_group.albumartist as albumartist,
|
||||
release_group.albumsort as albumsort,
|
||||
release_group.musicbrainz_albumtype as musicbrainz_albumtype,
|
||||
release_group.compilation as compilation,
|
||||
release_group.album_artist_id as album_artist_id
|
||||
json_object(
|
||||
'musicbrainz_releasetrackid', track.id,
|
||||
'musicbrainz_albumid', release_.id,
|
||||
'track', track.track,
|
||||
'isrc', track.isrc,
|
||||
'title', release_.title,
|
||||
'copyright', release_.copyright,
|
||||
'album_status', release_.album_status,
|
||||
'language', release_.language,
|
||||
'year', release_.year,
|
||||
'date', release_.date,
|
||||
'country', release_.country,
|
||||
'barcode', release_.barcode,
|
||||
'albumartist', release_group.albumartist,
|
||||
'albumsort', release_group.albumsort,
|
||||
'musicbrainz_albumtype', release_group.musicbrainz_albumtype,
|
||||
'compilation', release_group.compilation,
|
||||
'album_artist_id', release_group.album_artist_id
|
||||
)
|
||||
FROM track, release_, release_group, artist
|
||||
WHERE
|
||||
{where_arg};
|
||||
"""
|
||||
return query
|
||||
|
||||
|
||||
def get_custom_track(custom_where: list):
|
||||
query = get_custom_track_querry(custom_where=custom_where)
|
||||
return [dict(i) for i in cursor.execute(query)]
|
||||
return [json.loads(i[0]) for i in cursor.execute(query)]
|
||||
|
||||
|
||||
def get_track_metadata(musicbrainz_releasetrackid: str):
|
||||
# this would be vulnerable if musicbrainz_releasetrackid would be user input
|
||||
@ -169,14 +174,17 @@ def get_track_metadata(musicbrainz_releasetrackid: str):
|
||||
if len(resulting_tracks) != 1:
|
||||
return -1
|
||||
|
||||
return dict(resulting_tracks[0])
|
||||
return resulting_tracks[0]
|
||||
|
||||
|
||||
def get_tracks_to_download():
|
||||
return get_custom_track(["track.downloaded == 0"])
|
||||
|
||||
|
||||
def get_tracks_without_isrc():
|
||||
return get_custom_track(["track.isrc IS NULL"])
|
||||
|
||||
|
||||
init_db(cursor=cursor, connection=connection, reset_anyways=False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -187,4 +195,4 @@ if __name__ == "__main__":
|
||||
for track in get_tracks_without_isrc():
|
||||
print(track['track'])
|
||||
|
||||
#print(get_track_metadata("a85d5ed5-20e5-4f95-8034-d204d81a36dd"))
|
||||
# print(get_track_metadata("a85d5ed5-20e5-4f95-8034-d204d81a36dd"))
|
||||
|
Loading…
Reference in New Issue
Block a user