Lars Noack
0e6fe8187a
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
165 lines
6.2 KiB
Python
165 lines
6.2 KiB
Python
from typing import Tuple, Type, Dict, Set, Optional
|
|
|
|
from .results import SearchResults
|
|
from ..objects import DatabaseObject as DataObject, Source
|
|
|
|
from ..utils.config import youtube_settings
|
|
from ..utils.enums.source import SourcePages
|
|
from ..utils.support_classes.download_result import DownloadResult
|
|
from ..utils.support_classes.query import Query
|
|
from ..utils.exception.download import UrlNotFoundException
|
|
from ..utils.shared import DEBUG_PAGES
|
|
|
|
from ..pages import Page, EncyclopaediaMetallum, Musify, YouTube, YoutubeMusic, Bandcamp, INDEPENDENT_DB_OBJECTS
|
|
|
|
|
|
ALL_PAGES: Set[Type[Page]] = {
|
|
# EncyclopaediaMetallum,
|
|
Musify,
|
|
YoutubeMusic,
|
|
Bandcamp
|
|
}
|
|
|
|
if youtube_settings["use_youtube_alongside_youtube_music"]:
|
|
ALL_PAGES.add(YouTube)
|
|
|
|
AUDIO_PAGES: Set[Type[Page]] = {
|
|
Musify,
|
|
YouTube,
|
|
YoutubeMusic,
|
|
Bandcamp
|
|
}
|
|
|
|
SHADY_PAGES: Set[Type[Page]] = {
|
|
Musify,
|
|
}
|
|
|
|
fetch_map = {
|
|
Song: "fetch_song",
|
|
Album: "fetch_album",
|
|
Artist: "fetch_artist",
|
|
Label: "fetch_label",
|
|
}
|
|
|
|
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)
|
|
|
|
|
|
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()
|
|
|
|
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)
|
|
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)
|
|
|
|
for page_type in self.pages:
|
|
self._page_instances[page_type] = page_type()
|
|
self._source_to_page[page_type.SOURCE_TYPE] = page_type
|
|
|
|
def _get_page_from_enum(self, source_page: SourcePages) -> Page:
|
|
if source_page not in self._source_to_page:
|
|
return None
|
|
return self._page_instances[self._source_to_page[source_page]]
|
|
|
|
def search(self, query: Query) -> SearchResults:
|
|
result = SearchResults()
|
|
|
|
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, data_object: DataObject, stop_at_level: int = 1) -> DataObject:
|
|
if not isinstance(data_object, INDEPENDENT_DB_OBJECTS):
|
|
return data_object
|
|
|
|
source: Source
|
|
for source in data_object.source_collection.get_sources():
|
|
new_data_object = self.fetch_from_source(source=source, stop_at_level=stop_at_level)
|
|
if new_data_object is not None:
|
|
data_object.merge(new_data_object)
|
|
|
|
return data_object
|
|
|
|
def fetch_from_source(self, source: Source, **kwargs) -> Optional[DataObject]:
|
|
page: Page = self._get_page_from_enum(source.page_enum)
|
|
if page is None:
|
|
return None
|
|
|
|
# getting the appropriate function for the page and the object type
|
|
source_type = page.get_source_type(source)
|
|
if not hasattr(page, fetch_map[source_type]):
|
|
return None
|
|
func = getattr(page, fetch_map[source_type])(source=source, **kwargs)
|
|
|
|
# fetching the data object and marking it as fetched
|
|
data_object: DataObject = func(source=source)
|
|
data_object.mark_as_fetched(source.hash_url)
|
|
return data_object
|
|
|
|
def fetch_from_url(self, url: str) -> Optional[DataObject]:
|
|
source = Source.match_url(url, SourcePages.MANUAL)
|
|
if source is None:
|
|
return None
|
|
|
|
return self.fetch_from_source(source=source)
|
|
|
|
def is_downloadable(self, music_object: DataObject) -> 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
|
|
|
|
def download(self, music_object: DataObject, 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.")
|
|
|
|
self.fetch_details(music_object)
|
|
|
|
_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)
|
|
|
|
for download_page in audio_pages:
|
|
return self._page_instances[download_page].download(music_object=music_object, genre=genre)
|
|
|
|
return DownloadResult(error_message=f"No audio source has been found for {music_object}.")
|
|
|
|
def fetch_url(self, url: str, stop_at_level: int = 2) -> Tuple[Type[Page], DataObject]:
|
|
source = Source.match_url(url, SourcePages.MANUAL)
|
|
|
|
if source is None:
|
|
raise UrlNotFoundException(url=url)
|
|
|
|
_actual_page = self._source_to_page[source.page_enum]
|
|
|
|
return _actual_page, self._page_instances[_actual_page].fetch_object_from_source(source=source, stop_at_level=stop_at_level) |