music-kraken-core/src/music_kraken/__init__.py

142 lines
3.5 KiB
Python
Raw Normal View History

2022-11-23 07:24:05 +00:00
import logging
2023-03-30 10:43:43 +00:00
import re
2023-04-05 08:01:51 +00:00
from pathlib import Path
from typing import List
2022-11-23 07:24:05 +00:00
2023-04-15 15:17:33 +00:00
import gc
import musicbrainzngs
2023-06-12 12:56:14 +00:00
from .cli import Shell
2023-05-23 08:55:44 +00:00
from . import objects, pages, download
2023-04-18 07:02:03 +00:00
from .utils import exception, shared, path_manager
from .utils.config import config, read, write, PATHS_SECTION
2023-04-05 08:01:51 +00:00
from .utils.shared import MUSIC_DIR, MODIFY_GC, NOT_A_GENRE_REGEX, get_random_message
2023-04-15 15:17:33 +00:00
from .utils.string_processing import fit_to_file_system
2023-04-04 20:07:56 +00:00
2023-04-18 07:02:03 +00:00
2023-04-04 20:07:56 +00:00
if MODIFY_GC:
"""
At the start I modify the garbage collector to run a bit fewer times.
This should increase speed:
https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/
"""
# Clean up what might be garbage so far.
gc.collect(2)
allocs, gen1, gen2 = gc.get_threshold()
allocs = 50_000 # Start the GC sequence every 50K not 700 allocations.
gen1 = gen1 * 2
gen2 = gen2 * 2
gc.set_threshold(allocs, gen1, gen2)
2022-11-22 13:53:29 +00:00
logging.getLogger("musicbrainzngs").setLevel(logging.WARNING)
musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader")
2023-04-04 19:27:27 +00:00
URL_REGEX = 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
2023-03-31 08:34:29 +00:00
DOWNLOAD_COMMANDS = {
"ok",
"download",
2023-04-04 19:29:10 +00:00
"\\d",
2023-03-31 08:34:29 +00:00
"hs"
}
2023-03-30 10:43:43 +00:00
2023-04-04 18:19:29 +00:00
EXIT_COMMANDS = {
"exit",
"quit"
}
2023-04-06 15:45:15 +00:00
2023-04-06 14:34:51 +00:00
def print_cute_message():
message = get_random_message()
try:
print(message)
2023-04-06 15:45:15 +00:00
except UnicodeEncodeError:
2023-04-06 15:54:16 +00:00
message = str(c for c in message if 0 < ord(c) < 127)
print(message)
2022-11-24 17:25:49 +00:00
2023-04-06 15:45:15 +00:00
2023-04-12 11:13:02 +00:00
def exit_message():
print()
print_cute_message()
2023-04-14 09:22:47 +00:00
print("See you soon! :3")
2023-04-12 11:13:02 +00:00
2023-04-15 09:54:17 +00:00
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)
2023-04-15 10:42:12 +00:00
except exception.config.SettingException as e:
2023-04-15 09:54:17 +00:00
if invalid_ok:
2023-04-15 10:42:12 +00:00
print(e)
2023-04-15 09:54:17 +00:00
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}")
2023-04-15 10:42:12 +00:00
def modify_setting_by_index(index: int) -> bool:
2023-04-15 09:54:17 +00:00
attribute = list(config)[index]
print()
print(attribute)
2023-04-15 10:42:12 +00:00
input__ = input(f"{attribute.name}=")
2023-04-15 09:54:17 +00:00
if not modify_setting(attribute.name, input__.strip()):
2023-04-15 10:42:12 +00:00
return modify_setting_by_index(index)
2023-04-15 09:54:17 +00:00
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
2023-04-15 09:54:17 +00:00
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()
2023-04-15 09:54:17 +00:00
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
):
2023-06-12 12:56:14 +00:00
shell = Shell(genre=genre)
if command_list is not None:
for command in command_list:
2023-06-12 12:56:14 +00:00
shell.process_input(command)
return
2023-06-12 12:56:14 +00:00
if direct_download_url is not None:
if shell.download(direct_download_url, download_all=download_all):
exit_message()
return
shell.mainloop()
2023-04-12 11:13:02 +00:00
exit_message()