From 8405692d6b23c68b064d415615743fb4b3091c8e Mon Sep 17 00:00:00 2001 From: darkeox Date: Thu, 24 Nov 2022 23:22:06 +0100 Subject: [PATCH] introduce localization for Music dir on Linux --- src/music_kraken/utils/shared.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 2db3ae6..84acec4 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -2,8 +2,11 @@ import musicbrainzngs import logging import tempfile import os +import io +import configparser +from sys import platform as current_os - +USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirs") TEMP_FOLDER = "music-downloader" LOG_FILE = "download_logs.log" TEMP_DATABASE_FILE = "metadata.db" @@ -39,6 +42,21 @@ GENIUS_LOGGER = logging.getLogger("genius") NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music") +if current_os == "linux": + try: + with open(USER_XDG_DIR_FILE, 'r') as f: + data = io.StringIO("[XDG_USER_DIRS]\n" + f.read()) + config = configparser.ConfigParser(allow_no_value=True) + config.read_file(data) + xdg_config = config['XDG_USER_DIRS'] + MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) + except FileNotFoundError as N: + print(f''' +Missing XDG_USER_DIRS file at: '{USER_XDG_DIR_FILE}'. +Will fallback on default '$HOME/Music'. +---- + ''') + TOR = False proxies = { 'http': 'socks5h://127.0.0.1:9150',