From 8500b714a2b6f0960a3957462d6eac8e0cc3da91 Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Tue, 23 May 2023 17:53:03 +0200 Subject: [PATCH] rewritten general search for me --- src/music_kraken/download/download.py | 15 +++++++----- src/music_kraken/download/multiple_options.py | 4 ++-- src/music_kraken/download/page_attributes.py | 23 +++++++++---------- src/music_kraken/download/search.py | 16 ++++++------- .../pages/encyclopaedia_metallum.py | 5 ++-- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/music_kraken/download/download.py b/src/music_kraken/download/download.py index febdbf6..195b04c 100644 --- a/src/music_kraken/download/download.py +++ b/src/music_kraken/download/download.py @@ -10,12 +10,15 @@ MusicObject = Union[Song, Album, Artist, Label] class Download: def __init__( self, - pages: Tuple[Type[Page]] = page_attributes.ALL_PAGES, - exclude_pages: Set[Type[Page]] = set(), + pages: Tuple[Page] = page_attributes.ALL_PAGES, + exclude_pages=None, exclude_shady: bool = False, ) -> None: - _page_list: List[Type[Page]] = [] - _audio_page_list: List[Type[Page]] = [] + if exclude_pages is None: + exclude_pages = set() + + _page_list: List[Page] = [] + _audio_page_list: List[Page] = [] for page in pages: if exclude_shady and page in page_attributes.SHADY_PAGES: @@ -28,8 +31,8 @@ class Download: if page in page_attributes.AUDIO_PAGES: _audio_page_list.append(page) - self.pages: Tuple[Type[Page]] = tuple(_page_list) - self.audio_pages: Tuple[Type[Page]] = tuple(_audio_page_list) + self.pages: Tuple[Page] = tuple(_page_list) + self.audio_pages: Tuple[Page] = tuple(_audio_page_list) def fetch_details(self, music_object: MusicObject) -> MusicObject: for page in self.pages: diff --git a/src/music_kraken/download/multiple_options.py b/src/music_kraken/download/multiple_options.py index d2d99c0..4585952 100644 --- a/src/music_kraken/download/multiple_options.py +++ b/src/music_kraken/download/multiple_options.py @@ -34,11 +34,11 @@ class MultiPageOptions: def __len__(self) -> int: return self._length - def get_page_str(self, page: Type[Page]) -> str: + def get_page_str(self, page: Page) -> str: page_name_fill = "-" max_page_len = 21 - return f"({page_attributes.PAGE_NAME_MAP[page]}) ------------------------{page.__name__:{page_name_fill}<{max_page_len}}------------" + return f"({page_attributes.PAGE_NAME_MAP[page]}) ------------------------{type(page).__name__:{page_name_fill}<{max_page_len}}------------" def string_from_all_pages(self) -> str: if self._length == 1: diff --git a/src/music_kraken/download/page_attributes.py b/src/music_kraken/download/page_attributes.py index 56e18a4..e51afec 100644 --- a/src/music_kraken/download/page_attributes.py +++ b/src/music_kraken/download/page_attributes.py @@ -4,27 +4,26 @@ from ..utils.enums.source import SourcePages from ..pages import Page, EncyclopaediaMetallum, Musify -NAME_PAGE_MAP: Dict[str, Type[Page]] = dict() -PAGE_NAME_MAP: Dict[Type[Page], str] = dict() -SOURCE_PAGE_MAP: Dict[SourcePages, Type[Page]] = dict() +NAME_PAGE_MAP: Dict[str, Page] = dict() +PAGE_NAME_MAP: Dict[Page, str] = dict() +SOURCE_PAGE_MAP: Dict[SourcePages, Page] = dict() -ALL_PAGES: Tuple[Type[Page]] = ( - EncyclopaediaMetallum, - Musify +ALL_PAGES: Tuple[Page, ...] = ( + EncyclopaediaMetallum(), ) -AUDIO_PAGES: Tuple[Type[Page]] = ( - Musify, +AUDIO_PAGES: Tuple[Page, ...] = ( + Musify(), ) -SHADY_PAGES: Tuple[Type[Page]] = ( - Musify, +SHADY_PAGES: Tuple[Page, ...] = ( + Musify(), ) -# this needs to be case insensitive +# this needs to be case-insensitive SHORTHANDS = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') for i, page in enumerate(ALL_PAGES): - NAME_PAGE_MAP[page.__name__.lower()] = page + NAME_PAGE_MAP[type(page).__name__.lower()] = page NAME_PAGE_MAP[SHORTHANDS[i].lower()] = page PAGE_NAME_MAP[page] = SHORTHANDS[i] diff --git a/src/music_kraken/download/search.py b/src/music_kraken/download/search.py index f7a0ddf..ba053c5 100644 --- a/src/music_kraken/download/search.py +++ b/src/music_kraken/download/search.py @@ -94,7 +94,7 @@ class Search(Download): new_text = "" latest_key: str = None for i in range(len(query) - 1): - currenct_char = query[i] + current_char = query[i] next_char = query[i+1] if skip_next: @@ -102,24 +102,25 @@ class Search(Download): continue if escape_next: - new_text += currenct_char + new_text += current_char escape_next = False # escaping - if currenct_char == "\\": + if current_char == "\\": if next_char in special_characters: escape_next = True continue - if currenct_char == "#": + if current_char == "#": if latest_key is not None: - key_text[latest_key] + key_text[latest_key] = new_text + new_text = "" latest_key = next_char skip_next = True continue - new_text += currenct_char + new_text += current_char if latest_key is not None: key_text[latest_key] = new_text @@ -129,8 +130,7 @@ class Search(Download): for page in self.pages: - for search in parsed_query.default_search: - self._current_option[page].extend(page._raw_search(query=search)) + self._current_option[page].extend(page.search(parsed_query)) def choose_page(self, page: Type[Page]): """ diff --git a/src/music_kraken/pages/encyclopaedia_metallum.py b/src/music_kraken/pages/encyclopaedia_metallum.py index 012351a..8a57fae 100644 --- a/src/music_kraken/pages/encyclopaedia_metallum.py +++ b/src/music_kraken/pages/encyclopaedia_metallum.py @@ -35,8 +35,7 @@ ALBUM_TYPE_MAP: Dict[str, AlbumType] = defaultdict(lambda: AlbumType.OTHER, { }) -def _song_from_json(artist_html=None, album_html=None, release_type=None, title=None, - lyrics_html=None) -> Song: +def _song_from_json(artist_html=None, album_html=None, release_type=None, title=None, lyrics_html=None) -> Song: song_id = None if lyrics_html is not None: soup = BeautifulSoup(lyrics_html, 'html.parser') @@ -60,7 +59,7 @@ def _song_from_json(artist_html=None, album_html=None, release_type=None, title= def _artist_from_json(artist_html=None, genre=None, country=None) -> Artist: """ - TODO parse the country to a standart + TODO parse the country to a standard """ # parse the html # parse the html for the band name and link on metal-archives