music-kraken-core/src/music_kraken/database/song.py

144 lines
4.4 KiB
Python
Raw Normal View History

2022-11-22 08:21:28 +00:00
from typing import List
from .artist import Artist
from .metadata import Metadata
from .source import Source
from .target import Target
2022-11-25 17:27:48 +00:00
# I don't import cache from the db module because it would lead to circular imports
# from .temp_database import temp_database as cache
# from . import cache
2022-11-21 14:40:41 +00:00
class Song:
2022-11-30 08:03:14 +00:00
def __init__(
self,
json_response: dict,
) -> None:
"""
id: is not NECESARRILY the musicbrainz id, but is DISTINCT for every song
mb_id: is the musicbrainz_id
target: Each Song can have exactly one target which can be either full or empty
lyrics: There can be multiple lyrics. Each Lyrics object can me added to multiple lyrics
"""
# attributes
self.id: str | None = None
self.mb_id: str | None = None
self.title: str | None = None
self.isrc: str | None = None
self.length: int | None = None
self.metadata: Metadata = Metadata()
# joins
self.artists: List[Artist] = []
self.lyrics: LyricsContainer = LyricsContainer(parent=self)
self.sources: List[Source] = []
self.target: Target = Target()
2022-11-21 14:40:41 +00:00
self.json_data = json_response
# initialize the data
2022-11-24 13:13:55 +00:00
self.id = self.json_data['id']
2022-11-21 14:54:13 +00:00
self.title = self.json_data['title']
2022-11-23 10:33:24 +00:00
self.artists = []
for a in self.json_data['artists']:
new_artist = Artist(a)
exists = False
for existing_artist in self.artists:
if new_artist == existing_artist:
exists = True
break
if not exists:
self.artists.append(new_artist)
self.isrc = self.json_data['isrc']
2022-11-21 14:54:13 +00:00
# initialize the sources
2022-11-22 08:21:28 +00:00
self.sources: List[Source] = []
2022-11-21 14:44:59 +00:00
for src in self.json_data['source']:
if src['src'] is None:
continue
self.sources.append(Source(src))
2022-11-22 17:25:35 +00:00
# initialize the target
self.target = Target()
self.target.file = self.json_data['file']
self.target.path = self.json_data['path']
# initialize id3 metadata
self.metadata = Metadata()
2022-11-22 17:25:35 +00:00
for key, value in self.json_data.items():
self.metadata[key] = value
self.metadata['artist'] = self.get_artist_names()
2022-11-29 13:16:36 +00:00
self.length = self.json_data['length']
# EasyID3.valid_keys.keys()
2022-11-25 17:27:48 +00:00
# the lyrics are not in the metadata class because the field isn't supported
# by easyid3
self.lyrics: LyricsContainer = LyricsContainer(parent=self)
def __str__(self) -> str:
return f"\"{self.title}\" by {', '.join([str(a) for a in self.artists])}"
2022-11-25 11:12:58 +00:00
def __repr__(self) -> str:
return self.__str__()
2022-11-22 17:25:35 +00:00
def get_metadata(self):
return self.metadata.get_all_metadata()
def has_isrc(self) -> bool:
return self.isrc is not None
2022-11-21 14:40:41 +00:00
def get_artist_names(self) -> List[str]:
2022-11-22 13:25:01 +00:00
return [a.name for a in self.artists]
2022-11-21 14:40:41 +00:00
def __getitem__(self, item):
if item not in self.json_data:
return None
return self.json_data[item]
def __setitem__(self, item, value):
if item == "file":
self.target.file = value
return
if item == "path":
self.target.path = value
return
2022-11-21 14:40:41 +00:00
self.json_data[item] = value
2022-11-25 17:27:48 +00:00
class Lyrics:
def __init__(self, text: str, language: str) -> None:
self.text = text
self.language = language
class LyricsContainer:
def __init__(self, parent: Song):
self.lyrics_list: List[Lyrics] = []
self.parent = parent
def append(self, lyrics: Lyrics):
# due to my db not supporting multiple Lyrics yet, I just use for doing stuff with the lyrics
# the first element. I know this implementation is junk, but take it or leave it, it is going
# soon anyway
if len(self.lyrics_list) >= 1:
return
self.lyrics_list.append(lyrics)
# unfortunately can't do this here directly, because of circular imports. If anyone
# took the time to get familiar with this codebase... thank you, and if you have any
# suggestion of resolving this, please open an issue.
# cache.add_lyrics(track_id=self.parent.id, lyrics=lyrics.text)
def extend(self, lyrics_list: List[Lyrics]):
for lyrics in lyrics_list:
self.append(lyrics)
is_empty = property(fget=lambda self: len(self.lyrics_list) <= 0)