2022-11-11 23:29:07 +00:00
|
|
|
import musicbrainzngs
|
|
|
|
import logging
|
|
|
|
import tempfile
|
|
|
|
import os
|
2022-11-24 22:22:06 +00:00
|
|
|
import configparser
|
|
|
|
from sys import platform as current_os
|
2022-11-11 23:29:07 +00:00
|
|
|
|
2022-11-28 13:49:13 +00:00
|
|
|
|
2022-11-11 23:29:07 +00:00
|
|
|
TEMP_FOLDER = "music-downloader"
|
|
|
|
LOG_FILE = "download_logs.log"
|
2022-11-22 13:53:29 +00:00
|
|
|
TEMP_DATABASE_FILE = "metadata.db"
|
2022-11-11 23:29:07 +00:00
|
|
|
DATABASE_STRUCTURE_FILE = "database_structure.sql"
|
2022-11-14 14:44:32 +00:00
|
|
|
DATABASE_STRUCTURE_FALLBACK = "https://raw.githubusercontent.com/HeIIow2/music-downloader/master/assets/database_structure.sql"
|
2022-11-17 12:23:27 +00:00
|
|
|
temp_dir = os.path.join(tempfile.gettempdir(), TEMP_FOLDER)
|
|
|
|
if not os.path.exists(temp_dir):
|
|
|
|
os.mkdir(temp_dir)
|
|
|
|
|
2022-11-22 13:53:29 +00:00
|
|
|
TEMP_DATABASE_PATH = os.path.join(temp_dir, TEMP_DATABASE_FILE)
|
|
|
|
|
2022-11-17 12:23:27 +00:00
|
|
|
# configure logger default
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO,
|
|
|
|
format=logging.BASIC_FORMAT,
|
|
|
|
handlers=[
|
|
|
|
logging.FileHandler(os.path.join(temp_dir, LOG_FILE)),
|
|
|
|
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")
|
2022-11-16 12:21:30 +00:00
|
|
|
URL_DOWNLOAD_LOGGER = logging.getLogger("AudioSource")
|
|
|
|
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-11 15:30:53 +00:00
|
|
|
TAGGING_LOGGER = logging.getLogger("tagging")
|
2022-11-11 23:29:07 +00:00
|
|
|
|
2022-11-17 12:23:27 +00:00
|
|
|
NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea"
|
2022-11-28 16:56:19 +00:00
|
|
|
MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music")
|
2022-11-11 23:29:07 +00:00
|
|
|
|
2022-11-24 22:22:06 +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/
|
2022-11-28 16:56:19 +00:00
|
|
|
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)
|
2022-11-24 22:22:06 +00:00
|
|
|
try:
|
2022-11-28 14:16:04 +00:00
|
|
|
with open(XDG_USER_DIRS_FILE, 'r') as f:
|
2022-11-28 14:00:08 +00:00
|
|
|
data = "[XDG_USER_DIRS]\n" + f.read()
|
2022-11-28 13:49:13 +00:00
|
|
|
config = configparser.ConfigParser(allow_no_value=True)
|
2022-11-28 14:00:08 +00:00
|
|
|
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('"'))
|
2022-11-28 14:00:08 +00:00
|
|
|
except (FileNotFoundError, KeyError) as E:
|
2022-11-28 13:49:13 +00:00
|
|
|
logger.warning(f'''
|
2022-11-28 16:56:19 +00:00
|
|
|
Missing file or No entry found for "xdg_music_dir" in: \'{XDG_USER_DIRS_FILE}\'.
|
2022-11-24 22:22:06 +00:00
|
|
|
Will fallback on default '$HOME/Music'.
|
|
|
|
----
|
2022-11-28 16:56:19 +00:00
|
|
|
''')
|
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 {}
|
2022-11-16 12:21:30 +00:00
|
|
|
|
|
|
|
# only the sources here will get downloaded, in the order the list is ordered
|
2022-11-17 12:23:27 +00:00
|
|
|
AUDIO_SOURCES = ["Musify", "Youtube"]
|