semi completed pull function for album

This commit is contained in:
Hellow 2022-12-08 09:28:28 +01:00
parent 265f7228bc
commit 5d5bdb3343
6 changed files with 57 additions and 71 deletions

Binary file not shown.

Binary file not shown.

View File

@ -60,5 +60,7 @@ song_output_list = cache.pull_songs(album_ref=album_input.reference)
print(len(song_output_list), song_output_list) print(len(song_output_list), song_output_list)
# getting album # getting album
album_output_list = cache.pull_albums(album_ref=album_input.ref) album_output_list = cache.pull_albums(album_ref=album_input.reference)
print(album_output_list[0]) album_output = album_output_list[0]
print(album_output)
print(album_output.tracklist)

View File

@ -39,7 +39,7 @@ FROM Lyrics
WHERE {where}; WHERE {where};
""" """
ALBUM_QUERY = """ ALBUM_QUERY = """
SELECT Album.id AS album_id, title, copyright, album_status, year, date, country, barcode SELECT Album.id AS album_id, title, copyright, album_status, language, year, date, country, barcode
FROM Album FROM Album
WHERE {where}; WHERE {where};
""" """
@ -268,7 +268,7 @@ class Database:
url=source_row['url'] url=source_row['url']
) for source_row in source_rows] ) for source_row in source_rows]
def get_song_from_row(self, song_result) -> Song: def get_song_from_row(self, song_result, exclude_independent_relations: bool = False) -> Song:
song_id = song_result['song_id'] song_id = song_result['song_id']
return Song( return Song(
@ -286,17 +286,18 @@ class Database:
album_ref=Reference(song_result['album_id']) album_ref=Reference(song_result['album_id'])
) )
def pull_songs(self, song_ref: Reference = None, album_ref: Reference = None) -> List[Song]: def pull_songs(self, song_ref: Reference = None, album_ref: Reference = None, exclude_independent_relations: bool = False) -> List[Song]:
""" """
This function is used to get one song (including its children like Sources etc) This function is used to get one song (including its children like Sources etc)
from one song id (a reference object) from one song id (a reference object)
:param song_ref: :param song_ref:
:param album_ref:
:param exclude_independent_relations:
This excludes all relations from being fetched like for example the Album of the Song.
This is necessary when adding the Song as subclass as e.g. an Album (as tracklist or whatever).
:return requested_song: :return requested_song:
""" """
"""
if song_ref is None:
raise ValueError("The Song ref doesn't point anywhere. Remember to use the debugger.")
"""
where = "1=1" where = "1=1"
if song_ref is not None: if song_ref is not None:
where = f"Song.id=\"{song_ref.id}\"" where = f"Song.id=\"{song_ref.id}\""
@ -307,17 +308,13 @@ class Database:
self.cursor.execute(query) self.cursor.execute(query)
song_rows = self.cursor.fetchall() song_rows = self.cursor.fetchall()
"""
if len(song_rows) == 0:
logger.warning(f"No song found for the id {song_ref.id}")
return Song()
if len(song_rows) > 1:
logger.warning(f"Multiple Songs found for the id {song_ref.id}. Defaulting to the first one.")
"""
return [self.get_song_from_row(song_result=song_result) for song_result in song_rows] return [self.get_song_from_row(
song_result=song_result,
exclude_independent_relations=exclude_independent_relations
) for song_result in song_rows]
def get_album_from_row(self, album_result) -> Album: def get_album_from_row(self, album_result, exclude_independent_relations: bool = False) -> Album:
album_id = album_result['album_id'] album_id = album_result['album_id']
album_obj = Album( album_obj = Album(
@ -332,14 +329,17 @@ class Database:
barcode=album_result['barcode'] barcode=album_result['barcode']
) )
# getting the tracklist if not exclude_independent_relations:
tracklist: List[Song] = self.pull_songs(album_ref=Reference(id_=album_id)) # getting the tracklist
for track in tracklist: tracklist: List[Song] = self.pull_songs(
album_obj.add_song(track.reference, name=track.title) album_ref=Reference(id_=album_id),
exclude_independent_relations=True
)
album_obj.set_tracklist(tracklist=tracklist)
return album_obj return album_obj
def pull_albums(self, album_ref: Reference = None) -> List[Album]: def pull_albums(self, album_ref: Reference = None, exclude_independent_relations: bool = False) -> List[Album]:
""" """
This function is used to get matching albums/releses This function is used to get matching albums/releses
from one song id (a reference object) from one song id (a reference object)
@ -355,5 +355,11 @@ class Database:
album_rows = self.cursor.fetchall() album_rows = self.cursor.fetchall()
return [self.get_album_from_row(
album_result=album_row,
exclude_independent_relations=exclude_independent_relations
) for album_row in album_rows]
if __name__ == "__main__": if __name__ == "__main__":
cache = Database("") cache = Database("")

View File

@ -11,11 +11,11 @@ from .database_object import (
Reference Reference
) )
""" """
All Objects dependent All Objects dependent
""" """
class SongAttribute: class SongAttribute:
def __init__(self, song_ref: Reference = None): def __init__(self, song_ref: Reference = None):
# the reference to the song the lyrics belong to # the reference to the song the lyrics belong to
@ -190,6 +190,11 @@ class Song(DatabaseObject):
self.album_ref = album_ref self.album_ref = album_ref
self.artist_refs = artist_refs self.artist_refs = artist_refs
def __eq__(self, other):
if type(other) != type(self):
return False
return self.id == other.id
def __str__(self) -> str: def __str__(self) -> str:
return f"\"{self.title}\" by {', '.join(self.artist_names)}" return f"\"{self.title}\" by {', '.join(self.artist_names)}"
@ -227,6 +232,7 @@ class Song(DatabaseObject):
All objects dependend on Album All objects dependend on Album
""" """
class Album(DatabaseObject): class Album(DatabaseObject):
""" """
-------DB-FIELDS------- -------DB-FIELDS-------
@ -240,18 +246,18 @@ class Album(DatabaseObject):
barcode TEXT, barcode TEXT,
song_id BIGINT, song_id BIGINT,
""" """
def __init__( def __init__(
self, self,
id_: str = None, id_: str = None,
title: str = None, title: str = None,
copyright_: str = None, copyright_: str = None,
album_status: str = None, album_status: str = None,
language: str = None, language: str = None,
year: str = None, year: str = None,
date: str = None, date: str = None,
country: str = None, country: str = None,
barcode: str = None, barcode: str = None,
song_ref_list: List[Reference] = []
) -> None: ) -> None:
DatabaseObject.__init__(self, id_=id_) DatabaseObject.__init__(self, id_=id_)
self.title: str = title self.title: str = title
@ -263,41 +269,13 @@ class Album(DatabaseObject):
self.country: str = country self.country: str = country
self.barcode: str = barcode self.barcode: str = barcode
self.song_ref_list: List[Reference] = song_ref_list self.tracklist: List[Song] = []
self.track_names = {}
def add_song(self, song_ref: Reference, name: str = None): def set_tracklist(self, tracklist: List[Song]):
if name is not None: self.tracklist = tracklist
self.track_names[song_ref.id] = name
def add_song(self, song: Song):
for existing_song_ref in self.song_ref_list: for existing_song in self.tracklist:
if song_ref == existing_song_ref: if existing_song == song:
return return
self.song_ref_list.append(song_ref) self.tracklist.append(song)
if song_ref.id not in self.track_names:
self.track_names[song_ref.id] = None
if __name__ == "__main__":
"""
Example for creating a Song object
"""
song = Song(
title="Vein Deep in the Solution",
release_name="One Final Action",
target=Target(file="~/Music/genre/artist/album/song.mp3", path="~/Music/genre/artist/album"),
metadata={
"album": "One Final Action"
},
lyrics=[
Lyrics(text="these are some depressive lyrics", language="en")
],
sources=[
Source(src="youtube", url="https://youtu.be/dfnsdajlhkjhsd")
]
)

Binary file not shown.