fixed appending

This commit is contained in:
Hellow 2023-03-10 18:38:32 +01:00
parent be454bb91a
commit 76a91f57cb
5 changed files with 43 additions and 46 deletions

View File

@ -62,14 +62,16 @@ artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False)
print_artist(artist) print_artist(artist)
""" """
results = EncyclopaediaMetallum.search_by_query("#a only smile") results = EncyclopaediaMetallum.search_by_query("#a Ghost Bath")
artist = results[0] artist = results[0]
print(artist) print(artist)
artist: objects.Artist = EncyclopaediaMetallum.fetch_details(artist) artist: objects.Artist = EncyclopaediaMetallum.fetch_details(artist)
print(artist.option_string)
for release in artist.main_album_collection: for release in artist.main_album_collection:
print(release) print(release.option_string)
print(release.song_collection) print(release.song_collection)

View File

@ -42,6 +42,9 @@ class Collection:
def map_element(self, element: DatabaseObject): def map_element(self, element: DatabaseObject):
for name, value in element.indexing_values: for name, value in element.indexing_values:
if value is None:
continue
self._attribute_to_object_map[name][value] = element self._attribute_to_object_map[name][value] = element
def append(self, element: DatabaseObject, merge_on_conflict: bool = True): def append(self, element: DatabaseObject, merge_on_conflict: bool = True):
@ -57,12 +60,13 @@ class Collection:
for name, value in element.indexing_values: for name, value in element.indexing_values:
if value in self._attribute_to_object_map[name]: if value in self._attribute_to_object_map[name]:
# if the object does already exist if merge_on_conflict:
# thus merging and don't add it afterwards # if the object does already exist
existing_object = self._attribute_to_object_map[name][value] # thus merging and don't add it afterwards
existing_object.merge(element) existing_object = self._attribute_to_object_map[name][value]
# in case any relevant data has been added (e.g. it remaps the old object) existing_object.merge(element)
self.map_element(existing_object) # in case any relevant data has been added (e.g. it remaps the old object)
self.map_element(existing_object)
return return
self._data.append(element) self._data.append(element)

View File

@ -56,12 +56,12 @@ class DatabaseObject:
return list() return list()
def merge(self, other, override: bool = False): def merge(self, other, override: bool = False):
if isinstance(other, type(self)): if not isinstance(other, type(self)):
LOGGER.warning(f"can't merge \"{type(other)}\" into \"{type(self)}\"") LOGGER.warning(f"can't merge \"{type(other)}\" into \"{type(self)}\"")
return return
for collection in type(self).COLLECTION_ATTRIBUTES: for collection in type(self).COLLECTION_ATTRIBUTES:
getattr(self, collection).extend(collection) getattr(self, collection).extend(getattr(other, collection))
for simple_attribute in type(self).SIMPLE_ATTRIBUTES: for simple_attribute in type(self).SIMPLE_ATTRIBUTES:
if getattr(other, simple_attribute) is None: if getattr(other, simple_attribute) is None:

View File

@ -87,7 +87,7 @@ class Song(MainObject):
return [ return [
('id', self.id), ('id', self.id),
('title', self.unified_title), ('title', self.unified_title),
('isrc', self.isrc.strip()), ('isrc', self.isrc),
*[('url', source.url) for source in self.source_collection] *[('url', source.url) for source in self.source_collection]
] ]

View File

@ -211,7 +211,6 @@ class EncyclopaediaMetallum(Page):
""" """
discography_url = "https://www.metal-archives.com/band/discography/id/{}/tab/all" discography_url = "https://www.metal-archives.com/band/discography/id/{}/tab/all"
# make the request # make the request
r = cls.API_SESSION.get(discography_url.format(ma_artist_id)) r = cls.API_SESSION.get(discography_url.format(ma_artist_id))
if r.status_code != 200: if r.status_code != 200:
@ -237,15 +236,15 @@ class EncyclopaediaMetallum(Page):
except ValueError(): except ValueError():
pass pass
artist.main_album_collection.append(Album( artist.main_album_collection.append(
id_=album_id, Album(
title=album_name, id_=album_id,
album_type=album_type, title=album_name,
date=date_obj, album_type=album_type,
source_list=[ date=date_obj,
Source(SourcePages.ENCYCLOPAEDIA_METALLUM, album_url) source_list=[Source(SourcePages.ENCYCLOPAEDIA_METALLUM, album_url)]
] )
)) )
if not flat: if not flat:
for album in artist.main_album_collection: for album in artist.main_album_collection:
@ -346,9 +345,13 @@ class EncyclopaediaMetallum(Page):
label_url = None label_url = None
if label_anchor is not None: if label_anchor is not None:
label_url = label_anchor.get("href") label_url = label_anchor.get("href")
print(label_url) label_id = None
if type(label_url) is str and "/" in label_url:
label_id = label_url.split("/")[-1]
artist.label_collection.append( Label( artist.label_collection.append(
Label(
_id=label_id,
name=label_name, name=label_name,
source_list=[ source_list=[
Source(cls.SOURCE_TYPE, label_url) Source(cls.SOURCE_TYPE, label_url)
@ -378,7 +381,7 @@ class EncyclopaediaMetallum(Page):
@classmethod @classmethod
def fetch_artist_details(cls, artist: Artist, flat: bool = False) -> Artist: def fetch_artist_details(cls, artist: Artist, flat: bool = False) -> Artist:
source_list = artist.get_sources_from_page(cls.SOURCE_TYPE) source_list = artist.source_collection.get_sources_from_page(cls.SOURCE_TYPE)
if len(source_list) == 0: if len(source_list) == 0:
return artist return artist
@ -412,7 +415,7 @@ class EncyclopaediaMetallum(Page):
@classmethod @classmethod
def fetch_album_details(cls, album: Album, flat: bool = False) -> Album: def fetch_album_details(cls, album: Album, flat: bool = False) -> Album:
source_list = album.get_sources_from_page(cls.SOURCE_TYPE) source_list = album.source_collection.get_sources_from_page(cls.SOURCE_TYPE)
if len(source_list) == 0: if len(source_list) == 0:
return album return album
@ -458,33 +461,21 @@ class EncyclopaediaMetallum(Page):
minutes, seconds = duration_stamp.split(":") minutes, seconds = duration_stamp.split(":")
length = (int(minutes) * 60 + int(seconds))*1000 # in milliseconds length = (int(minutes) * 60 + int(seconds))*1000 # in milliseconds
track: Song = album.song_collection.get_object_with_source(track_id) or album.song_collection.get_object_with_attribute("title", title) album.song_collection.append(
Song(
if track is not None: id_=track_id,
track.add_source(Source(cls.SOURCE_TYPE, track_id)) title=title,
if length is not None: length=length,
track.length = length tracksort=track_sort,
track.tracksort = track_sort source_list=[Source(cls.SOURCE_TYPE, track_id)]
continue )
track = Song(
id_=track_id,
title=title,
length=length,
tracksort=track_sort,
source_list=[
Source(cls.SOURCE_TYPE, track_id)
]
) )
album.song_collection.append(track)
return album return album
@classmethod @classmethod
def fetch_song_details(cls, song: Song, flat: bool = False) -> Song: def fetch_song_details(cls, song: Song, flat: bool = False) -> Song:
source_list = song.get_sources_from_page(cls.SOURCE_TYPE) source_list = song.source_collection.get_sources_from_page(cls.SOURCE_TYPE)
if len(source_list) == 0: if len(source_list) == 0:
return song return song