added classes to querry through the results
This commit is contained in:
parent
ff5a79a3c7
commit
58dbb5a9c7
@ -1,30 +0,0 @@
|
|||||||
from typing import Optional, Tuple, Type, Set, Union, List
|
|
||||||
|
|
||||||
from .page_attributes import Pages
|
|
||||||
from ..pages import Page
|
|
||||||
from ..objects import Song, Album, Artist, Label, Source
|
|
||||||
|
|
||||||
MusicObject = Union[Song, Album, Artist, Label]
|
|
||||||
|
|
||||||
|
|
||||||
class Download:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
exclude_pages: Set[Type[Page]] = None,
|
|
||||||
exclude_shady: bool = False
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
self.pages: Pages = Pages(exclude_pages=exclude_pages, exclude_shady=exclude_shady)
|
|
||||||
|
|
||||||
def fetch_details(self, music_object: MusicObject) -> MusicObject:
|
|
||||||
for page in self.pages:
|
|
||||||
page.fetch_details(music_object=music_object)
|
|
||||||
return music_object
|
|
||||||
|
|
||||||
def fetch_source(self, source: Source) -> Optional[MusicObject]:
|
|
||||||
source_page = page_attributes.SOURCE_PAGE_MAP[source.page_enum]
|
|
||||||
|
|
||||||
if source_page not in self.pages:
|
|
||||||
return
|
|
||||||
|
|
||||||
return source_page.fetch_object_from_source(source)
|
|
@ -1,15 +1,42 @@
|
|||||||
from typing import Tuple, Type, Dict, List
|
from typing import Tuple, Type, Dict, List, Generator
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from ..objects import DatabaseObject
|
from ..objects import DatabaseObject
|
||||||
from ..utils.enums.source import SourcePages
|
from ..utils.enums.source import SourcePages
|
||||||
from ..pages import Page, EncyclopaediaMetallum, Musify
|
from ..pages import Page, EncyclopaediaMetallum, Musify
|
||||||
|
|
||||||
class SearchResults:
|
|
||||||
|
@dataclass
|
||||||
|
class Option:
|
||||||
|
index: int
|
||||||
|
music_object: DatabaseObject
|
||||||
|
|
||||||
|
|
||||||
|
class Results:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._by_index = Dict[int, DatabaseObject] = dict()
|
||||||
|
|
||||||
|
def __iter__(self) -> Generator[DatabaseObject]:
|
||||||
|
for option in self.formated_generator():
|
||||||
|
if isinstance(option, Option):
|
||||||
|
yield option.music_object
|
||||||
|
|
||||||
|
def formated_generator(self, max_items_per_page: int = 10) -> Generator[Type[Page], Option]:
|
||||||
|
self._by_index = dict()
|
||||||
|
|
||||||
|
def get_music_object_by_index(self, index: int) -> DatabaseObject:
|
||||||
|
# if this throws a key error, either the formated generator needs to be iterated, or the option doesn't exist.
|
||||||
|
return self._by_index[index]
|
||||||
|
|
||||||
|
|
||||||
|
class SearchResults(Results):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
pages: Tuple[Type[Page], ...]
|
pages: Tuple[Type[Page], ...]
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
super().__init()
|
||||||
|
|
||||||
self.pages = pages
|
self.pages = pages
|
||||||
# this would initialize a list for every page, which I don't think I want
|
# this would initialize a list for every page, which I don't think I want
|
||||||
# self.results = Dict[Type[Page], List[DatabaseObject]] = {page: [] for page in self.pages}
|
# self.results = Dict[Type[Page], List[DatabaseObject]] = {page: [] for page in self.pages}
|
||||||
@ -23,8 +50,41 @@ class SearchResults:
|
|||||||
|
|
||||||
self.results[page] = search_result
|
self.results[page] = search_result
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def get_page_results(self, page: Type[Page]) -> "PageResults":
|
||||||
for page in self.pages:
|
return PageResults(page, self.results.get(page, []))
|
||||||
if page not in self.results:
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
def formated_generator(self, max_items_per_page: int = 10):
|
||||||
|
super().formated_generator()
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
for page in self.results:
|
||||||
|
yield page
|
||||||
|
|
||||||
|
j = 0
|
||||||
|
for option in self.results[page]:
|
||||||
|
yield Option(i, option)
|
||||||
|
self._by_index[i] = option
|
||||||
|
i += 1
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
if j >= max_items_per_page:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
class PageResults(Results):
|
||||||
|
def __init__(self, page: Type[Page], results: List[DatabaseObject]) -> None:
|
||||||
|
super().__init()
|
||||||
|
|
||||||
|
self.page: Type[Page] = page
|
||||||
|
self.results: List[DatabaseObject] = results
|
||||||
|
|
||||||
|
def formated_generator(self, max_items_per_page: int = 10):
|
||||||
|
super().formated_generator()
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
yield self.page
|
||||||
|
|
||||||
|
for option in self.results:
|
||||||
|
yield Option(i, option)
|
||||||
|
self._by_index[i] = option
|
||||||
|
i += 1
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from typing import Tuple, List, Set, Type, Optional, Dict
|
from typing import Tuple, List, Set, Type, Optional, Dict
|
||||||
|
|
||||||
from . import page_attributes
|
from .page_attributes import Pages
|
||||||
from .download import Download
|
|
||||||
from .multiple_options import MultiPageOptions
|
from .multiple_options import MultiPageOptions
|
||||||
from ..pages.abstract import Page
|
from ..pages.abstract import Page
|
||||||
from ..utils.support_classes import DownloadResult, Query
|
from ..utils.support_classes import DownloadResult, Query
|
||||||
@ -9,20 +8,15 @@ from ..objects import DatabaseObject, Source, Artist, Song, Album
|
|||||||
from ..utils.enums.source import SourcePages
|
from ..utils.enums.source import SourcePages
|
||||||
|
|
||||||
|
|
||||||
class Search(Download):
|
class Search:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
pages: Tuple[Type[Page]] = page_attributes.ALL_PAGES,
|
exclude_pages: Set[Type[Page]] = None,
|
||||||
exclude_pages: Set[Type[Page]] = set(),
|
|
||||||
exclude_shady: bool = False,
|
exclude_shady: bool = False,
|
||||||
max_displayed_options: int = 10,
|
max_displayed_options: int = 10,
|
||||||
option_digits: int = 3,
|
option_digits: int = 3,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(
|
self.pages: Pages = Pages(exclude_pages=exclude_pages, exclude_shady=exclude_shady)
|
||||||
pages=pages,
|
|
||||||
exclude_pages=exclude_pages,
|
|
||||||
exclude_shady=exclude_shady
|
|
||||||
)
|
|
||||||
|
|
||||||
self.max_displayed_options = max_displayed_options
|
self.max_displayed_options = max_displayed_options
|
||||||
self.option_digits: int = option_digits
|
self.option_digits: int = option_digits
|
||||||
|
Loading…
Reference in New Issue
Block a user