From cb5b1b1fccc5cdbc12729f240239a0005c488cb7 Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 20 Jan 2023 23:05:15 +0100 Subject: [PATCH] added source to artist --- src/goof.py | 13 ++++-- src/music_kraken/__init__.py | 3 +- src/music_kraken/database/__init__.py | 3 +- src/music_kraken/database/new_database.py | 43 ++++++++++-------- src/music_kraken/database/objects/__init__.py | 3 +- src/music_kraken/database/objects/song.py | 41 ++++++++++++++--- src/music_kraken/database/objects/source.py | 34 +++++++------- src/test.db | Bin 69632 -> 65536 bytes 8 files changed, 92 insertions(+), 48 deletions(-) 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 a7d8a2c13254cf3d0f00a49e73de18714088a673..1a30c7b5e6268a0ddd6c3669463a86f0a9c12fa6 100644 GIT binary patch literal 65536 zcmeI*%WvGq9S87zSWnxc-NF%!^uWL^EY%(@-;V@IDm&{`wJbH3Hfhp2!jK$Fw7Fbm z$&IZBZ6T-erH9^g%^wh;J?0$8^y0W%;b-1huZn$uK*^Twv?Uh&8LoH!0@k+srOLyf`;C|J-x+_kUF+Z=;uNm#I6PlExBuIDeG3lr|d1UY5)F%WLbK z@>*wgbr>x^j7lN!n3bY*D-=8%^hLhbwe|JY_VU^&DEXE4jpfejrmPGDJ(|{oSd67K zkL=Ui?4FsLzj(2}|3aDbMFL8kvtuOXyaSH$ES8r$o9kOEYqb8Y_S&ZW)7vYzmT$kA zXJ?s)gm;UyZnl56IlFk}?Bx8*7wfes3B-f+otXaO+2Vt&SA5?p_p_yzpL6@0vqk*g zZx-=e-RnnwT8>YT#jl)qEPe%WaTtDS95eACD{_rSc|6OQ$2{2{@a<9T%ehC7g`-Du z)tn-O(@!!e)3T+OpJ(=OP89|Z-zf|>d4F4E<#>9`pqzKi;NU3yvOEf-_*g1;dA5^_ z9BK^HeGp`4ZRNF2yCnG!l0NgKq?bw9(hibxWxB%3YPs^vLvLbgety3G!KLz8F0Nwv zXmU)cxN;ps8s>aOtSrh(t%L0fBDuKI#oQC|Ft{u~! zmux?Mwhez4^7J1bq)~d9U59gHk!+Wjqaqis=G&0@!EAz#61f}2ag++bmjtQ2Kqp8X z$exhz_bJ|sg?HBH{=I_}V083vi=_N19*ahKww6D1=j9^0Mriq-V(Bb&OwJ5nGns3uA@f4Z7FM7Ko~vd%`OSPP-yNix%#&WWBl=Wi zl5F2;Om{Bl!E_YvDxOdUGXj@JhTx2cuEt#9x~8V;t`%6za_HZSl5IJ^zZSmXI*wy9 zPWz+{0+n&@7|h{H5C}D}LYMD1>J-f@jry}4ZU;)B`+`|+7&1fCIdc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