refactored cli

This commit is contained in:
Hellow2
2023-06-20 16:40:34 +02:00
parent 08b47b7f59
commit f95083050e
13 changed files with 209 additions and 199 deletions

View File

@@ -1 +1,5 @@
from .download.shell import Shell
from .informations import print_paths
from .main_downloader import download
from .options.settings import settings
from .options.frontend import set_frontend

View File

@@ -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}")

View File

@@ -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
@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()

View File

@@ -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)

View File

@@ -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()

View File

@@ -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)