rewrote sqlite querry to return directly a json object

This commit is contained in:
lars 2022-10-30 16:29:42 +01:00
parent ba60d9bbc1
commit b808595d1f

View File

@ -1,6 +1,7 @@
import sqlite3 import sqlite3
import os import os
import logging import logging
import json
def get_temp_dir(): def get_temp_dir():
@ -13,14 +14,14 @@ def get_temp_dir():
return temp_dir return temp_dir
# DATABASE_STRUCTURE_FILE = "database_structure.sql" DATABASE_STRUCTURE_FILE = "database_structure.sql"
DATABASE_STRUCTURE_FILE = "src/metadata/database_structure.sql" # DATABASE_STRUCTURE_FILE = "src/metadata/database_structure.sql"
TEMP_DIR = get_temp_dir() TEMP_DIR = get_temp_dir()
DATABASE_FILE = "metadata.db" DATABASE_FILE = "metadata.db"
db_path = os.path.join(TEMP_DIR, DATABASE_FILE) db_path = os.path.join(TEMP_DIR, DATABASE_FILE)
connection = sqlite3.connect(db_path) connection = sqlite3.connect(db_path)
connection.row_factory = sqlite3.Row # connection.row_factory = sqlite3.Row
cursor = connection.cursor() cursor = connection.cursor()
@ -125,6 +126,7 @@ def add_track(
cursor.execute(query, values) cursor.execute(query, values)
connection.commit() connection.commit()
def get_custom_track_querry(custom_where: list) -> str: def get_custom_track_querry(custom_where: list) -> str:
where_args = [ where_args = [
"track.release_id == release_.id", "track.release_id == release_.id",
@ -134,34 +136,37 @@ def get_custom_track_querry(custom_where: list) -> str:
where_arg = " AND ".join(where_args) where_arg = " AND ".join(where_args)
query = f""" query = f"""
SELECT DISTINCT SELECT DISTINCT
track.id as musicbrainz_releasetrackid, json_object(
release_.id as musicbrainz_albumid, 'musicbrainz_releasetrackid', track.id,
release_group.id as musicbrainz_releasegroupid, 'musicbrainz_albumid', release_.id,
track.track as track, 'track', track.track,
track.isrc as isrc, 'isrc', track.isrc,
release_.title as title, 'title', release_.title,
release_.copyright as copyright, 'copyright', release_.copyright,
release_.album_status as album_status, 'album_status', release_.album_status,
release_.language as language, 'language', release_.language,
release_.year as year, 'year', release_.year,
release_.date as date, 'date', release_.date,
release_.country as country, 'country', release_.country,
release_.barcode as barcode, 'barcode', release_.barcode,
release_group.albumartist as albumartist, 'albumartist', release_group.albumartist,
release_group.albumsort as albumsort, 'albumsort', release_group.albumsort,
release_group.musicbrainz_albumtype as musicbrainz_albumtype, 'musicbrainz_albumtype', release_group.musicbrainz_albumtype,
release_group.compilation as compilation, 'compilation', release_group.compilation,
release_group.album_artist_id as album_artist_id 'album_artist_id', release_group.album_artist_id
)
FROM track, release_, release_group, artist FROM track, release_, release_group, artist
WHERE WHERE
{where_arg}; {where_arg};
""" """
return query return query
def get_custom_track(custom_where: list): def get_custom_track(custom_where: list):
query = get_custom_track_querry(custom_where=custom_where) 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): def get_track_metadata(musicbrainz_releasetrackid: str):
# this would be vulnerable if musicbrainz_releasetrackid would be user input # 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: if len(resulting_tracks) != 1:
return -1 return -1
return dict(resulting_tracks[0]) return resulting_tracks[0]
def get_tracks_to_download(): def get_tracks_to_download():
return get_custom_track(["track.downloaded == 0"]) return get_custom_track(["track.downloaded == 0"])
def get_tracks_without_isrc(): def get_tracks_without_isrc():
return get_custom_track(["track.isrc IS NULL"]) return get_custom_track(["track.isrc IS NULL"])
init_db(cursor=cursor, connection=connection, reset_anyways=False) init_db(cursor=cursor, connection=connection, reset_anyways=False)
if __name__ == "__main__": if __name__ == "__main__":
@ -187,4 +195,4 @@ if __name__ == "__main__":
for track in get_tracks_without_isrc(): for track in get_tracks_without_isrc():
print(track['track']) print(track['track'])
#print(get_track_metadata("a85d5ed5-20e5-4f95-8034-d204d81a36dd")) # print(get_track_metadata("a85d5ed5-20e5-4f95-8034-d204d81a36dd"))