music-kraken-core/src/music_kraken/download/page_attributes.py

113 lines
4.5 KiB
Python
Raw Normal View History

2023-08-30 19:14:03 +00:00
from typing import Tuple, Type, Dict, Set
2023-03-28 10:50:23 +00:00
from .results import SearchResults
from ..objects import DatabaseObject, Source
2023-08-30 19:14:03 +00:00
2023-05-23 11:59:24 +00:00
from ..utils.enums.source import SourcePages
from ..utils.support_classes import Query, DownloadResult
from ..utils.exception.download import UrlNotFoundException
2023-09-12 08:58:44 +00:00
from ..utils.shared import DEBUG_PAGES
2023-08-30 19:14:03 +00:00
from ..pages import Page, EncyclopaediaMetallum, Musify, YouTube, YoutubeMusic, INDEPENDENT_DB_OBJECTS
2023-09-12 08:58:44 +00:00
if DEBUG_PAGES:
DEBUGGING_PAGE = YoutubeMusic
2023-05-23 11:59:24 +00:00
2023-05-26 08:11:36 +00:00
ALL_PAGES: Set[Type[Page]] = {
EncyclopaediaMetallum,
2023-06-12 19:53:40 +00:00
Musify,
YouTube,
YoutubeMusic
2023-05-26 08:11:36 +00:00
}
2023-05-26 08:11:36 +00:00
AUDIO_PAGES: Set[Type[Page]] = {
Musify,
2023-06-12 19:53:40 +00:00
YouTube,
YoutubeMusic
2023-05-26 08:11:36 +00:00
}
2023-05-26 08:11:36 +00:00
SHADY_PAGES: Set[Type[Page]] = {
Musify,
2023-05-26 08:11:36 +00:00
}
if DEBUGGING_PAGE is not None:
print(f"The DEBUGGING_PAGE is not None, but {DEBUGGING_PAGE}. Only using this page")
ALL_PAGES = {DEBUGGING_PAGE}
AUDIO_PAGES = ALL_PAGES.union(AUDIO_PAGES)
2023-05-26 08:11:36 +00:00
class Pages:
def __init__(self, exclude_pages: Set[Type[Page]] = None, exclude_shady: bool = False) -> None:
# initialize all page instances
self._page_instances: Dict[Type[Page], Page] = dict()
self._source_to_page: Dict[SourcePages, Type[Page]] = dict()
2023-05-26 08:11:36 +00:00
exclude_pages = exclude_pages if exclude_pages is not None else set()
if exclude_shady:
exclude_pages = exclude_pages.union(SHADY_PAGES)
if not exclude_pages.issubset(ALL_PAGES):
raise ValueError(f"The excluded pages have to be a subset of all pages: {exclude_pages} | {ALL_PAGES}")
def _set_to_tuple(page_set: Set[Type[Page]]) -> Tuple[Type[Page], ...]:
return tuple(sorted(page_set, key=lambda page: page.__name__))
self._pages_set: Set[Type[Page]] = ALL_PAGES.difference(exclude_pages)
2023-06-12 12:56:14 +00:00
self.pages: Tuple[Type[Page], ...] = _set_to_tuple(self._pages_set)
self._audio_pages_set: Set[Type[Page]] = self._pages_set.intersection(AUDIO_PAGES)
self.audio_pages: Tuple[Type[Page], ...] = _set_to_tuple(self._audio_pages_set)
2023-05-26 08:11:36 +00:00
for page_type in self.pages:
2023-05-26 08:11:36 +00:00
self._page_instances[page_type] = page_type()
self._source_to_page[page_type.SOURCE_TYPE] = page_type
2023-05-26 08:11:36 +00:00
def search(self, query: Query) -> SearchResults:
result = SearchResults()
2023-05-26 08:11:36 +00:00
for page_type in self.pages:
result.add(
page=page_type,
search_result=self._page_instances[page_type].search(query=query)
)
return result
def fetch_details(self, music_object: DatabaseObject, stop_at_level: int = 1) -> DatabaseObject:
if not isinstance(music_object, INDEPENDENT_DB_OBJECTS):
return music_object
for source_page in music_object.source_collection.source_pages:
if source_page not in self._source_to_page:
continue
page_type = self._source_to_page[source_page]
if page_type in self._pages_set:
music_object.merge(self._page_instances[page_type].fetch_details(music_object=music_object, stop_at_level=stop_at_level))
return music_object
2023-06-20 17:30:48 +00:00
def download(self, music_object: DatabaseObject, genre: str, download_all: bool = False, process_metadata_anyway: bool = False) -> DownloadResult:
if not isinstance(music_object, INDEPENDENT_DB_OBJECTS):
return DownloadResult(error_message=f"{type(music_object).__name__} can't be downloaded.")
_page_types = set(self._source_to_page[src] for src in music_object.source_collection.source_pages)
audio_pages = self._audio_pages_set.intersection(_page_types)
for download_page in audio_pages:
2023-06-20 17:30:48 +00:00
return self._page_instances[download_page].download(music_object=music_object, genre=genre, download_all=download_all, process_metadata_anyway=process_metadata_anyway)
return DownloadResult(error_message=f"No audio source has been found for {music_object}.")
2023-06-12 15:40:54 +00:00
def fetch_url(self, url: str, stop_at_level: int = 2) -> Tuple[Type[Page], DatabaseObject]:
source = Source.match_url(url, SourcePages.MANUAL)
if source is None:
raise UrlNotFoundException(url=url)
_actual_page = self._source_to_page[source.page_enum]
2023-06-12 15:40:54 +00:00
return _actual_page, self._page_instances[_actual_page].fetch_object_from_source(source=source, stop_at_level=stop_at_level)