2023-08-30 19:14:03 +00:00
|
|
|
from typing import Tuple, Type, Dict, Set
|
2023-03-28 10:50:23 +00:00
|
|
|
|
2023-05-26 09:41:20 +00:00
|
|
|
from .results import SearchResults
|
2023-06-12 08:16:56 +00:00
|
|
|
from ..objects import DatabaseObject, Source
|
2023-08-30 19:14:03 +00:00
|
|
|
|
2023-09-12 09:16:25 +00:00
|
|
|
from ..utils.config import youtube_settings
|
2023-05-23 11:59:24 +00:00
|
|
|
from ..utils.enums.source import SourcePages
|
2023-10-23 14:21:44 +00:00
|
|
|
from ..utils.support_classes.download_result import DownloadResult
|
|
|
|
from ..utils.support_classes.query import Query
|
2023-06-12 08:16:56 +00:00
|
|
|
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
|
|
|
|
2023-09-12 12:17:58 +00:00
|
|
|
from ..pages import Page, EncyclopaediaMetallum, Musify, YouTube, YoutubeMusic, Bandcamp, INDEPENDENT_DB_OBJECTS
|
2023-09-12 08:58:44 +00:00
|
|
|
|
2023-05-23 11:59:24 +00:00
|
|
|
|
2023-05-26 08:11:36 +00:00
|
|
|
ALL_PAGES: Set[Type[Page]] = {
|
2023-05-26 07:00:02 +00:00
|
|
|
EncyclopaediaMetallum,
|
2023-06-12 19:53:40 +00:00
|
|
|
Musify,
|
2023-09-14 08:09:51 +00:00
|
|
|
YoutubeMusic,
|
2023-09-12 12:17:58 +00:00
|
|
|
Bandcamp
|
2023-05-26 08:11:36 +00:00
|
|
|
}
|
2023-03-28 12:04:15 +00:00
|
|
|
|
2023-09-12 09:16:25 +00:00
|
|
|
if youtube_settings["use_youtube_alongside_youtube_music"]:
|
|
|
|
ALL_PAGES.add(YouTube)
|
|
|
|
|
2023-05-26 08:11:36 +00:00
|
|
|
AUDIO_PAGES: Set[Type[Page]] = {
|
2023-05-26 07:00:02 +00:00
|
|
|
Musify,
|
2023-06-12 19:53:40 +00:00
|
|
|
YouTube,
|
2023-09-12 12:17:58 +00:00
|
|
|
YoutubeMusic,
|
|
|
|
Bandcamp
|
2023-05-26 08:11:36 +00:00
|
|
|
}
|
2023-03-28 12:04:15 +00:00
|
|
|
|
2023-05-26 08:11:36 +00:00
|
|
|
SHADY_PAGES: Set[Type[Page]] = {
|
2023-05-26 07:00:02 +00:00
|
|
|
Musify,
|
2023-05-26 08:11:36 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 12:17:58 +00:00
|
|
|
if DEBUG_PAGES:
|
|
|
|
DEBUGGING_PAGE = Bandcamp
|
|
|
|
print(f"Only downloading from page {DEBUGGING_PAGE}.")
|
|
|
|
|
|
|
|
ALL_PAGES = {DEBUGGING_PAGE}
|
|
|
|
AUDIO_PAGES = ALL_PAGES.union(AUDIO_PAGES)
|
|
|
|
|
2023-12-29 14:43:33 +00:00
|
|
|
|
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()
|
2023-05-26 09:41:20 +00:00
|
|
|
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__))
|
|
|
|
|
2023-05-26 09:41:20 +00:00
|
|
|
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)
|
2023-05-26 09:41:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-26 09:41:20 +00:00
|
|
|
for page_type in self.pages:
|
2023-05-26 08:11:36 +00:00
|
|
|
self._page_instances[page_type] = page_type()
|
2023-05-26 09:41:20 +00:00
|
|
|
self._source_to_page[page_type.SOURCE_TYPE] = page_type
|
2023-05-26 08:11:36 +00:00
|
|
|
|
2023-05-26 09:41:20 +00:00
|
|
|
def search(self, query: Query) -> SearchResults:
|
|
|
|
result = SearchResults()
|
|
|
|
|
2023-05-26 08:11:36 +00:00
|
|
|
for page_type in self.pages:
|
2023-05-26 09:41:20 +00:00
|
|
|
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:
|
2023-08-28 19:01:21 +00:00
|
|
|
if source_page not in self._source_to_page:
|
|
|
|
continue
|
|
|
|
|
2023-05-26 09:41:20 +00:00
|
|
|
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
|
2024-01-16 09:08:08 +00:00
|
|
|
|
|
|
|
def is_downloadable(self, music_object: DatabaseObject) -> bool:
|
|
|
|
_page_types = set(self._source_to_page)
|
|
|
|
for src in music_object.source_collection.source_pages:
|
|
|
|
if src in self._source_to_page:
|
|
|
|
_page_types.add(self._source_to_page[src])
|
|
|
|
|
|
|
|
audio_pages = self._audio_pages_set.intersection(_page_types)
|
|
|
|
return len(audio_pages) > 0
|
2023-05-26 09:41:20 +00:00
|
|
|
|
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:
|
2023-05-26 09:41:20 +00:00
|
|
|
if not isinstance(music_object, INDEPENDENT_DB_OBJECTS):
|
|
|
|
return DownloadResult(error_message=f"{type(music_object).__name__} can't be downloaded.")
|
2024-01-15 11:48:36 +00:00
|
|
|
|
|
|
|
self.fetch_details(music_object)
|
|
|
|
|
|
|
|
_page_types = set(self._source_to_page)
|
2023-09-13 14:01:01 +00:00
|
|
|
for src in music_object.source_collection.source_pages:
|
|
|
|
if src in self._source_to_page:
|
|
|
|
_page_types.add(self._source_to_page[src])
|
|
|
|
|
2023-05-26 09:41:20 +00:00
|
|
|
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)
|
2023-05-26 09:41:20 +00:00
|
|
|
|
|
|
|
return DownloadResult(error_message=f"No audio source has been found for {music_object}.")
|
2023-05-26 07:00:02 +00:00
|
|
|
|
2023-06-12 15:40:54 +00:00
|
|
|
def fetch_url(self, url: str, stop_at_level: int = 2) -> Tuple[Type[Page], DatabaseObject]:
|
2023-06-12 08:16:56 +00:00
|
|
|
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)
|