From 9efc806bec4ee8693e52b40902b47174f897e13e Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Wed, 8 Feb 2023 17:14:51 +0100 Subject: [PATCH] finished fetching of tracklist --- src/metal_archives.py | 26 ++++----- .../database/objects/collection.py | 9 +++ src/music_kraken/database/objects/song.py | 27 ++++++--- .../pages/encyclopaedia_metallum.py | 58 +++++++------------ 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/src/metal_archives.py b/src/metal_archives.py index ceef0cc..d0a1d4f 100644 --- a/src/metal_archives.py +++ b/src/metal_archives.py @@ -1,7 +1,8 @@ from music_kraken import ( Song, Database, - Artist + Artist, + Album ) from music_kraken.pages import ( @@ -13,7 +14,6 @@ test_db = Database("test.db") # test_db.reset() def print_source(source_obj): - print("---source---") for source in source_obj.source_list: print(source) @@ -33,10 +33,15 @@ def print_song(song_: Song): def print_artist(artist: Artist): print(artist) + print_source(artist) print("---discography---") for album in artist.discography: - print(album) - print_source(artist) + print_album(album) + + +def print_album(album: Album): + print(album) + print_source(album) # only_smile = EncyclopaediaMetallum.search_by_query("only smile") @@ -57,14 +62,5 @@ else: print_song(song) artist = song.main_artist_list[0] - -# print_artist(artist) - -# artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False) - -album = EncyclopaediaMetallum.fetch_album_details(song.album, flat=False) -print(album) - -# print_artist(artist) - -# print(only_smile) +artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False) +print_artist(artist) diff --git a/src/music_kraken/database/objects/collection.py b/src/music_kraken/database/objects/collection.py index dd98e08..8592a45 100644 --- a/src/music_kraken/database/objects/collection.py +++ b/src/music_kraken/database/objects/collection.py @@ -73,3 +73,12 @@ class Collection: def __str__(self) -> str: return "\n".join([f"{str(j).zfill(2)}: {i}" for j, i in enumerate(self._data)]) + + def __len__(self) -> int: + return len(self._data) + + def copy(self) -> List: + """ + returns a shallow copy of the data list + """ + return self._data.copy() diff --git a/src/music_kraken/database/objects/song.py b/src/music_kraken/database/objects/song.py index d2bbf7d..a32db30 100644 --- a/src/music_kraken/database/objects/song.py +++ b/src/music_kraken/database/objects/song.py @@ -264,9 +264,17 @@ class Album(DatabaseObject, SourceAttribute, MetadataAttribute): dynamic: bool = False, source_list: List[Source] = None, artists: list = None, - tracklist: List[Song] = None + tracklist: List[Song] = None, + album_type: str = None ) -> None: DatabaseObject.__init__(self, id_=id_, dynamic=dynamic) + + """ + TODO + add to db + """ + self.album_type = album_type + self.title: str = title self.album_status: str = album_status self.label = label @@ -413,9 +421,18 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): self.name: str | None = name self.main_songs = main_songs - self.feature_songs = feature_songs - self.main_albums = main_albums + self.feature_songs = Collection( + data=feature_songs, + map_attributes=["title"], + element_type=Song + ) + + self.main_albums = Collection( + data=main_albums, + map_attributes=["title"], + element_type=Album + ) if source_list is not None: self.source_list = source_list @@ -467,10 +484,6 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): return flat_copy_discography def get_metadata(self) -> MetadataAttribute.Metadata: - """ - TODO refactor - :return: - """ metadata = MetadataAttribute.Metadata({ id3Mapping.ARTIST: [self.name] }) diff --git a/src/music_kraken/pages/encyclopaedia_metallum.py b/src/music_kraken/pages/encyclopaedia_metallum.py index 9f23f11..cc6d39e 100644 --- a/src/music_kraken/pages/encyclopaedia_metallum.py +++ b/src/music_kraken/pages/encyclopaedia_metallum.py @@ -210,19 +210,6 @@ class EncyclopaediaMetallum(Page): """ discography_url = "https://www.metal-archives.com/band/discography/id/{}/tab/all" - # prepare tracklist - album_by_url = dict() - album_by_name = dict() - for album in artist.main_albums: - album_by_name[string_processing.unify(album.title)] = album - for source in album.get_sources_from_page(cls.SOURCE_TYPE): - album_by_url[source.url] = album - old_discography = artist.main_albums.copy() - # save the ids of the albums, that are added to this set, so I can - # efficiently add all leftover albums from the discography to the new one - used_ids = set() - - new_discography: List[Album] = [] # make the request r = cls.API_SESSION.get(discography_url.format(ma_artist_id)) @@ -243,37 +230,32 @@ class EncyclopaediaMetallum(Page): album_id = album_url.split('/')[-1] album_type = td_list[1].text album_year = td_list[2].text - - unified_name = string_processing.unify(album_name) - - album_obj: Album = Album(id_=album_id) - - if album_url in album_by_url: - album_obj = album_by_url[album_url] - used_ids.add(album_obj.id) - - elif unified_name in album_by_name: - album_obj = album_by_name[unified_name] - album_obj.add_source(Source(SourcePages.ENCYCLOPAEDIA_METALLUM, album_url)) - used_ids.add(album_obj.id) - else: - album_obj.add_source(Source(SourcePages.ENCYCLOPAEDIA_METALLUM, album_url)) - - album_obj.title = album_name - album_obj.album_type = album_type + date_obj = None try: - album_obj.date = ID3Timestamp(year=int(album_year)) + date_obj = ID3Timestamp(year=int(album_year)) except ValueError(): pass - new_discography.append(album_obj) - # add the albums back, which weren't on this page - for old_object in old_discography: - if old_object.id not in used_ids: - new_discography.append(old_object) + album_obj: Album = artist.main_albums.get_object_with_source(album_url) or artist.main_albums.get_object_with_attribute("title", album_name) - artist.main_albums = new_discography + if album_obj is not None: + album_obj.add_source(Source(SourcePages.ENCYCLOPAEDIA_METALLUM, album_url)) + album_obj.title = album_name + album_obj.album_type = album_type + if date_obj is not None: + album_obj.date = date_obj + continue + + artist.main_albums.append(Album( + id_=album_id, + title=album_name, + album_type=album_type, + date=date_obj, + source_list=[ + Source(SourcePages.ENCYCLOPAEDIA_METALLUM, album_url) + ] + )) if not flat: for album in artist.main_albums: