music-kraken-core/src/music_kraken/utils/shared.py

112 lines
3.5 KiB
Python
Raw Normal View History

2023-04-04 18:19:29 +00:00
from typing import List
2022-11-11 23:29:07 +00:00
import logging
import tempfile
import os
import configparser
from sys import platform as current_os
2023-04-03 08:38:12 +00:00
from pathlib import Path
2023-04-04 18:19:29 +00:00
import random
"""
I will now and then use those messages in the programm.
But I won't overuse them dw.
I will keep those messages, if you disagree with me on the messages,
feel free to fork the programm and edit them, or just edit them in the config
file once I implemented it.
"""
HAPPY_MESSAGES: List[str] = [
"Support the artist.",
"Star Me: https://github.com/HeIIow2/music-downloader",
"🏳️‍⚧️🏳️‍⚧️ Trans rights are human rights. 🏳️‍⚧️🏳️‍⚧️",
"🏳️‍⚧️🏳️‍⚧️ Trans women are women, trans men are men. 🏳️‍⚧️🏳️‍⚧️",
"🏴‍☠️🏴‍☠️ Unite under one flag, fuck borders. 🏴‍☠️🏴‍☠️",
"Join my Matrix Space: https://matrix.to/#/#music-kraken:matrix.org",
"Gotta love BPJM!! :/"
]
def get_random_message() -> str:
return random.choice(HAPPY_MESSAGES)
2022-11-11 23:29:07 +00:00
2022-11-28 13:49:13 +00:00
2022-11-11 23:29:07 +00:00
LOG_FILE = "download_logs.log"
2023-04-03 08:38:12 +00:00
TEMP_DIR = Path(tempfile.gettempdir(), "music-downloader")
TEMP_DIR.mkdir(exist_ok=True)
2022-11-22 13:53:29 +00:00
2022-11-17 12:23:27 +00:00
# configure logger default
logging.basicConfig(
level=logging.INFO,
format=logging.BASIC_FORMAT,
handlers=[
2023-03-31 07:47:03 +00:00
logging.FileHandler(os.path.join(TEMP_DIR, LOG_FILE)),
2022-11-17 12:23:27 +00:00
logging.StreamHandler()
]
)
2022-11-11 23:29:07 +00:00
2022-12-01 12:15:30 +00:00
SONG_LOGGER = logging.getLogger("song-obj")
2022-11-11 23:29:07 +00:00
SEARCH_LOGGER = logging.getLogger("mb-cli")
2022-11-28 13:49:13 +00:00
INIT_PATH_LOGGER = logging.getLogger("init_path")
2022-11-11 23:29:07 +00:00
DATABASE_LOGGER = logging.getLogger("database")
2022-11-17 12:23:27 +00:00
METADATA_DOWNLOAD_LOGGER = logging.getLogger("metadata")
URL_DOWNLOAD_LOGGER = logging.getLogger("AudioSource")
2023-04-03 08:38:12 +00:00
TAGGING_LOGGER = logging.getLogger("tagging")
YOUTUBE_LOGGER = logging.getLogger("Youtube")
2022-11-17 12:23:27 +00:00
MUSIFY_LOGGER = logging.getLogger("Musify")
2022-11-11 23:29:07 +00:00
PATH_LOGGER = logging.getLogger("create-paths")
DOWNLOAD_LOGGER = logging.getLogger("download")
2022-11-14 14:44:32 +00:00
LYRICS_LOGGER = logging.getLogger("lyrics")
2022-11-11 23:29:07 +00:00
GENIUS_LOGGER = logging.getLogger("genius")
2023-01-24 08:40:01 +00:00
ENCYCLOPAEDIA_METALLUM_LOGGER = logging.getLogger("ma")
2022-11-17 12:23:27 +00:00
NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea"
2023-04-03 08:38:12 +00:00
MUSIC_DIR = Path(os.path.expanduser("~"), "Music")
2022-11-11 23:29:07 +00:00
if current_os == "linux":
2022-11-28 14:16:04 +00:00
# XDG_USER_DIRS_FILE reference: https://freedesktop.org/wiki/Software/xdg-user-dirs/
XDG_USER_DIRS_FILE = os.path.join(os.path.expanduser("~"), ".config", "user-dirs.dirs")
2022-11-28 13:49:13 +00:00
logger = logging.getLogger("init_path")
logger.setLevel(logging.WARNING)
try:
2022-11-28 14:16:04 +00:00
with open(XDG_USER_DIRS_FILE, 'r') as f:
data = "[XDG_USER_DIRS]\n" + f.read()
2022-11-28 13:49:13 +00:00
config = configparser.ConfigParser(allow_no_value=True)
config.read_string(data)
2022-11-28 13:49:13 +00:00
xdg_config = config['XDG_USER_DIRS']
MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"'))
2023-04-04 18:19:29 +00:00
except (FileNotFoundError, KeyError) as E:
2023-04-03 08:38:12 +00:00
logger.warning(
2023-04-04 18:20:15 +00:00
f"Missing file or No entry found for \"xdg_music_dir\" in: \"{XDG_USER_DIRS_FILE}\".\n"
2023-04-04 18:19:29 +00:00
f"Will fallback on default \"$HOME/Music\"."
)
2022-11-11 23:29:07 +00:00
TOR = False
proxies = {
'http': 'socks5h://127.0.0.1:9150',
'https': 'socks5h://127.0.0.1:9150'
} if TOR else {}
2023-04-03 08:38:12 +00:00
"""
available variables:
- genre
- label
- artist
- album
- song
2023-04-04 08:43:52 +00:00
- album_type
2023-04-03 08:38:12 +00:00
"""
2023-04-04 08:43:52 +00:00
DOWNLOAD_PATH = "{genre}/{artist}/{album_type}/{album}"
2023-04-03 08:38:12 +00:00
DOWNLOAD_FILE = "{song}.mp3"
DEFAULT_VALUES = {
"genre": "Various Genre",
"label": "Various Labels",
"artist": "Various Artists",
"album": "Various Album",
"song": "Various Song",
2023-04-04 08:43:52 +00:00
"album_type": "Other"
2023-04-03 08:38:12 +00:00
}
# size of the chunks that are streamed
CHUNK_SIZE = 1024