From 2a00735f720c2e69c2c14b4e48b4d1438ee6b734 Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Tue, 23 May 2023 10:49:52 +0200 Subject: [PATCH] refactor --- src/metal_archives.py | 2 +- src/music_kraken/pages/abstract.py | 128 ++---------------- .../pages/download_center/search.py | 2 +- .../pages/encyclopaedia_metallum.py | 6 +- src/music_kraken/pages/musify.py | 4 +- src/music_kraken/pages/preset.py | 2 +- .../utils/support_classes/__init__.py | 3 + src/musify_search.py | 2 +- 8 files changed, 23 insertions(+), 126 deletions(-) diff --git a/src/metal_archives.py b/src/metal_archives.py index 33b6b50..e603849 100644 --- a/src/metal_archives.py +++ b/src/metal_archives.py @@ -3,7 +3,7 @@ from music_kraken.pages import EncyclopaediaMetallum def search(): - results = EncyclopaediaMetallum.search_by_query("#a Ghost Bath") + results = EncyclopaediaMetallum._raw_search("#a Ghost Bath") print(results) print(results[0].source_collection) diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index 0d69604..d5502e3 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -7,8 +7,6 @@ import requests from bs4 import BeautifulSoup from ..connection import Connection -from ..utils.support_classes.default_target import DefaultTarget -from ..utils.support_classes.download_result import DownloadResult from ..objects import ( Song, Source, @@ -24,6 +22,7 @@ from ..utils.enums.source import SourcePages from ..utils.enums.album import AlbumType from ..audio import write_metadata_to_target, correct_codec from ..utils import shared +from ..utils.support_classes import Query, DownloadResult, DefaultTarget class Page: @@ -41,125 +40,22 @@ class Page: LOGGER = logging.getLogger("this shouldn't be used") SOURCE_TYPE: SourcePages - - @classmethod - def get_request( - cls, - url: str, - stream: bool = False, - accepted_response_codes: set = {200}, - trie: int = 0 - ) -> Optional[requests.Response]: - - retry = False - try: - r = cls.API_SESSION.get(url, timeout=cls.TIMEOUT, stream=stream) - except requests.exceptions.Timeout: - cls.LOGGER.warning(f"request timed out at \"{url}\": ({trie}-{cls.TRIES})") - retry = True - except requests.exceptions.ConnectionError: - cls.LOGGER.warning(f"couldn't connect to \"{url}\": ({trie}-{cls.TRIES})") - retry = True - - if not retry and r.status_code in accepted_response_codes: - return r - - if not retry: - cls.LOGGER.warning(f"{cls.__name__} responded wit {r.status_code} at GET:{url}. ({trie}-{cls.TRIES})") - cls.LOGGER.debug(r.content) - - if trie >= cls.TRIES: - cls.LOGGER.warning("to many tries. Aborting.") - return None - - return cls.get_request(url=url, stream=stream, accepted_response_codes=accepted_response_codes, trie=trie + 1) - - @classmethod - def post_request(cls, url: str, json: dict, accepted_response_codes: set = {200}, trie: int = 0) -> Optional[ - requests.Response]: - retry = False - try: - r = cls.API_SESSION.post(url, json=json, timeout=cls.POST_TIMEOUT) - except requests.exceptions.Timeout: - cls.LOGGER.warning(f"request timed out at \"{url}\": ({trie}-{cls.TRIES})") - retry = True - except requests.exceptions.ConnectionError: - cls.LOGGER.warning(f"couldn't connect to \"{url}\": ({trie}-{cls.TRIES})") - retry = True - - if not retry and r.status_code in accepted_response_codes: - return r - - if not retry: - cls.LOGGER.warning(f"{cls.__name__} responded wit {r.status_code} at POST:{url}. ({trie}-{cls.TRIES})") - cls.LOGGER.debug(r.content) - - if trie >= cls.TRIES: - cls.LOGGER.warning("to many tries. Aborting.") - return None - - cls.LOGGER.warning(f"payload: {json}") - return cls.post_request(url=url, json=json, accepted_response_codes=accepted_response_codes, trie=trie + 1) - + @classmethod def get_soup_from_response(cls, r: requests.Response) -> BeautifulSoup: return BeautifulSoup(r.content, "html.parser") - class Query: - def __init__(self, query: str): - self.query = query - self.is_raw = False - - self.artist = None - self.album = None - self.song = None - - self.parse_query(query=query) - - def __str__(self): - if self.is_raw: - return self.query - return f"{self.artist}; {self.album}; {self.song}" - - def parse_query(self, query: str): - if not '#' in query: - self.is_raw = True - return - - query = query.strip() - parameters = query.split('#') - parameters.remove('') - - for parameter in parameters: - splitted = parameter.split(" ") - type_ = splitted[0] - input_ = " ".join(splitted[1:]).strip() - - if type_ == "a": - self.artist = input_ - continue - if type_ == "r": - self.album = input_ - continue - if type_ == "t": - self.song = input_ - continue - - def get_str(self, string): - if string is None: - return "" - return string - - artist_str = property(fget=lambda self: self.get_str(self.artist)) - album_str = property(fget=lambda self: self.get_str(self.album)) - song_str = property(fget=lambda self: self.get_str(self.song)) + @classmethod + def search(cls, query: Query) -> Options: + results = [] + + for default_query in query.default_search: + results.extend(cls._raw_search(default_query)) + + return Options(results) @classmethod - def search_by_object(cls, data_object: DatabaseObject, filter_none: bool = True) -> List[DatabaseObject]: - return [] - - @classmethod - def search_by_query(cls, query: str) -> Options: + def _raw_search(cls, query: str) -> Options: """ # The Query You can define a new parameter with "#", @@ -174,7 +70,7 @@ class Page: :return possible_music_objects: """ - return Options() + return [] @classmethod def fetch_details(cls, music_object: Union[Song, Album, Artist, Label], stop_at_level: int = 1) -> DatabaseObject: diff --git a/src/music_kraken/pages/download_center/search.py b/src/music_kraken/pages/download_center/search.py index f1c685c..10d8433 100644 --- a/src/music_kraken/pages/download_center/search.py +++ b/src/music_kraken/pages/download_center/search.py @@ -67,7 +67,7 @@ class Search(Download): """ for page in self.pages: - self._current_option[page] = page.search_by_query(query=query) + self._current_option[page] = page._raw_search(query=query) def choose_page(self, page: Type[Page]): """ diff --git a/src/music_kraken/pages/encyclopaedia_metallum.py b/src/music_kraken/pages/encyclopaedia_metallum.py index 1592b91..54435a5 100644 --- a/src/music_kraken/pages/encyclopaedia_metallum.py +++ b/src/music_kraken/pages/encyclopaedia_metallum.py @@ -1,13 +1,11 @@ from collections import defaultdict from typing import List, Optional, Dict, Type, Union -import requests from bs4 import BeautifulSoup import pycountry from urllib.parse import urlparse from ..connection import Connection -from ..utils.shared import ENCYCLOPAEDIA_METALLUM_LOGGER, proxies -from ..utils import string_processing +from ..utils.shared import ENCYCLOPAEDIA_METALLUM_LOGGER from .abstract import Page from ..utils.enums.source import SourcePages from ..utils.enums.album import AlbumType @@ -45,7 +43,7 @@ class EncyclopaediaMetallum(Page): LOGGER = ENCYCLOPAEDIA_METALLUM_LOGGER @classmethod - def search_by_query(cls, query: str) -> Options: + def _raw_search(cls, query: str) -> Options: query_obj = cls.Query(query) if query_obj.is_raw: diff --git a/src/music_kraken/pages/musify.py b/src/music_kraken/pages/musify.py index 4c1ce04..305d147 100644 --- a/src/music_kraken/pages/musify.py +++ b/src/music_kraken/pages/musify.py @@ -25,7 +25,7 @@ from ..objects import ( ) from ..utils.shared import MUSIFY_LOGGER from ..utils import string_processing, shared -from .support_classes.download_result import DownloadResult +from ..utils.support_classes import DownloadResult """ https://musify.club/artist/ghost-bath-280348?_pjax=#bodyContent @@ -112,7 +112,7 @@ class Musify(Page): ) @classmethod - def search_by_query(cls, query: str) -> Options: + def _raw_search(cls, query: str) -> Options: query_obj = cls.Query(query) if query_obj.is_raw: diff --git a/src/music_kraken/pages/preset.py b/src/music_kraken/pages/preset.py index 826e3f2..333b265 100644 --- a/src/music_kraken/pages/preset.py +++ b/src/music_kraken/pages/preset.py @@ -46,7 +46,7 @@ class YouTube(Page): @classmethod - def search_by_query(cls, query: str) -> Options: + def _raw_search(cls, query: str) -> Options: return Options() @classmethod diff --git a/src/music_kraken/utils/support_classes/__init__.py b/src/music_kraken/utils/support_classes/__init__.py index e69de29..625ed41 100644 --- a/src/music_kraken/utils/support_classes/__init__.py +++ b/src/music_kraken/utils/support_classes/__init__.py @@ -0,0 +1,3 @@ +from .default_target import DefaultTarget +from .download_result import DownloadResult +from .query import Query diff --git a/src/musify_search.py b/src/musify_search.py index ad79908..09fdf5f 100644 --- a/src/musify_search.py +++ b/src/musify_search.py @@ -3,7 +3,7 @@ from music_kraken.pages import Musify def search(): - results = Musify.search_by_query("#a Ghost Bath") + results = Musify._raw_search("#a Ghost Bath") print(results)