diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 28d505a..03e84a5 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -6,12 +6,7 @@ from typing import List import gc import musicbrainzngs -from .cli import Shell -from . import objects, pages, download -from .utils import exception, shared, path_manager -from .utils.config import config, read, write, PATHS_SECTION -from .utils.shared import MUSIC_DIR, MODIFY_GC, NOT_A_GENRE_REGEX, get_random_message -from .utils.string_processing import fit_to_file_system +from .utils.shared import MODIFY_GC if MODIFY_GC: @@ -31,111 +26,3 @@ if MODIFY_GC: logging.getLogger("musicbrainzngs").setLevel(logging.WARNING) musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader") - -URL_REGEX = 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+' -DOWNLOAD_COMMANDS = { - "ok", - "download", - "\\d", - "hs" -} - -EXIT_COMMANDS = { - "exit", - "quit" -} - - -def print_cute_message(): - message = get_random_message() - try: - print(message) - except UnicodeEncodeError: - message = str(c for c in message if 0 < ord(c) < 127) - print(message) - - -def exit_message(): - print() - print_cute_message() - print("See you soon! :3") - - -def settings( - name: str = None, - value: str = None, -): - def modify_setting(_name: str, _value: str, invalid_ok: bool = True) -> bool: - try: - config.set_name_to_value(_name, _value) - except exception.config.SettingException as e: - if invalid_ok: - print(e) - return False - else: - raise e - - write() - return True - - def print_settings(): - for i, attribute in enumerate(config): - print(f"{i:0>2}: {attribute.name}={attribute.value}") - - def modify_setting_by_index(index: int) -> bool: - attribute = list(config)[index] - - print() - print(attribute) - - input__ = input(f"{attribute.name}=") - if not modify_setting(attribute.name, input__.strip()): - return modify_setting_by_index(index) - - return True - - if name is not None and value is not None: - modify_setting(name, value, invalid_ok=True) - - print() - print_cute_message() - print() - return - - while True: - print_settings() - - input_ = input("Id of setting to modify: ") - print() - if input_.isdigit() and int(input_) < len(config): - if modify_setting_by_index(int(input_)): - print() - print_cute_message() - print() - return - else: - print("Please input a valid ID.") - print() - - -def cli( - genre: str = None, - download_all: bool = False, - direct_download_url: str = None, - command_list: List[str] = None -): - shell = Shell(genre=genre) - - if command_list is not None: - for command in command_list: - shell.process_input(command) - return - - if direct_download_url is not None: - if shell.download(direct_download_url, download_all=download_all): - exit_message() - return - - shell.mainloop() - - exit_message() diff --git a/src/music_kraken/__main__.py b/src/music_kraken/__main__.py index 776622b..7a249a8 100644 --- a/src/music_kraken/__main__.py +++ b/src/music_kraken/__main__.py @@ -80,44 +80,39 @@ if __name__ == "__main__": print("Setting logging-level to DEBUG") logging.getLogger().setLevel(logging.DEBUG) - import music_kraken - - music_kraken.read() - - if arguments.setting is not None: - music_kraken.settings(*arguments.setting) - exit() - - if arguments.settings: - music_kraken.settings() - exit() - - if arguments.paths: - music_kraken.paths() - exit() - + from . import cli + from .utils.config import read_config + from .utils import shared + if arguments.r: import os - if os.path.exists(music_kraken.shared.CONFIG_FILE): - os.remove(music_kraken.shared.CONFIG_FILE) - music_kraken.read() + if os.path.exists(shared.CONFIG_FILE): + os.remove(shared.CONFIG_FILE) + read_config() + + exit() + + read_config() + + if arguments.setting is not None: + cli.settings(*arguments.setting) + + if arguments.settings: + cli.settings() + + if arguments.paths: + cli.print_paths() if arguments.frontend: - from .cli.options.frontend import set_frontend - set_frontend() - exit() + cli.set_frontend(silent=False) # getting the genre genre: str = arguments.genre if arguments.test: genre = "test" - try: - music_kraken.cli( - genre=genre, - download_all=arguments.all, - direct_download_url=arguments.url - ) - except KeyboardInterrupt: - print("\n\nRaise an issue if I fucked up:\nhttps://github.com/HeIIow2/music-downloader/issues") - music_kraken.exit_message() + cli.download( + genre=genre, + download_all=arguments.all, + direct_download_url=arguments.url + ) \ No newline at end of file diff --git a/src/music_kraken/cli/__init__.py b/src/music_kraken/cli/__init__.py index c21af81..1dbf213 100644 --- a/src/music_kraken/cli/__init__.py +++ b/src/music_kraken/cli/__init__.py @@ -1 +1,5 @@ -from .download.shell import Shell \ No newline at end of file +from .informations import print_paths +from .main_downloader import download +from .options.settings import settings +from .options.frontend import set_frontend + diff --git a/src/music_kraken/cli/download/__init__.py b/src/music_kraken/cli/download/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/music_kraken/cli/informations/paths.py b/src/music_kraken/cli/informations/paths.py index d6bff30..cb1ba98 100644 --- a/src/music_kraken/cli/informations/paths.py +++ b/src/music_kraken/cli/informations/paths.py @@ -1,3 +1,5 @@ +from ..utils import cli_function + from ...utils.path_manager import LOCATIONS from ...utils import shared @@ -12,6 +14,7 @@ def all_paths(): } +@cli_function def print_paths(): for name, path in all_paths().items(): print(f"{name}:\t{path}") \ No newline at end of file diff --git a/src/music_kraken/cli/download/shell.py b/src/music_kraken/cli/main_downloader.py similarity index 90% rename from src/music_kraken/cli/download/shell.py rename to src/music_kraken/cli/main_downloader.py index 03e804f..b9076ca 100644 --- a/src/music_kraken/cli/download/shell.py +++ b/src/music_kraken/cli/main_downloader.py @@ -2,14 +2,16 @@ from typing import Set, Type, Dict, List from pathlib import Path import re -from ...utils.shared import MUSIC_DIR, NOT_A_GENRE_REGEX, ENABLE_RESULT_HISTORY, HISTORY_LENGTH -from ...utils.regex import URL_PATTERN -from ...utils.string_processing import fit_to_file_system -from ...utils.support_classes import Query, DownloadResult -from ...download.results import Results, SearchResults, Option, PageResults -from ...download.page_attributes import Pages -from ...pages import Page -from ...objects import Song, Album, Artist, DatabaseObject +from .utils import cli_function + +from ..utils.shared import MUSIC_DIR, NOT_A_GENRE_REGEX, ENABLE_RESULT_HISTORY, HISTORY_LENGTH, HELP_MESSAGE +from ..utils.regex import URL_PATTERN +from ..utils.string_processing import fit_to_file_system +from ..utils.support_classes import Query, DownloadResult +from ..download.results import Results, SearchResults, Option, PageResults +from ..download.page_attributes import Pages +from ..pages import Page +from ..objects import Song, Album, Artist, DatabaseObject """ @@ -128,30 +130,12 @@ def get_genre(): def help_message(): print() - print(""" -to search: -> s: {query or url} -> s: https://musify.club/release/some-random-release-183028492 -> s: #a {artist} #r {release} #t {track} - -to download: -> d: {option ids or direct url} -> d: 0, 3, 4 -> d: 1 -> d: https://musify.club/release/some-random-release-183028492 - -have fun :3 - """.strip()) + print(HELP_MESSAGE) print() -class Shell: - """ - TODO: - - - Implement search and download for direct urls - """ - + +class Downloader: def __init__( self, exclude_pages: Set[Type[Page]] = None, @@ -388,4 +372,23 @@ class Shell: while True: if self.process_input(input("> ")): return - \ No newline at end of file + +@cli_function +def download( + genre: str = None, + download_all: bool = False, + direct_download_url: str = None, + command_list: List[str] = None +): + shell = Downloader(genre=genre) + + if command_list is not None: + for command in command_list: + shell.process_input(command) + return + + if direct_download_url is not None: + if shell.download(direct_download_url, download_all=download_all): + return + + shell.mainloop() diff --git a/src/music_kraken/cli/options/frontend.py b/src/music_kraken/cli/options/frontend.py index 809ebe8..28f9239 100644 --- a/src/music_kraken/cli/options/frontend.py +++ b/src/music_kraken/cli/options/frontend.py @@ -1,10 +1,11 @@ from typing import Dict, List -import requests from dataclasses import dataclass from collections import defaultdict +from ..utils import cli_function + from ...objects import Country -from ...utils import config, write, shared +from ...utils import config, write_config from ...connection import Connection @@ -45,7 +46,7 @@ class FrontendInstance: def set_instance(self, instance: Instance): config.set_name_to_value(self.SETTING_NAME, instance.uri) - write() + write_config() def _choose_country(self) -> List[Instance]: print("Input the country code, an example would be \"US\"") @@ -123,7 +124,7 @@ class Invidious(FrontendInstance): r = self.connection.get(self.endpoint) if r is None: return - + for instance in r.json(): self._process_instance(all_instance_data=instance) @@ -177,6 +178,8 @@ class FrontendSelection: self.piped.choose(silent) +@cli_function def set_frontend(silent: bool = False): shell = FrontendSelection() shell.choose(silent=silent) + \ No newline at end of file diff --git a/src/music_kraken/cli/options/settings.py b/src/music_kraken/cli/options/settings.py new file mode 100644 index 0000000..3ba0ade --- /dev/null +++ b/src/music_kraken/cli/options/settings.py @@ -0,0 +1,71 @@ +from ..utils import cli_function + +from ...utils.config import config, write_config +from ...utils import exception + + +def modify_setting(_name: str, _value: str, invalid_ok: bool = True) -> bool: + try: + config.set_name_to_value(_name, _value) + except exception.config.SettingException as e: + if invalid_ok: + print(e) + return False + else: + raise e + + write_config() + return True + + +def print_settings(): + for i, attribute in enumerate(config): + print(f"{i:0>2}: {attribute.name}={attribute.value}") + + + def modify_setting_by_index(index: int) -> bool: + attribute = list(config)[index] + + print() + print(attribute) + + input__ = input(f"{attribute.name}=") + if not modify_setting(attribute.name, input__.strip()): + return modify_setting_by_index(index) + + return True + + +def modify_setting_by_index(index: int) -> bool: + attribute = list(config)[index] + + print() + print(attribute) + + input__ = input(f"{attribute.name}=") + if not modify_setting(attribute.name, input__.strip()): + return modify_setting_by_index(index) + + return True + + +@cli_function +def settings( + name: str = None, + value: str = None, +): + if name is not None and value is not None: + modify_setting(name, value, invalid_ok=True) + return + + while True: + print_settings() + + input_ = input("Id of setting to modify: ") + print() + if input_.isdigit() and int(input_) < len(config): + if modify_setting_by_index(int(input_)): + return + else: + print("Please input a valid ID.") + print() \ No newline at end of file diff --git a/src/music_kraken/cli/utils.py b/src/music_kraken/cli/utils.py new file mode 100644 index 0000000..ffd629f --- /dev/null +++ b/src/music_kraken/cli/utils.py @@ -0,0 +1,32 @@ +from ..utils.shared import get_random_message + + +def cli_function(function): + def wrapper(*args, **kwargs): + print_cute_message() + print() + try: + function(*args, **kwargs) + except KeyboardInterrupt: + print("\n\nRaise an issue if I fucked up:\nhttps://github.com/HeIIow2/music-downloader/issues") + + finally: + print() + print_cute_message() + print("See you soon! :3") + + exit() + + return wrapper + + +def print_cute_message(): + message = get_random_message() + try: + print(message) + except UnicodeEncodeError: + message = str(c for c in message if 0 < ord(c) < 127) + print(message) + + + \ No newline at end of file diff --git a/src/music_kraken/utils/__init__.py b/src/music_kraken/utils/__init__.py index fb3447a..89186a6 100644 --- a/src/music_kraken/utils/__init__.py +++ b/src/music_kraken/utils/__init__.py @@ -1,4 +1 @@ -from .config import config, read, write - -# tells what exists -__all__ = ["shared", "object_handeling", "phonetic_compares", "functions"] +from .config import config, read_config, write_config diff --git a/src/music_kraken/utils/config/__init__.py b/src/music_kraken/utils/config/__init__.py index 0313788..3d57aed 100644 --- a/src/music_kraken/utils/config/__init__.py +++ b/src/music_kraken/utils/config/__init__.py @@ -4,7 +4,20 @@ from .connection import CONNECTION_SECTION from .misc import MISC_SECTION from .paths import PATHS_SECTION -from .config import read, write, config +from .paths import LOCATIONS +from .config import Config -read() +config = Config() + + +def read_config(): + if not LOCATIONS.CONFIG_FILE.is_file(): + write_config() + config.read_from_config_file(LOCATIONS.CONFIG_FILE) + + +def write_config(): + config.write_to_config_file(LOCATIONS.CONFIG_FILE) + +set_name_to_value = config.set_name_to_value diff --git a/src/music_kraken/utils/config/config.py b/src/music_kraken/utils/config/config.py index 1ecfe28..50b1ac0 100644 --- a/src/music_kraken/utils/config/config.py +++ b/src/music_kraken/utils/config/config.py @@ -125,16 +125,3 @@ class Config: for section in self._section_list: for name, attribute in section.name_attribute_map.items(): yield attribute - - -config = Config() - - -def read(): - if not LOCATIONS.CONFIG_FILE.is_file(): - write() - config.read_from_config_file(LOCATIONS.CONFIG_FILE) - - -def write(): - config.write_to_config_file(LOCATIONS.CONFIG_FILE) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index f9bf8a2..bceba4a 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -105,3 +105,18 @@ THREADED = False ENABLE_RESULT_HISTORY: bool = MISC_SECTION.ENABLE_RESULT_HISTORY.object_from_value HISTORY_LENGTH: int = MISC_SECTION.HISTORY_LENGTH.object_from_value + +HELP_MESSAGE = """ +to search: +> s: {query or url} +> s: https://musify.club/release/some-random-release-183028492 +> s: #a {artist} #r {release} #t {track} + +to download: +> d: {option ids or direct url} +> d: 0, 3, 4 +> d: 1 +> d: https://musify.club/release/some-random-release-183028492 + +have fun :3 +""".strip()