rewritten general search for me

This commit is contained in:
Hellow 2023-05-23 17:53:03 +02:00
parent 70e0a16b20
commit 8500b714a2
5 changed files with 32 additions and 31 deletions

View File

@ -10,12 +10,15 @@ MusicObject = Union[Song, Album, Artist, Label]
class Download: class Download:
def __init__( def __init__(
self, self,
pages: Tuple[Type[Page]] = page_attributes.ALL_PAGES, pages: Tuple[Page] = page_attributes.ALL_PAGES,
exclude_pages: Set[Type[Page]] = set(), exclude_pages=None,
exclude_shady: bool = False, exclude_shady: bool = False,
) -> None: ) -> None:
_page_list: List[Type[Page]] = [] if exclude_pages is None:
_audio_page_list: List[Type[Page]] = [] exclude_pages = set()
_page_list: List[Page] = []
_audio_page_list: List[Page] = []
for page in pages: for page in pages:
if exclude_shady and page in page_attributes.SHADY_PAGES: if exclude_shady and page in page_attributes.SHADY_PAGES:
@ -28,8 +31,8 @@ class Download:
if page in page_attributes.AUDIO_PAGES: if page in page_attributes.AUDIO_PAGES:
_audio_page_list.append(page) _audio_page_list.append(page)
self.pages: Tuple[Type[Page]] = tuple(_page_list) self.pages: Tuple[Page] = tuple(_page_list)
self.audio_pages: Tuple[Type[Page]] = tuple(_audio_page_list) self.audio_pages: Tuple[Page] = tuple(_audio_page_list)
def fetch_details(self, music_object: MusicObject) -> MusicObject: def fetch_details(self, music_object: MusicObject) -> MusicObject:
for page in self.pages: for page in self.pages:

View File

@ -34,11 +34,11 @@ class MultiPageOptions:
def __len__(self) -> int: def __len__(self) -> int:
return self._length return self._length
def get_page_str(self, page: Type[Page]) -> str: def get_page_str(self, page: Page) -> str:
page_name_fill = "-" page_name_fill = "-"
max_page_len = 21 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: def string_from_all_pages(self) -> str:
if self._length == 1: if self._length == 1:

View File

@ -4,27 +4,26 @@ from ..utils.enums.source import SourcePages
from ..pages import Page, EncyclopaediaMetallum, Musify from ..pages import Page, EncyclopaediaMetallum, Musify
NAME_PAGE_MAP: Dict[str, Type[Page]] = dict() NAME_PAGE_MAP: Dict[str, Page] = dict()
PAGE_NAME_MAP: Dict[Type[Page], str] = dict() PAGE_NAME_MAP: Dict[Page, str] = dict()
SOURCE_PAGE_MAP: Dict[SourcePages, Type[Page]] = dict() SOURCE_PAGE_MAP: Dict[SourcePages, Page] = dict()
ALL_PAGES: Tuple[Type[Page]] = ( ALL_PAGES: Tuple[Page, ...] = (
EncyclopaediaMetallum, EncyclopaediaMetallum(),
Musify
) )
AUDIO_PAGES: Tuple[Type[Page]] = ( AUDIO_PAGES: Tuple[Page, ...] = (
Musify, Musify(),
) )
SHADY_PAGES: Tuple[Type[Page]] = ( SHADY_PAGES: Tuple[Page, ...] = (
Musify, 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') 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): 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 NAME_PAGE_MAP[SHORTHANDS[i].lower()] = page
PAGE_NAME_MAP[page] = SHORTHANDS[i] PAGE_NAME_MAP[page] = SHORTHANDS[i]

View File

@ -94,7 +94,7 @@ class Search(Download):
new_text = "" new_text = ""
latest_key: str = None latest_key: str = None
for i in range(len(query) - 1): for i in range(len(query) - 1):
currenct_char = query[i] current_char = query[i]
next_char = query[i+1] next_char = query[i+1]
if skip_next: if skip_next:
@ -102,24 +102,25 @@ class Search(Download):
continue continue
if escape_next: if escape_next:
new_text += currenct_char new_text += current_char
escape_next = False escape_next = False
# escaping # escaping
if currenct_char == "\\": if current_char == "\\":
if next_char in special_characters: if next_char in special_characters:
escape_next = True escape_next = True
continue continue
if currenct_char == "#": if current_char == "#":
if latest_key is not None: if latest_key is not None:
key_text[latest_key] key_text[latest_key] = new_text
new_text = ""
latest_key = next_char latest_key = next_char
skip_next = True skip_next = True
continue continue
new_text += currenct_char new_text += current_char
if latest_key is not None: if latest_key is not None:
key_text[latest_key] = new_text key_text[latest_key] = new_text
@ -129,8 +130,7 @@ class Search(Download):
for page in self.pages: for page in self.pages:
for search in parsed_query.default_search: self._current_option[page].extend(page.search(parsed_query))
self._current_option[page].extend(page._raw_search(query=search))
def choose_page(self, page: Type[Page]): def choose_page(self, page: Type[Page]):
""" """

View File

@ -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, def _song_from_json(artist_html=None, album_html=None, release_type=None, title=None, lyrics_html=None) -> Song:
lyrics_html=None) -> Song:
song_id = None song_id = None
if lyrics_html is not None: if lyrics_html is not None:
soup = BeautifulSoup(lyrics_html, 'html.parser') 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: 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
# parse the html for the band name and link on metal-archives # parse the html for the band name and link on metal-archives