fixed disgusting bug

This commit is contained in:
Hellow2 2023-03-22 12:58:11 +01:00
parent c648a330e0
commit 1f0ae30f02
4 changed files with 47 additions and 55 deletions

View File

@ -49,12 +49,25 @@ class Collection:
self._attribute_to_object_map[name][value] = element self._attribute_to_object_map[name][value] = element
self._used_ids.add(element.id) self._used_ids.add(element.id)
def unmap_element(self, element: DatabaseObject):
for name, value in element.indexing_values:
if value is None:
continue
if value in self._attribute_to_object_map[name]:
if element is self._attribute_to_object_map[name][value]:
try:
self._attribute_to_object_map[name].pop(value)
except KeyError:
pass
def append(self, element: DatabaseObject, merge_on_conflict: bool = True) -> DatabaseObject: def append(self, element: DatabaseObject, merge_on_conflict: bool = True, merge_into_existing: bool = True) -> bool:
""" """
:param element: :param element:
:param merge_on_conflict: :param merge_on_conflict:
:return: :param merge_into_existing:
:return did_not_exist:
""" """
# if the element type has been defined in the initializer it checks if the type matches # if the element type has been defined in the initializer it checks if the type matches
@ -68,15 +81,24 @@ class Collection:
if merge_on_conflict: if merge_on_conflict:
# if the object does already exist # if the object does already exist
# thus merging and don't add it afterwards # thus merging and don't add it afterwards
existing_object.merge(element) if merge_into_existing:
# 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)
return existing_object self.map_element(existing_object)
else:
element.merge(existing_object)
exists_at = self._data.index(existing_object)
self._data[exists_at] = element
self.unmap_element(existing_object)
self.map_element(element)
return True
self._data.append(element) self._data.append(element)
self.map_element(element) self.map_element(element)
return element return False
def extend(self, element_list: Iterable[DatabaseObject], merge_on_conflict: bool = True): def extend(self, element_list: Iterable[DatabaseObject], merge_on_conflict: bool = True):
for element in element_list: for element in element_list:
@ -104,21 +126,3 @@ class Collection:
returns a shallow copy of the data list returns a shallow copy of the data list
""" """
return self._data.copy() return self._data.copy()
def insecure_append(self, element: DatabaseObject):
if element.id in self._used_ids:
return False
self._used_ids.add(element.id)
self._data.append(element)
self.map_element(element)
return True
def insecure_extend(self, element_list: Iterable[DatabaseObject]):
success = False
for element in element_list:
if self.insecure_append(element):
success = True
return success

View File

@ -99,17 +99,18 @@ class Song(MainObject):
def compile(self): def compile(self):
album: Album album: Album
for album in self.album_collection: for album in self.album_collection:
if album.song_collection.insecure_append(self): if album.song_collection.append(self, merge_into_existing=False):
album.compile() album.compile()
artist: Artist artist: Artist
for artist in self.feature_artist_collection: for artist in self.feature_artist_collection:
if artist.feature_song_collection.insecure_append(self): if artist.feature_song_collection.append(self, merge_into_existing=False):
artist.compile() artist.compile()
for artist in self.main_artist_collection: for artist in self.main_artist_collection:
if artist.main_album_collection.insecure_extend(self.album_collection): for album in self.album_collection:
artist.compile() if artist.main_album_collection.append(album, merge_into_existing=False):
artist.compile()
@property @property
def indexing_values(self) -> List[Tuple[str, object]]: def indexing_values(self) -> List[Tuple[str, object]]:
@ -259,17 +260,17 @@ class Album(MainObject):
def compile(self): def compile(self):
song: Song song: Song
for song in self.song_collection: for song in self.song_collection:
if song.album_collection.insecure_append(self): if song.album_collection.append(self, merge_into_existing=False):
song.compile() song.compile()
artist: Artist artist: Artist
for artist in self.artist_collection: for artist in self.artist_collection:
if artist.main_album_collection.insecure_append(self): if artist.main_album_collection.append(self, merge_into_existing=False):
artist.compile() artist.compile()
label: Label label: Label
for label in self.label_collection: for label in self.label_collection:
if label.album_collection.insecure_append(self): if label.album_collection.append(self, merge_into_existing=False):
label.compile() label.compile()
@property @property
@ -435,17 +436,17 @@ class Artist(MainObject):
def compile(self): def compile(self):
song: "Song" song: "Song"
for song in self.feature_song_collection: for song in self.feature_song_collection:
if song.feature_artist_collection.insecure_append(self): if song.feature_artist_collection.append(self, merge_into_existing=False):
song.compile() song.compile()
album: "Album" album: "Album"
for album in self.main_album_collection: for album in self.main_album_collection:
if album.artist_collection.insecure_append(self): if album.artist_collection.append(self, merge_into_existing=False):
album.compile() album.compile()
label: Label label: Label
for label in self.label_collection: for label in self.label_collection:
if label.current_artist_collection.insecure_append(self): if label.current_artist_collection.append(self, merge_into_existing=False):
label.compile() label.compile()
@property @property
@ -579,12 +580,12 @@ class Label(MainObject):
def compile(self) -> bool: def compile(self) -> bool:
album: Album album: Album
for album in self.album_collection: for album in self.album_collection:
if album.label_collection.insecure_append(self): if album.label_collection.append(self, merge_into_existing=False):
album.compile() album.compile()
artist: Artist artist: Artist
for artist in self.current_artist_collection: for artist in self.current_artist_collection:
if artist.label_collection.insecure_append(self): if artist.label_collection.append(self, merge_into_existing=False):
artist.compile() artist.compile()
@property @property

View File

@ -20,20 +20,11 @@ from ..objects import (
) )
class PageCache(Collection):
def clear(self):
self.__init__(element_type=self.element_type)
class Page: class Page:
""" """
This is an abstract class, laying out the This is an abstract class, laying out the
functionality for every other class fetching something functionality for every other class fetching something
""" """
SONG_CACHE = PageCache(element_type=Song)
ALBUM_CACHE = PageCache(element_type=Album)
ARTIST_CACHE = PageCache(element_type=Artist)
API_SESSION: requests.Session = requests.Session() API_SESSION: requests.Session = requests.Session()
API_SESSION.proxies = shared.proxies API_SESSION.proxies = shared.proxies
TIMEOUT = 5 TIMEOUT = 5
@ -160,10 +151,6 @@ class Page:
tracklist of every album of the artist. tracklist of every album of the artist.
:return detailed_music_object: IT MODIFIES THE INPUT OBJ :return detailed_music_object: IT MODIFIES THE INPUT OBJ
""" """
cls.ARTIST_CACHE.clear()
cls.ALBUM_CACHE.clear()
cls.SONG_CACHE.clear()
if type(music_object) == Song: if type(music_object) == Song:
song = cls.fetch_song_details(music_object, flat=flat) song = cls.fetch_song_details(music_object, flat=flat)

View File

@ -151,11 +151,11 @@ class Musify(Page):
artist_thumbnail = image_soup.get("src") artist_thumbnail = image_soup.get("src")
return cls.ARTIST_CACHE.append(Artist( return Artist(
_id=_id, _id=_id,
name=name, name=name,
source_list=source_list source_list=source_list
)) )
@classmethod @classmethod
def parse_album_contact(cls, contact: BeautifulSoup) -> Album: def parse_album_contact(cls, contact: BeautifulSoup) -> Album:
@ -700,13 +700,13 @@ class Musify(Page):
if note_soup is not None: if note_soup is not None:
notes.html = note_soup.decode_contents() notes.html = note_soup.decode_contents()
return cls.ARTIST_CACHE.append(Artist( return Artist(
_id=url.musify_id, _id=url.musify_id,
name=name, name=name,
country=country, country=country,
source_list=source_list, source_list=source_list,
notes=notes notes=notes
)) )
@classmethod @classmethod
def fetch_artist_from_source(cls, source: Source, flat: bool = False) -> Artist: def fetch_artist_from_source(cls, source: Source, flat: bool = False) -> Artist:
@ -842,7 +842,7 @@ class Musify(Page):
_artist_name = meta_artist_name_text _artist_name = meta_artist_name_text
if _artist_name is not None or _artist_src is not None: if _artist_name is not None or _artist_src is not None:
artist_list.append(cls.ARTIST_CACHE.append(Artist(name=_artist_name, source_list=_artist_src))) artist_list.append(Artist(name=_artist_name, source_list=_artist_src))
return Song( return Song(
title=song_name, title=song_name,