diff --git a/src/goof.py b/src/goof.py index b5cc69c..1441abf 100644 --- a/src/goof.py +++ b/src/goof.py @@ -7,7 +7,8 @@ from music_kraken import ( Album, Artist, ID3Timestamp, - source_types + SourcePages, + SourceTypes ) from music_kraken.tagging import ( @@ -28,8 +29,12 @@ def div(msg: str = ""): cache = music_kraken.database.new_database.Database("test.db") cache.reset() + main_artist = Artist( - name="I'm in a coffin" + name="I'm in a coffin", + sources=[ + Source(SourcePages.ENCYCLOPAEDIA_METALLUM, "https://www.metal-archives.com/bands/I%27m_in_a_Coffin/127727") + ] ) artist_ref = main_artist.reference @@ -65,8 +70,8 @@ song_input = Song( Lyrics(text="test", language="en") ], sources=[ - Source(source_types.SONG, src="youtube", url="https://youtu.be/dfnsdajlhkjhsd"), - Source(source_types.SONG, src="musify", url="https://ln.topdf.de/Music-Kraken/") + Source(SourcePages.YOUTUBE, "https://youtu.be/dfnsdajlhkjhsd"), + Source(SourcePages.MUSIFY, "https://ln.topdf.de/Music-Kraken/") ], album=album_input, main_artist_list=[main_artist], diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index b20d84e..a713aad 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -41,7 +41,8 @@ musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeI Song = database.Song Artist = database.Artist Source = database.Source -source_types = database.source_types +SourceTypes = database.SourceTypes +SourcePages = database.SourcePages Target = database.Target Lyrics = database.Lyrics Album = database.Album diff --git a/src/music_kraken/database/__init__.py b/src/music_kraken/database/__init__.py index a29e6bf..ad93dcd 100644 --- a/src/music_kraken/database/__init__.py +++ b/src/music_kraken/database/__init__.py @@ -4,7 +4,8 @@ from . import ( ) ID3Timestamp = objects.ID3Timestamp -source_types = objects.source_types +SourceTypes = objects.SourceTypes +SourcePages = objects.SourcePages Song = objects.Song Source = objects.Source Target = objects.Target diff --git a/src/music_kraken/database/new_database.py b/src/music_kraken/database/new_database.py index 9dc65a0..5777e40 100644 --- a/src/music_kraken/database/new_database.py +++ b/src/music_kraken/database/new_database.py @@ -16,10 +16,10 @@ from .objects import ( Artist, Album, ID3Timestamp, - source_types + SourceTypes, + SourcePages ) - logger = logging.getLogger("database") # Due to this not being deployed on a Server **HOPEFULLY** @@ -186,7 +186,7 @@ class Database: # add sources for source in song.sources: source.add_song(song) - source.type_enum = source_types.SONG + source.type_enum = SourceTypes.SONG self.push_source(source=source) # add lyrics @@ -223,8 +223,6 @@ class Database: self.connection.commit() def push_source(self, source: Source): - - if source.song_ref_id is None: logger.warning("the Source don't refer to a song") @@ -234,7 +232,7 @@ class Database: source.id, source.type_str, source.song_ref_id, - source.site_str, + source.page_str, source.url ) @@ -316,6 +314,10 @@ class Database: for album in artist.main_albums: self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference) + for source in artist.sources: + source.add_song(artist) + self.push_source(source) + def pull_lyrics(self, song_ref: Reference = None, lyrics_ref: Reference = None) -> List[Lyrics]: """ Gets a list of sources. if lyrics_ref is passed in the List will most likely only @@ -342,7 +344,7 @@ class Database: language=lyrics_row['language'] ) for lyrics_row in lyrics_rows] - def pull_sources(self, type_enum, song_ref: Reference = None, source_ref: Reference = None) -> List[Source]: + def pull_sources(self, artist_ref: Reference = None, song_ref: Reference = None, source_ref: Reference = None) -> List[Source]: """ Gets a list of sources. if source_ref is passed in the List will most likely only contain one Element if everything goes accordingly. @@ -357,19 +359,23 @@ class Database: if song_ref is not None: where = f"song_id=\"{song_ref.id}\"" elif source_ref is not None: - where = f"id=\"{source_ref.id}\"" + where = f"id=\"{source_ref.id}\" AND type=\"{SourceTypes.SONG.value}\"" + elif artist_ref is not None: + where = f"song_id=\"{artist_ref.id}\" AND type=\"{SourceTypes.ARTIST.value}\"" query = SOURCE_QUERY.format(where=where) self.cursor.execute(query) source_rows = self.cursor.fetchall() - - return [Source( - source_types(source_row['type']), - id_=source_row['id'], - src=source_row['src'], - url=source_row['url'] - ) for source_row in source_rows] + + return [ + Source( + page_enum=SourcePages(source_row['src']), + type_enum=SourceTypes(source_row['type']), + url=source_row['url'], + id_=source_row['id'] + ) for source_row in source_rows + ] def pull_artist_song(self, song_ref: Reference = None, artist_ref: Reference = None) -> List[tuple]: table = "SongArtist" @@ -422,7 +428,8 @@ class Database: artist_obj = Artist( id_=artist_id, - name=artist_row['artist_name'] + name=artist_row['artist_name'], + sources=self.pull_sources(artist_ref=Reference(id_=artist_id)) ) if flat: return artist_obj @@ -491,7 +498,7 @@ class Database: file=song_result['file'], path=song_result['path'] ), - sources=self.pull_sources(type_enum=source_types.SONG, song_ref=Reference(id_=song_id)), + sources=self.pull_sources(song_ref=Reference(id_=song_id)), lyrics=self.pull_lyrics(song_ref=Reference(id_=song_id)), ) @@ -585,7 +592,7 @@ class Database: return album_obj def pull_albums(self, album_ref: Reference = None, song_ref: Reference = None, exclude_relations: set = None) -> \ - List[Album]: + List[Album]: """ This function is used to get matching albums/releses from one song id (a reference object) diff --git a/src/music_kraken/database/objects/__init__.py b/src/music_kraken/database/objects/__init__.py index 1e2e9ea..0478582 100644 --- a/src/music_kraken/database/objects/__init__.py +++ b/src/music_kraken/database/objects/__init__.py @@ -7,7 +7,8 @@ from . import ( ID3_MAPPING = metadata.Mapping ID3Timestamp = metadata.ID3Timestamp -source_types = source.source_types +SourceTypes = source.SourceTypes +SourcePages = source.SourcePages Song = song.Song Artist = song.Artist diff --git a/src/music_kraken/database/objects/song.py b/src/music_kraken/database/objects/song.py index c61d5dc..e296cb3 100644 --- a/src/music_kraken/database/objects/song.py +++ b/src/music_kraken/database/objects/song.py @@ -18,7 +18,11 @@ from .parents import ( SongAttribute, ID3Metadata ) -from .source import Source +from .source import ( + Source, + SourceTypes, + SourcePages +) """ All Objects dependent @@ -118,9 +122,8 @@ class Song(DatabaseObject, ID3Metadata): self.tracksort: int | None = tracksort self.genre: str = genre - self.sources: List[Source] = [] - if sources is not None: - self.sources = sources + self._sources: List[Source] = [] + self.sources = sources self.album = album @@ -192,6 +195,15 @@ class Song(DatabaseObject, ID3Metadata): return metadata + def set_sources(self, source_list: List[Source]): + if source_list is None: + return + + self._sources = source_list + for source in self._sources: + source.type_enum = SourceTypes.SONG + + sources: List[Source] = property(fget=lambda self: self._sources, fset=set_sources) metadata = property(fget=get_metadata) @@ -335,9 +347,9 @@ class Artist(DatabaseObject, ID3Metadata): self.main_albums = main_albums - self.sources = [] - if sources is not None: - self.sources = sources + self._sources = [] + self.sources = sources + def __str__(self): return self.name or "" @@ -381,13 +393,28 @@ class Artist(DatabaseObject, ID3Metadata): return flat_copy_discography def get_id3_dict(self) -> dict: + """ + TODO refactor + :return: + """ id3_dict = { ID3_MAPPING.ARTIST: [self.name] } + if len(self.sources) <= 0: + return id3_dict id3_dict.update(self.sources[0].get_id3_dict()) return id3_dict + def set_sources(self, source_list: List[Source]): + if source_list is None: + return + + self._sources = source_list + for source in self._sources: + source.type_enum = SourceTypes.ARTIST + + sources: List[Source] = property(fget=lambda self: self._sources, fset=set_sources) discography: List[Album] = property(fget=get_discography) features: Album = property(fget=get_features) songs: Album = property(fget=get_songs) diff --git a/src/music_kraken/database/objects/source.py b/src/music_kraken/database/objects/source.py index 021a090..ce71830 100644 --- a/src/music_kraken/database/objects/source.py +++ b/src/music_kraken/database/objects/source.py @@ -7,15 +7,17 @@ from .parents import ( ID3Metadata ) -class source_types(Enum): + +class SourceTypes(Enum): SONG = "song" ALBUM = "album" ARTIST = "artist" LYRICS = "lyrics" -class sources(Enum): + +class SourcePages(Enum): YOUTUBE = "youtube" - MUSIFY = "musify" + MUSIFY = "musify" GENIUS = "genius" MUSICBRAINZ = "musicbrainz" ENCYCLOPAEDIA_METALLUM = "encyclopaedia metallum" @@ -23,16 +25,15 @@ class sources(Enum): @classmethod def get_homepage(cls, attribute) -> str: homepage_map = { - cls.YOUTUBE: "https://www.youtube.com/", - cls.MUSIFY: "https://musify.club/", - cls.MUSICBRAINZ:"https://musicbrainz.org/", + cls.YOUTUBE: "https://www.youtube.com/", + cls.MUSIFY: "https://musify.club/", + cls.MUSICBRAINZ: "https://musicbrainz.org/", cls.ENCYCLOPAEDIA_METALLUM: "https://www.metal-archives.com/", - cls.GENIUS: "https://genius.com/" + cls.GENIUS: "https://genius.com/" } return homepage_map[attribute] - class Source(DatabaseObject, SongAttribute, ID3Metadata): """ create somehow like that @@ -42,22 +43,23 @@ class Source(DatabaseObject, SongAttribute, ID3Metadata): ``` """ - def __init__(self, type_enum, id_: str = None, src: str = None, url: str = None) -> None: + def __init__(self, page_enum, url: str, id_: str = None, type_enum=None) -> None: DatabaseObject.__init__(self, id_=id_) SongAttribute.__init__(self) self.type_enum = type_enum - self.src = sources(src) + self.page_enum = page_enum + self.url = url def get_id3_dict(self) -> dict: - if self.type_enum == source_types.SONG: + if self.type_enum == SourceTypes.SONG: return { Mapping.FILE_WEBPAGE_URL: [self.url], Mapping.SOURCE_WEBPAGE_URL: [self.homepage] } - - if self.type_enum == source_types.ARTIST: + + if self.type_enum == SourceTypes.ARTIST: return { Mapping.ARTIST_WEBPAGE_URL: [self.url] } @@ -65,8 +67,8 @@ class Source(DatabaseObject, SongAttribute, ID3Metadata): return {} def __str__(self): - return f"{self.src}: {self.url}" + return f"{self.page_enum}: {self.url}" - site_str = property(fget=lambda self: self.src.value) + page_str = property(fget=lambda self: self.page_enum.value) type_str = property(fget=lambda self: self.type_enum.value) - homepage = property(fget=lambda self: sources.get_homepage(self.src)) + homepage = property(fget=lambda self: SourcePages.get_homepage(self.page_enum)) diff --git a/src/test.db b/src/test.db index a7d8a2c..1a30c7b 100644 Binary files a/src/test.db and b/src/test.db differ