From 8255ad5264049303f95992e2753b24eed37a00fb Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Thu, 23 May 2024 14:24:20 +0200 Subject: [PATCH] feat: added detection to autoscann pages --- music_kraken/pages/__init__.py | 62 ++++++++++++++++--- .../pages/{abstract.py => _abstract.py} | 5 +- .../pages/{bandcamp.py => _bandcamp.py} | 2 +- ...metallum.py => _encyclopaedia_metallum.py} | 3 +- music_kraken/pages/{genius.py => _genius.py} | 2 +- music_kraken/pages/{musify.py => _musify.py} | 2 +- .../pages/{youtube.py => _youtube.py} | 6 +- .../__init__.py | 0 .../_list_render.py | 1 - .../_music_object_render.py | 1 - .../super_youtube.py | 2 +- .../youtube_music.py | 2 +- music_kraken/utils/enums/__init__.py | 3 + 13 files changed, 72 insertions(+), 19 deletions(-) rename music_kraken/pages/{abstract.py => _abstract.py} (98%) rename music_kraken/pages/{bandcamp.py => _bandcamp.py} (99%) rename music_kraken/pages/{encyclopaedia_metallum.py => _encyclopaedia_metallum.py} (99%) rename music_kraken/pages/{genius.py => _genius.py} (99%) rename music_kraken/pages/{musify.py => _musify.py} (99%) rename music_kraken/pages/{youtube.py => _youtube.py} (98%) rename music_kraken/pages/{youtube_music => _youtube_music}/__init__.py (100%) rename music_kraken/pages/{youtube_music => _youtube_music}/_list_render.py (99%) rename music_kraken/pages/{youtube_music => _youtube_music}/_music_object_render.py (99%) rename music_kraken/pages/{youtube_music => _youtube_music}/super_youtube.py (99%) rename music_kraken/pages/{youtube_music => _youtube_music}/youtube_music.py (99%) diff --git a/music_kraken/pages/__init__.py b/music_kraken/pages/__init__.py index ba24501..5f82511 100644 --- a/music_kraken/pages/__init__.py +++ b/music_kraken/pages/__init__.py @@ -1,8 +1,56 @@ -from .encyclopaedia_metallum import EncyclopaediaMetallum -from .musify import Musify -from .youtube import YouTube -from .youtube_music import YoutubeMusic -from .bandcamp import Bandcamp -from .genius import Genius +from typing import Type, Generator, Set, Dict, List +from collections import defaultdict -from .abstract import Page, INDEPENDENT_DB_OBJECTS +from ._encyclopaedia_metallum import EncyclopaediaMetallum +from ._musify import Musify +from ._youtube import YouTube +from ._youtube_music import YoutubeMusic +from ._bandcamp import Bandcamp +from ._genius import Genius +from ._abstract import Page, INDEPENDENT_DB_OBJECTS + + +_registered_pages: Dict[Type[Page], Set[Page]] = defaultdict(set) + + +def get_pages(*page_types: List[Type[Page]]) -> Generator[Page, None, None]: + if len(page_types) == 0: + page_types = _registered_pages.keys() + + for page_type in page_types: + yield from _registered_pages[page_type] + + +def register_page(page_type: Type[Page], **kwargs): + if page_type in _registered_pages: + return + + _registered_pages[page_type].add(page_type(**kwargs)) + + +def deregister_page(page_type: Type[Page]): + if page_type not in _registered_pages: + return + + for p in _registered_pages[page_type]: + p.__del__() + del _registered_pages[page_type] + +def scan_for_pages(): + # assuming the wanted pages are the leaf classes of the interface + leaf_classes = [] + + _class_list = [Page] + while len(_class_list): + _class = _class_list.pop() + _class_subclasses = _class.__subclasses__() + + if len(_class_subclasses) == 0: + if _class.REGISTER: + leaf_classes.append(_class) + else: + _class_list.extend(_class_subclasses) + + print(leaf_classes) + for leaf_class in leaf_classes: + register_page(leaf_class) diff --git a/music_kraken/pages/abstract.py b/music_kraken/pages/_abstract.py similarity index 98% rename from music_kraken/pages/abstract.py rename to music_kraken/pages/_abstract.py index 8783dbb..e6e35f7 100644 --- a/music_kraken/pages/abstract.py +++ b/music_kraken/pages/_abstract.py @@ -48,12 +48,12 @@ class DownloadOptions: process_metadata_if_found: bool = True class Page: + REGISTER = True SOURCE_TYPE: SourceType LOGGER: logging.Logger def __new__(cls, *args, **kwargs): cls.LOGGER = logging.getLogger(cls.__name__) - return super().__new__(cls) def __init__(self, download_options: DownloadOptions = None, fetch_options: FetchOptions = None): @@ -62,6 +62,9 @@ class Page: self.download_options: DownloadOptions = download_options or DownloadOptions() self.fetch_options: FetchOptions = fetch_options or FetchOptions() + def __del__(self): + self.SOURCE_TYPE.deregister_page(self) + def _search_regex(self, pattern, string, default=None, fatal=True, flags=0, group=None): """ Perform a regex search on the given string, using a single or a list of diff --git a/music_kraken/pages/bandcamp.py b/music_kraken/pages/_bandcamp.py similarity index 99% rename from music_kraken/pages/bandcamp.py rename to music_kraken/pages/_bandcamp.py index 1caf803..658b448 100644 --- a/music_kraken/pages/bandcamp.py +++ b/music_kraken/pages/_bandcamp.py @@ -6,7 +6,7 @@ from bs4 import BeautifulSoup import pycountry from ..objects import Source, DatabaseObject -from .abstract import Page +from ._abstract import Page from ..objects import ( Artist, Source, diff --git a/music_kraken/pages/encyclopaedia_metallum.py b/music_kraken/pages/_encyclopaedia_metallum.py similarity index 99% rename from music_kraken/pages/encyclopaedia_metallum.py rename to music_kraken/pages/_encyclopaedia_metallum.py index 52d9ea3..cd6ec4c 100644 --- a/music_kraken/pages/encyclopaedia_metallum.py +++ b/music_kraken/pages/_encyclopaedia_metallum.py @@ -6,7 +6,7 @@ from urllib.parse import urlparse, urlencode from ..connection import Connection from ..utils.config import logging_settings -from .abstract import Page +from ._abstract import Page from ..utils.enums import SourceType, ALL_SOURCE_TYPES from ..utils.enums.album import AlbumType from ..utils.support_classes.query import Query @@ -207,6 +207,7 @@ def create_grid( class EncyclopaediaMetallum(Page): + REGISTER = False SOURCE_TYPE = ALL_SOURCE_TYPES.ENCYCLOPAEDIA_METALLUM LOGGER = logging_settings["metal_archives_logger"] diff --git a/music_kraken/pages/genius.py b/music_kraken/pages/_genius.py similarity index 99% rename from music_kraken/pages/genius.py rename to music_kraken/pages/_genius.py index 1719025..08d38f0 100644 --- a/music_kraken/pages/genius.py +++ b/music_kraken/pages/_genius.py @@ -6,7 +6,7 @@ from bs4 import BeautifulSoup import pycountry from ..objects import Source, DatabaseObject -from .abstract import Page +from ._abstract import Page from ..objects import ( Artist, Source, diff --git a/music_kraken/pages/musify.py b/music_kraken/pages/_musify.py similarity index 99% rename from music_kraken/pages/musify.py rename to music_kraken/pages/_musify.py index e8078fb..0d00d26 100644 --- a/music_kraken/pages/musify.py +++ b/music_kraken/pages/_musify.py @@ -8,7 +8,7 @@ import pycountry from bs4 import BeautifulSoup from ..connection import Connection -from .abstract import Page +from ._abstract import Page from ..utils.enums import SourceType, ALL_SOURCE_TYPES from ..utils.enums.album import AlbumType, AlbumStatus from ..objects import ( diff --git a/music_kraken/pages/youtube.py b/music_kraken/pages/_youtube.py similarity index 98% rename from music_kraken/pages/youtube.py rename to music_kraken/pages/_youtube.py index 8f21c73..2530aa1 100644 --- a/music_kraken/pages/youtube.py +++ b/music_kraken/pages/_youtube.py @@ -5,7 +5,7 @@ from enum import Enum import python_sponsorblock from ..objects import Source, DatabaseObject, Song, Target -from .abstract import Page +from ._abstract import Page from ..objects import ( Artist, Source, @@ -22,7 +22,7 @@ from ..utils.enums import SourceType, ALL_SOURCE_TYPES from ..utils.support_classes.download_result import DownloadResult from ..utils.config import youtube_settings, main_settings, logging_settings -from .youtube_music.super_youtube import SuperYouTube, YouTubeUrl, get_invidious_url, YouTubeUrlType +from ._youtube_music.super_youtube import SuperYouTube, YouTubeUrl, get_invidious_url, YouTubeUrlType """ @@ -38,7 +38,7 @@ def get_piped_url(path: str = "", params: str = "", query: str = "", fragment: s class YouTube(SuperYouTube): - # CHANGE + REGISTER = youtube_settings["use_youtube_alongside_youtube_music"] SOURCE_TYPE = ALL_SOURCE_TYPES.YOUTUBE def __init__(self, *args, **kwargs): diff --git a/music_kraken/pages/youtube_music/__init__.py b/music_kraken/pages/_youtube_music/__init__.py similarity index 100% rename from music_kraken/pages/youtube_music/__init__.py rename to music_kraken/pages/_youtube_music/__init__.py diff --git a/music_kraken/pages/youtube_music/_list_render.py b/music_kraken/pages/_youtube_music/_list_render.py similarity index 99% rename from music_kraken/pages/youtube_music/_list_render.py rename to music_kraken/pages/_youtube_music/_list_render.py index 7dcc8cf..29474cd 100644 --- a/music_kraken/pages/youtube_music/_list_render.py +++ b/music_kraken/pages/_youtube_music/_list_render.py @@ -3,7 +3,6 @@ from enum import Enum from ...utils.config import logging_settings from ...objects import Source, DatabaseObject -from ..abstract import Page from ...objects import ( Artist, Source, diff --git a/music_kraken/pages/youtube_music/_music_object_render.py b/music_kraken/pages/_youtube_music/_music_object_render.py similarity index 99% rename from music_kraken/pages/youtube_music/_music_object_render.py rename to music_kraken/pages/_youtube_music/_music_object_render.py index 43aee3e..474f395 100644 --- a/music_kraken/pages/youtube_music/_music_object_render.py +++ b/music_kraken/pages/_youtube_music/_music_object_render.py @@ -6,7 +6,6 @@ from ...utils.string_processing import clean_song_title from ...utils.enums import SourceType, ALL_SOURCE_TYPES from ...objects import Source, DatabaseObject -from ..abstract import Page from ...objects import ( Artist, Source, diff --git a/music_kraken/pages/youtube_music/super_youtube.py b/music_kraken/pages/_youtube_music/super_youtube.py similarity index 99% rename from music_kraken/pages/youtube_music/super_youtube.py rename to music_kraken/pages/_youtube_music/super_youtube.py index df900a1..fa5ce1c 100644 --- a/music_kraken/pages/youtube_music/super_youtube.py +++ b/music_kraken/pages/_youtube_music/super_youtube.py @@ -6,7 +6,7 @@ import requests import python_sponsorblock from ...objects import Source, DatabaseObject, Song, Target -from ..abstract import Page +from .._abstract import Page from ...objects import ( Artist, Source, diff --git a/music_kraken/pages/youtube_music/youtube_music.py b/music_kraken/pages/_youtube_music/youtube_music.py similarity index 99% rename from music_kraken/pages/youtube_music/youtube_music.py rename to music_kraken/pages/_youtube_music/youtube_music.py index 08e2207..b2d4aa2 100644 --- a/music_kraken/pages/youtube_music/youtube_music.py +++ b/music_kraken/pages/_youtube_music/youtube_music.py @@ -22,7 +22,7 @@ from ...utils import get_current_millis, traverse_json_path from ...utils import dump_to_file -from ..abstract import Page +from .._abstract import Page from ...objects import ( DatabaseObject as DataObject, Source, diff --git a/music_kraken/utils/enums/__init__.py b/music_kraken/utils/enums/__init__.py index 28f0b9f..38fc03f 100644 --- a/music_kraken/utils/enums/__init__.py +++ b/music_kraken/utils/enums/__init__.py @@ -17,6 +17,9 @@ class SourceType: def register_page(self, page: Page): self.page = page + def deregister_page(self): + self.page = None + def __hash__(self): return hash(self.name)