From aec62903e01857c0bfcc75d2fe8f2f11315af845 Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Thu, 10 Aug 2023 23:01:16 +0200 Subject: [PATCH] feat: layed out commit --- .vscode/settings.json | 6 +- src/actual_donwload.py | 3 +- src/music_kraken/__init__.py | 2 +- src/music_kraken/__main__.py | 2 +- src/music_kraken/cli/main_downloader.py | 2 +- src/music_kraken/cli/options/settings.py | 2 +- src/music_kraken/pages/youtube.py | 11 ---- .../pages/youtube_music/super_youtube.py | 18 ++++++ .../pages/youtube_music/youtube_music.py | 5 +- src/music_kraken/utils/__init__.py | 2 +- src/music_kraken/utils/config.py | 17 ++++++ .../utils/{config => old_config}/__init__.py | 12 ++-- .../utils/old_config/attributes/__init__,py | 0 .../utils/old_config/attributes/attribute.py | 60 +++++++++++++++++++ .../old_config/attributes/list_attributes.py | 34 +++++++++++ .../attributes/single_attributes.py | 18 ++++++ .../{config => old_config}/base_classes.py | 0 .../utils/{config => old_config}/config.py | 10 ++-- .../utils/old_config/sections/__init__.py | 0 .../{config => old_config/sections}/audio.py | 6 +- .../sections}/connection.py | 6 +- .../sections}/logging.py | 2 +- .../{config => old_config/sections}/misc.py | 2 +- .../{config => old_config/sections}/paths.py | 4 +- src/music_kraken/utils/old_config/utils.py | 4 ++ .../utils/path_manager/locations.py | 3 +- src/music_kraken/utils/regex.py | 5 +- src/music_kraken/utils/shared.py | 2 +- src/music_kraken/utils/string_processing.py | 7 +++ 29 files changed, 201 insertions(+), 44 deletions(-) create mode 100644 src/music_kraken/utils/config.py rename src/music_kraken/utils/{config => old_config}/__init__.py (55%) create mode 100644 src/music_kraken/utils/old_config/attributes/__init__,py create mode 100644 src/music_kraken/utils/old_config/attributes/attribute.py create mode 100644 src/music_kraken/utils/old_config/attributes/list_attributes.py create mode 100644 src/music_kraken/utils/old_config/attributes/single_attributes.py rename src/music_kraken/utils/{config => old_config}/base_classes.py (100%) rename src/music_kraken/utils/{config => old_config}/config.py (95%) create mode 100644 src/music_kraken/utils/old_config/sections/__init__.py rename src/music_kraken/utils/{config => old_config/sections}/audio.py (97%) rename src/music_kraken/utils/{config => old_config/sections}/connection.py (96%) rename src/music_kraken/utils/{config => old_config/sections}/logging.py (97%) rename src/music_kraken/utils/{config => old_config/sections}/misc.py (97%) rename src/music_kraken/utils/{config => old_config/sections}/paths.py (94%) create mode 100644 src/music_kraken/utils/old_config/utils.py diff --git a/.vscode/settings.json b/.vscode/settings.json index f9f3882..664d07e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,9 @@ "test*.py" ], "python.testing.pytestEnabled": false, - "python.testing.unittestEnabled": true + "python.testing.unittestEnabled": true, + "[python]": { + "editor.defaultFormatter": "ms-python.autopep8" + }, + "python.formatting.provider": "none" } \ No newline at end of file diff --git a/src/actual_donwload.py b/src/actual_donwload.py index f69b781..3b2074a 100644 --- a/src/actual_donwload.py +++ b/src/actual_donwload.py @@ -29,7 +29,8 @@ if __name__ == "__main__": ] youtube_music_test = [ - "s: psychonaut 4" + "s: psychonaut 4", + "0" ] music_kraken.cli.download(genre="test", command_list=youtube_music_test, process_metadata_anyway=True) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 599ad89..99d9f23 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -3,7 +3,7 @@ import logging import gc import musicbrainzngs -from .utils.config import read_config +from .utils.old_config import read_config from .utils.shared import MODIFY_GC from . import cli diff --git a/src/music_kraken/__main__.py b/src/music_kraken/__main__.py index 0425d5d..53ecc92 100644 --- a/src/music_kraken/__main__.py +++ b/src/music_kraken/__main__.py @@ -87,7 +87,7 @@ def cli(): logging.getLogger().setLevel(logging.DEBUG) from . import cli - from .utils.config import read_config + from .utils.old_config import read_config from .utils import shared if arguments.r: diff --git a/src/music_kraken/cli/main_downloader.py b/src/music_kraken/cli/main_downloader.py index ba5ee5f..6dbaff2 100644 --- a/src/music_kraken/cli/main_downloader.py +++ b/src/music_kraken/cli/main_downloader.py @@ -5,7 +5,7 @@ import re from .utils import cli_function from .options.first_config import initial_config -from ..utils.config import set_name_to_value, write_config +from ..utils.old_config import set_name_to_value, write_config from ..utils.shared import MUSIC_DIR, NOT_A_GENRE_REGEX, ENABLE_RESULT_HISTORY, HISTORY_LENGTH, HELP_MESSAGE, HASNT_YET_STARTED from ..utils.regex import URL_PATTERN from ..utils.string_processing import fit_to_file_system diff --git a/src/music_kraken/cli/options/settings.py b/src/music_kraken/cli/options/settings.py index 3ba0ade..ce113f0 100644 --- a/src/music_kraken/cli/options/settings.py +++ b/src/music_kraken/cli/options/settings.py @@ -1,6 +1,6 @@ from ..utils import cli_function -from ...utils.config import config, write_config +from ...utils.old_config import config, write_config from ...utils import exception diff --git a/src/music_kraken/pages/youtube.py b/src/music_kraken/pages/youtube.py index 8b884ee..5450252 100644 --- a/src/music_kraken/pages/youtube.py +++ b/src/music_kraken/pages/youtube.py @@ -67,17 +67,6 @@ class YouTube(SuperYouTube): super().__init__(*args, **kwargs) - def get_source_type(self, source: Source) -> Optional[Type[DatabaseObject]]: - _url_type = { - YouTubeUrlType.CHANNEL: Artist, - YouTubeUrlType.PLAYLIST: Album, - YouTubeUrlType.VIDEO: Song, - } - - parsed = YouTubeUrl(source.url) - if parsed.url_type in _url_type: - return _url_type[parsed.url_type] - def general_search(self, search_query: str) -> List[DatabaseObject]: return self.artist_search(Artist(name=search_query, dynamic=True)) diff --git a/src/music_kraken/pages/youtube_music/super_youtube.py b/src/music_kraken/pages/youtube_music/super_youtube.py index d1153f4..9b5d40c 100644 --- a/src/music_kraken/pages/youtube_music/super_youtube.py +++ b/src/music_kraken/pages/youtube_music/super_youtube.py @@ -50,11 +50,16 @@ class YouTubeUrl: """ def __init__(self, url: str) -> None: + self.SOURCE_TYPE = SourcePages.YOUTUBE + """ Raises Index exception for wrong url, and value error for not found enum type """ self.id = "" parsed = urlparse(url=url) + + if parsed.netloc == "music.youtube.com": + self.SOURCE_TYPE = SourcePages.YOUTUBE_MUSIC self.url_type: YouTubeUrlType @@ -135,6 +140,19 @@ class SuperYouTube(Page): _sponsorblock_connection: Connection = Connection(host="https://sponsor.ajay.app/") self.sponsorblock_client = sponsorblock.Client(session=_sponsorblock_connection.session) + + def get_source_type(self, source: Source) -> Optional[Type[DatabaseObject]]: + _url_type = { + YouTubeUrlType.CHANNEL: Artist, + YouTubeUrlType.PLAYLIST: Album, + YouTubeUrlType.VIDEO: Song, + } + + parsed = YouTubeUrl(source.url) + if parsed.url_type in _url_type: + return _url_type[parsed.url_type] + + def download_song_to_target(self, source: Source, target: Target, desc: str = None) -> DownloadResult: """ 1. getting the optimal source diff --git a/src/music_kraken/pages/youtube_music/youtube_music.py b/src/music_kraken/pages/youtube_music/youtube_music.py index bc03692..0edd0d6 100644 --- a/src/music_kraken/pages/youtube_music/youtube_music.py +++ b/src/music_kraken/pages/youtube_music/youtube_music.py @@ -8,7 +8,7 @@ import re from ...utils.exception.config import SettingValueError from ...utils.shared import PROXIES_LIST, YOUTUBE_MUSIC_LOGGER, DEBUG -from ...utils.config import CONNECTION_SECTION, write_config +from ...utils.old_config import CONNECTION_SECTION, write_config from ...utils.functions import get_current_millis if DEBUG: from ...utils.debug_utils import dump_to_file @@ -279,10 +279,13 @@ class YoutubeMusic(SuperYouTube): def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song: + print(source) return Song() def fetch_album(self, source: Source, stop_at_level: int = 1) -> Album: return Album() def fetch_artist(self, source: Source, stop_at_level: int = 1) -> Artist: + print("fuck you") + print(source) return Artist() diff --git a/src/music_kraken/utils/__init__.py b/src/music_kraken/utils/__init__.py index 89186a6..429f1d6 100644 --- a/src/music_kraken/utils/__init__.py +++ b/src/music_kraken/utils/__init__.py @@ -1 +1 @@ -from .config import config, read_config, write_config +from .old_config import config, read_config, write_config diff --git a/src/music_kraken/utils/config.py b/src/music_kraken/utils/config.py new file mode 100644 index 0000000..026d43b --- /dev/null +++ b/src/music_kraken/utils/config.py @@ -0,0 +1,17 @@ +from dynaconf import Dynaconf + +from .path_manager import LOCATIONS + +""" +https://www.dynaconf.com/settings_files/ + +This file is there to load the settings. +How I will structure this programm exactly is in the stars. + +The concept is that I package a config file, with this programm, and then load it. +Then I check if there is a config file at the LOCATIONS.CONFIG_FILE, and if yes they get merged +""" + +settings = Dynaconf( + settings_files=[str(LOCATIONS.CONFIG_FILE)], +) diff --git a/src/music_kraken/utils/config/__init__.py b/src/music_kraken/utils/old_config/__init__.py similarity index 55% rename from src/music_kraken/utils/config/__init__.py rename to src/music_kraken/utils/old_config/__init__.py index 1da4dab..b490e26 100644 --- a/src/music_kraken/utils/config/__init__.py +++ b/src/music_kraken/utils/old_config/__init__.py @@ -1,10 +1,10 @@ -from .logging import LOGGING_SECTION -from .audio import AUDIO_SECTION -from .connection import CONNECTION_SECTION -from .misc import MISC_SECTION -from .paths import PATHS_SECTION +from .sections.logging import LOGGING_SECTION +from .sections.audio import AUDIO_SECTION +from .sections.connection import CONNECTION_SECTION +from .sections.misc import MISC_SECTION +from .sections.paths import PATHS_SECTION -from .paths import LOCATIONS +from .sections.paths import LOCATIONS from .config import Config diff --git a/src/music_kraken/utils/old_config/attributes/__init__,py b/src/music_kraken/utils/old_config/attributes/__init__,py new file mode 100644 index 0000000..e69de29 diff --git a/src/music_kraken/utils/old_config/attributes/attribute.py b/src/music_kraken/utils/old_config/attributes/attribute.py new file mode 100644 index 0000000..72d7274 --- /dev/null +++ b/src/music_kraken/utils/old_config/attributes/attribute.py @@ -0,0 +1,60 @@ +import re + +from ...exception.config import SettingValueError +from ..utils import comment + + +class Description: + def __init__(self, string: str) -> None: + self.string = string + + @property + def config_string(self) -> str: + return comment(self.string) + + +class Attribute: + pattern: str = r'^.*a$' + rule: str = "This is a default string, it has no rule." + string_value: str = "" + + def __init__(self, name: str, description: str, pattern: str = None, rule: str = None) -> None: + if pattern is not None: + self.pattern = pattern + if rule is not None: + self.rule = rule + + self.name = name + self.description = description + + def validate(self, input_string: str) -> bool: + return re.match(self.REGEX, input_string) is None + + def output_parse(self): + return self.string_value.strip() + + def input_parse(self, input_string: str) -> str: + match_result = re.match(self.pattern, input_string) + + if match_result is None: + raise SettingValueError( + setting_name=self.name, + setting_value=input_string, + rule=self.rule + ) + + return match_result.string + + @property + def value(self) -> str: + raise NotImplementedError() + + @property + def config_string(self) -> str: + return NotImplementedError() + + + +attr = Attribute(name="hello world", description="fuck you", value="defaulte") +attr.input_parse("fafda") +attr.input_parse("eeee") diff --git a/src/music_kraken/utils/old_config/attributes/list_attributes.py b/src/music_kraken/utils/old_config/attributes/list_attributes.py new file mode 100644 index 0000000..aa49abf --- /dev/null +++ b/src/music_kraken/utils/old_config/attributes/list_attributes.py @@ -0,0 +1,34 @@ +from typing import List + +from .attribute import Attribute +from ..utils import comment + + +class ListAttribute(Attribute): + def __init__(self, name: str, description: str, value: List[str], pattern: str = None, rule: str = None) -> None: + super().__init__(name, description, pattern, rule) + + self.string_value_list = [] + self.set_to_list(value) + + + def set_to_list(self, input_value_list: List[str]): + self.string_value_list = [] + for input_value in input_value_list: + self.string_value_list.append(input_value) + + def append(self, input_value: str): + self.string_value_list.append(self.input_parse(input_value)) + + @property + def value(self) -> str: + return [self.output_parse(element) for element in self.string_value_list] + + @property + def config_string(self) -> str: + NEWLINE = "\n" + return f"[{self.name}.start]" \ + f"{comment(self.description)}\n" \ + f"{NEWLINE.join(self.name+'='+v for v in self.string_value_list)}\n" \ + f"{comment('RULE: ' + self.rule)}\n" \ + f"[{self.name}.end]" diff --git a/src/music_kraken/utils/old_config/attributes/single_attributes.py b/src/music_kraken/utils/old_config/attributes/single_attributes.py new file mode 100644 index 0000000..5f53fa7 --- /dev/null +++ b/src/music_kraken/utils/old_config/attributes/single_attributes.py @@ -0,0 +1,18 @@ +from ..utils import comment +from .attribute import Attribute + +class SingleAttribute(Attribute): + def __init__(self, name: str, description: str, value: str, pattern: str = None, rule: str = None) -> None: + super().__init__(name, description, pattern, rule) + + self.string_value = self.input_parse(value) + + @property + def value(self) -> str: + return self.output_parse(self.string_value) + + @property + def config_string(self) -> str: + return f"{comment(self.description)}\n" \ + f"{self.name}={self.value}\n" \ + f"{comment('RULE: ' + self.rule)}" \ diff --git a/src/music_kraken/utils/config/base_classes.py b/src/music_kraken/utils/old_config/base_classes.py similarity index 100% rename from src/music_kraken/utils/config/base_classes.py rename to src/music_kraken/utils/old_config/base_classes.py diff --git a/src/music_kraken/utils/config/config.py b/src/music_kraken/utils/old_config/config.py similarity index 95% rename from src/music_kraken/utils/config/config.py rename to src/music_kraken/utils/old_config/config.py index 50b1ac0..4bac445 100644 --- a/src/music_kraken/utils/config/config.py +++ b/src/music_kraken/utils/old_config/config.py @@ -6,11 +6,11 @@ import os from ..exception.config import SettingNotFound, SettingValueError from ..path_manager import LOCATIONS from .base_classes import Description, Attribute, Section, EmptyLine, COMMENT_PREFIX -from .audio import AUDIO_SECTION -from .logging import LOGGING_SECTION -from .connection import CONNECTION_SECTION -from .misc import MISC_SECTION -from .paths import PATHS_SECTION +from .sections.audio import AUDIO_SECTION +from .sections.logging import LOGGING_SECTION +from .sections.connection import CONNECTION_SECTION +from .sections.misc import MISC_SECTION +from .sections.paths import PATHS_SECTION LOGGER = logging.getLogger("config") diff --git a/src/music_kraken/utils/old_config/sections/__init__.py b/src/music_kraken/utils/old_config/sections/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/music_kraken/utils/config/audio.py b/src/music_kraken/utils/old_config/sections/audio.py similarity index 97% rename from src/music_kraken/utils/config/audio.py rename to src/music_kraken/utils/old_config/sections/audio.py index ea85d84..def1348 100644 --- a/src/music_kraken/utils/config/audio.py +++ b/src/music_kraken/utils/old_config/sections/audio.py @@ -1,6 +1,6 @@ import logging -from .base_classes import ( +from ..base_classes import ( SingleAttribute, FloatAttribute, StringAttribute, @@ -10,8 +10,8 @@ from .base_classes import ( BoolAttribute, ListAttribute ) -from ...utils.enums.album import AlbumType -from ...utils.exception.config import SettingValueError +from ...enums.album import AlbumType +from ...exception.config import SettingValueError # Only the formats with id3 metadata can be used # https://www.audioranger.com/audio-formats.php diff --git a/src/music_kraken/utils/config/connection.py b/src/music_kraken/utils/old_config/sections/connection.py similarity index 96% rename from src/music_kraken/utils/config/connection.py rename to src/music_kraken/utils/old_config/sections/connection.py index 2dfe629..b29ac05 100644 --- a/src/music_kraken/utils/config/connection.py +++ b/src/music_kraken/utils/old_config/sections/connection.py @@ -1,9 +1,9 @@ from urllib.parse import urlparse, ParseResult import re -from .base_classes import Section, FloatAttribute, IntAttribute, BoolAttribute, ListAttribute, StringAttribute -from ..regex import URL_PATTERN -from ..exception.config import SettingValueError +from ..base_classes import Section, FloatAttribute, IntAttribute, BoolAttribute, ListAttribute, StringAttribute +from ...regex import URL_PATTERN +from ...exception.config import SettingValueError class ProxAttribute(ListAttribute): diff --git a/src/music_kraken/utils/config/logging.py b/src/music_kraken/utils/old_config/sections/logging.py similarity index 97% rename from src/music_kraken/utils/config/logging.py rename to src/music_kraken/utils/old_config/sections/logging.py index c972934..17c0969 100644 --- a/src/music_kraken/utils/config/logging.py +++ b/src/music_kraken/utils/old_config/sections/logging.py @@ -1,7 +1,7 @@ import logging from typing import Callable -from .base_classes import SingleAttribute, StringAttribute, Section, Description, EmptyLine +from ..base_classes import SingleAttribute, StringAttribute, Section, Description, EmptyLine LOG_LEVELS = { "CRITICAL": 50, diff --git a/src/music_kraken/utils/config/misc.py b/src/music_kraken/utils/old_config/sections/misc.py similarity index 97% rename from src/music_kraken/utils/config/misc.py rename to src/music_kraken/utils/old_config/sections/misc.py index eac18e2..ad8641a 100644 --- a/src/music_kraken/utils/config/misc.py +++ b/src/music_kraken/utils/old_config/sections/misc.py @@ -1,4 +1,4 @@ -from .base_classes import Section, IntAttribute, ListAttribute, BoolAttribute +from ..base_classes import Section, IntAttribute, ListAttribute, BoolAttribute class MiscSection(Section): diff --git a/src/music_kraken/utils/config/paths.py b/src/music_kraken/utils/old_config/sections/paths.py similarity index 94% rename from src/music_kraken/utils/config/paths.py rename to src/music_kraken/utils/old_config/sections/paths.py index 18cc839..cd35a4b 100644 --- a/src/music_kraken/utils/config/paths.py +++ b/src/music_kraken/utils/old_config/sections/paths.py @@ -1,7 +1,7 @@ from pathlib import Path -from ..path_manager import LOCATIONS -from .base_classes import Section, StringAttribute, ListAttribute +from ...path_manager import LOCATIONS +from ..base_classes import Section, StringAttribute, ListAttribute class PathAttribute(StringAttribute): diff --git a/src/music_kraken/utils/old_config/utils.py b/src/music_kraken/utils/old_config/utils.py new file mode 100644 index 0000000..db05ec4 --- /dev/null +++ b/src/music_kraken/utils/old_config/utils.py @@ -0,0 +1,4 @@ +def comment(uncommented_string: str) -> str: + _fragments = uncommented_string.split("\n") + _fragments = ["# " + frag for frag in _fragments] + return "\n".join(_fragments) diff --git a/src/music_kraken/utils/path_manager/locations.py b/src/music_kraken/utils/path_manager/locations.py index 072c65e..25ab0fb 100644 --- a/src/music_kraken/utils/path_manager/locations.py +++ b/src/music_kraken/utils/path_manager/locations.py @@ -19,7 +19,8 @@ class Locations: self.CONFIG_DIRECTORY = get_config_directory(str(application_name)) self.CONFIG_DIRECTORY.mkdir(exist_ok=True, parents=True) - self.CONFIG_FILE = Path(self.CONFIG_DIRECTORY, f"{application_name}.conf") + self.CONFIG_FILE = Path(self.CONFIG_DIRECTORY, f"{application_name}.toml") + self.LEGACY_CONFIG_FILE = Path(self.CONFIG_DIRECTORY, f"{application_name}.conf") self.FFMPEG_BIN = Path(FFmpeg(enable_log=False).get_ffmpeg_bin()) diff --git a/src/music_kraken/utils/regex.py b/src/music_kraken/utils/regex.py index 1290de5..d8f58f5 100644 --- a/src/music_kraken/utils/regex.py +++ b/src/music_kraken/utils/regex.py @@ -1,2 +1,3 @@ -URL_PATTERN = 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+' - +URL_PATTERN = r"https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+" +INT_PATTERN = r"^\d*$" +FLOAT_PATTERN = r"^[\d|\,|\.]*$" diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 762ccb4..936feb4 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -5,7 +5,7 @@ from typing import List, Tuple, Set, Dict from urllib.parse import ParseResult from .path_manager import LOCATIONS -from .config import LOGGING_SECTION, AUDIO_SECTION, CONNECTION_SECTION, MISC_SECTION, PATHS_SECTION +from .old_config import LOGGING_SECTION, AUDIO_SECTION, CONNECTION_SECTION, MISC_SECTION, PATHS_SECTION from .enums.album import AlbumType CONFIG_FILE = LOCATIONS.CONFIG_FILE diff --git a/src/music_kraken/utils/string_processing.py b/src/music_kraken/utils/string_processing.py index a11c56d..8214e46 100644 --- a/src/music_kraken/utils/string_processing.py +++ b/src/music_kraken/utils/string_processing.py @@ -33,3 +33,10 @@ def fit_to_file_system(string: str) -> str: string = sanitize_filename(string) return string + + +def comment(uncommented_string: str) -> str: + _fragments = uncommented_string.split("\n") + _fragments = ["# " + frag for frag in _fragments] + return "\n".join(_fragments) +