fix: fixed uglyyy typo

This commit is contained in:
Hazel 2023-12-20 12:31:53 +01:00
parent 15841ee079
commit 695c9f62b9
4 changed files with 51 additions and 41 deletions

View File

@ -130,7 +130,6 @@ for _id, _object in objects_by_id.items():
print(only_smile) print(only_smile)
"""
c = Collection([Song(title="hi"), Song(title="hi2"), Song(title="hi3")]) c = Collection([Song(title="hi"), Song(title="hi2"), Song(title="hi3")])
c1 = Collection([Song(title="he"), Song(title="hi5")]) c1 = Collection([Song(title="he"), Song(title="hi5")])
c11 = Collection([Song(title="wow how ultra subby", isrc="hiii")]) c11 = Collection([Song(title="wow how ultra subby", isrc="hiii")])
@ -179,4 +178,3 @@ print("b: ", b)
print(c.data) print(c.data)
print(c._data) print(c._data)
"""

View File

@ -4,6 +4,7 @@ from collections import defaultdict
from typing import TypeVar, Generic, Dict, Optional, Iterable, List, Iterator from typing import TypeVar, Generic, Dict, Optional, Iterable, List, Iterator
from .parents import OuterProxy from .parents import OuterProxy
T = TypeVar('T', bound=OuterProxy) T = TypeVar('T', bound=OuterProxy)
@ -34,7 +35,6 @@ class Collection(Generic[T]):
# List of collection attributes that should be modified on append # List of collection attributes that should be modified on append
# Key: collection attribute (str) of appended element # Key: collection attribute (str) of appended element
# Value: main collection to sync to # Value: main collection to sync to
self.sync_on_append: Dict[str, Collection] = sync_on_append or {}
self.contain_given_in_attribute: Dict[str, Collection] = contain_given_in_attribute or {} self.contain_given_in_attribute: Dict[str, Collection] = contain_given_in_attribute or {}
self.contain_attribute_in_given: Dict[str, Collection] = contain_attribute_in_given or {} self.contain_attribute_in_given: Dict[str, Collection] = contain_attribute_in_given or {}
self.append_object_to_attribute: Dict[str, T] = append_object_to_attribute or {} self.append_object_to_attribute: Dict[str, T] = append_object_to_attribute or {}
@ -60,11 +60,11 @@ class Collection(Generic[T]):
for attribute, new_object in self.contain_given_in_attribute.items(): for attribute, new_object in self.contain_given_in_attribute.items():
__object.__getattribute__(attribute).contain_collection_inside(new_object) __object.__getattribute__(attribute).contain_collection_inside(new_object)
for attribute, new_object in self.contain_given_in_attribute.items(): for attribute, new_object in self.contain_attribute_in_given.items():
new_object.contain_collection_inside(__object.__getattribute__(attribute)) new_object.contain_collection_inside(__object.__getattribute__(attribute))
for attribute, new_object in self.append_object_to_attribute.items(): for attribute, new_object in self.append_object_to_attribute.items():
__object.__getattribute__(attribute).append(new_object, from_map=True) __object.__getattribute__(attribute).append(new_object)
def _unmap_element(self, __object: T): def _unmap_element(self, __object: T):
self._contains_ids.remove(__object.id) self._contains_ids.remove(__object.id)
@ -94,6 +94,29 @@ class Collection(Generic[T]):
return True return True
return False return False
def _contained_in_sub(self, __object: T, break_at_first: bool = True) -> List[Collection]:
"""
Gets the collection this object is found in, if it is found in any.
:param __object:
:param break_at_first:
:return:
"""
results = []
if self._contained_in_self(__object):
return [self]
print(len(self.children), id(self), ";".join(str(id(i)) for i in self.children))
print()
for collection in self.children:
results.extend(collection._contained_in_sub(__object, break_at_first=break_at_first))
if break_at_first:
return results
return results
def _get_root_collections(self) -> List[Collection]: def _get_root_collections(self) -> List[Collection]:
if not len(self.parents): if not len(self.parents):
return [self] return [self]
@ -107,19 +130,6 @@ class Collection(Generic[T]):
def _is_root(self) -> bool: def _is_root(self) -> bool:
return len(self.parents) <= 0 return len(self.parents) <= 0
def _contained_in_sub(self, __object: T, break_at_first: bool = True) -> List[Collection]:
results = []
if self._contained_in_self(__object):
return [self]
for collection in self.children:
results.extend(collection._contained_in_sub(__object, break_at_first=break_at_first))
if break_at_first:
return results
return results
def _get_parents_of_multiple_contained_children(self, __object: T): def _get_parents_of_multiple_contained_children(self, __object: T):
results = [] results = []
if len(self.children) < 2 or self._contained_in_self(__object): if len(self.children) < 2 or self._contained_in_self(__object):
@ -153,6 +163,7 @@ class Collection(Generic[T]):
for name, value in __object.indexing_values: for name, value in __object.indexing_values:
if value is None: if value is None:
continue continue
if value in self._indexed_values[name]: if value in self._indexed_values[name]:
existing_object = self._indexed_to_objects[value][0] existing_object = self._indexed_to_objects[value][0]
if existing_object.id == __object.id: if existing_object.id == __object.id:
@ -175,18 +186,16 @@ class Collection(Generic[T]):
return len(self._contained_in_sub(__object)) > 0 return len(self._contained_in_sub(__object)) > 0
def _append(self, __object: T, from_map: bool = False): def _append(self, __object: T, from_map: bool = False):
for attribute, to_sync_with in self.sync_on_append.items():
to_sync_with.sync_with_other_collection(__object.__getattribute__(attribute))
self._map_element(__object, from_map=from_map) self._map_element(__object, from_map=from_map)
self._data.append(__object) self._data.append(__object)
def append(self, __object: Optional[T], already_is_parent: bool = False, from_map: bool = False): def append(self, __object: Optional[T], already_is_parent: bool = False, from_map: bool = False):
print(__object)
if __object is None or __object.id in self._contains_ids: if __object is None or __object.id in self._contains_ids:
return return
exists_in_collection = self._contained_in_sub(__object) exists_in_collection = self._contained_in_sub(__object)
if len(exists_in_collection) > 0 and self is exists_in_collection[0]: if len(exists_in_collection) and self is exists_in_collection[0]:
# assuming that the object already is contained in the correct collections # assuming that the object already is contained in the correct collections
if not already_is_parent: if not already_is_parent:
self.merge_into_self(__object, from_map=from_map) self.merge_into_self(__object, from_map=from_map)
@ -230,15 +239,11 @@ class Collection(Generic[T]):
for equal_sub_collection in equal_collection.children: for equal_sub_collection in equal_collection.children:
self.contain_collection_inside(equal_sub_collection) self.contain_collection_inside(equal_sub_collection)
# now the ugly part def contain_collection_inside(self, sub_collection: Collection):
# replace all refs of the other element with this one
self = self._risky_merge(equal_collection)
def contain_collection_inside(self, sub_collection: "Collection"):
""" """
This collection will ALWAYS contain everything from the passed in collection This collection will ALWAYS contain everything from the passed in collection
""" """
if sub_collection in self.children: if self is sub_collection or sub_collection in self.children:
return return
self.children.append(sub_collection) self.children.append(sub_collection)
@ -257,5 +262,5 @@ class Collection(Generic[T]):
return self.__len__() == 0 return self.__len__() == 0
def __iter__(self) -> Iterator[T]: def __iter__(self) -> Iterator[T]:
for element in self.data: for element in self._data:
yield element yield element

View File

@ -15,7 +15,6 @@ from ...objects import (
) )
from ._music_object_render import parse_run_list, parse_run_element from ._music_object_render import parse_run_list, parse_run_element
LOGGER = logging_settings["youtube_music_logger"] LOGGER = logging_settings["youtube_music_logger"]
@ -23,20 +22,24 @@ def music_card_shelf_renderer(renderer: dict) -> List[DatabaseObject]:
results = parse_run_list(renderer.get("title", {}).get("runs", [])) results = parse_run_list(renderer.get("title", {}).get("runs", []))
for sub_renderer in renderer.get("contents", []): for sub_renderer in renderer.get("contents", []):
results.extend(parse_renderer(sub_renderer)) results.extend(parse_renderer(sub_renderer))
return results return results
def music_responsive_list_item_flex_column_renderer(renderer: dict) -> List[DatabaseObject]: def music_responsive_list_item_flex_column_renderer(renderer: dict) -> List[DatabaseObject]:
return parse_run_list(renderer.get("text", {}).get("runs", [])) return parse_run_list(renderer.get("text", {}).get("runs", []))
def music_responsive_list_item_renderer(renderer: dict) -> List[DatabaseObject]: def music_responsive_list_item_renderer(renderer: dict) -> List[DatabaseObject]:
results = [] results = []
for i, collumn in enumerate(renderer.get("flexColumns", [])): for i, column in enumerate(renderer.get("flexColumns", [])):
_r = parse_renderer(collumn) _r = parse_renderer(column)
if i == 0 and len(_r) == 0: if i == 0 and len(_r) == 0:
renderer["text"] = collumn.get("musicResponsiveListItemFlexColumnRenderer", {}).get("text", {}).get("runs", [{}])[0].get("text") renderer["text"] = \
column.get("musicResponsiveListItemFlexColumnRenderer", {}).get("text", {}).get("runs", [{}])[0].get(
"text")
results.extend(_r) results.extend(_r)
_r = parse_run_element(renderer) _r = parse_run_element(renderer)
@ -54,7 +57,7 @@ def music_responsive_list_item_renderer(renderer: dict) -> List[DatabaseObject]:
for song in song_list: for song in song_list:
song.album_collection.extend(album_list) song.album_collection.extend(album_list)
song.main_artist_collection.extend(artist_list) song.main_artist_collection.extend(artist_list)
for album in album_list: for album in album_list:
album.artist_collection.extend(artist_list) album.artist_collection.extend(artist_list)
@ -64,19 +67,22 @@ def music_responsive_list_item_renderer(renderer: dict) -> List[DatabaseObject]:
return album_list return album_list
if len(artist_list) > 0: if len(artist_list) > 0:
return artist_list return artist_list
return results return results
def music_shelf_renderer(renderer: dict) -> List[DatabaseObject]: def music_shelf_renderer(renderer: dict) -> List[DatabaseObject]:
result = [] result = []
for subrenderer in renderer.get("contents"): for subrenderer in renderer.get("contents"):
result.extend(parse_renderer(subrenderer)) result.extend(parse_renderer(subrenderer))
return result return result
def music_carousel_shelf_renderer(renderer: dict): def music_carousel_shelf_renderer(renderer: dict):
return music_shelf_renderer(renderer=renderer) return music_shelf_renderer(renderer=renderer)
def music_two_row_item_renderer(renderer: dict): def music_two_row_item_renderer(renderer: dict):
return parse_run_list(renderer.get("title", {}).get("runs", [])) return parse_run_list(renderer.get("title", {}).get("runs", []))
@ -92,6 +98,7 @@ RENDERER_PARSERS = {
"itemSectionRenderer": lambda _: [], "itemSectionRenderer": lambda _: [],
} }
def parse_renderer(renderer: dict) -> List[DatabaseObject]: def parse_renderer(renderer: dict) -> List[DatabaseObject]:
result: List[DatabaseObject] = [] result: List[DatabaseObject] = []
@ -99,7 +106,7 @@ def parse_renderer(renderer: dict) -> List[DatabaseObject]:
if renderer_name not in RENDERER_PARSERS: if renderer_name not in RENDERER_PARSERS:
LOGGER.warning(f"Can't parse the renderer {renderer_name}.") LOGGER.warning(f"Can't parse the renderer {renderer_name}.")
continue continue
result.extend(RENDERER_PARSERS[renderer_name](renderer)) result.extend(RENDERER_PARSERS[renderer_name](renderer))
return result return result

View File

@ -261,7 +261,7 @@ class YoutubeMusic(SuperYouTube):
results = [] results = []
""" """
cant use fixed indices, because if something has no entries, the list dissappears cant use fixed indices, because if something has no entries, the list disappears
instead I have to try parse everything, and just reject community playlists and profiles. instead I have to try parse everything, and just reject community playlists and profiles.
""" """