diff --git a/.travis.yml b/.travis.yml index f468ee5..863c6e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ before_install: addons: apt: packages: + - xdg-user-dirs - automake - autoconf - build-essential @@ -36,6 +37,7 @@ install: - pip install -e . - tinydownload 22684734659253158385 -o ~/bin/ffmpeg - chmod 755 ~/bin/ffmpeg + - xdg-user-dirs-update script: python -m pytest test --cov=. after_success: - pip install codecov diff --git a/core/handle.py b/core/handle.py index a14f04b..a1bafd7 100644 --- a/core/handle.py +++ b/core/handle.py @@ -14,7 +14,7 @@ default_conf = { 'spotify-downloader': { 'manual' : False, 'no-metadata' : False, 'avconv' : False, - 'folder' : os.path.join(sys.path[0], 'Music'), + 'folder' : internals.get_music_dir(), 'overwrite' : 'prompt', 'input-ext' : '.m4a', 'output-ext' : '.mp3', diff --git a/core/internals.py b/core/internals.py index 5f9f280..d9d5b09 100755 --- a/core/internals.py +++ b/core/internals.py @@ -141,3 +141,27 @@ def get_splits(url): else: splits = url.split(':') return splits + + +# a hacky way to user's localized music directory +# (thanks @linusg, issue #203) +def get_music_dir(): + home = os.path.expanduser('~') + + # On Linux, the localized folder names are the actual ones. + # It's a freedesktop standard though. + if sys.platform.startswith('linux'): + for file_item in ('.config/user-dirs.dirs', 'user-dirs.dirs'): + path = os.path.join(home, file_item) + if os.path.isfile(path): + with open(path, 'r') as f: + for line in f: + if line.startswith('XDG_MUSIC_DIR'): + return os.path.expandvars(line.strip().split('=')[1].strip('"')) + + # On both Windows and macOS, the localized folder names you see in + # Explorer and Finder are actually in English on the file system. + # So, defaulting to C:\Users\\Music or /Users//Music + # respectively is sufficient. + # On Linux, default to /home//Music if the above method failed. + return os.path.join(home, 'Music') diff --git a/test/test_internals.py b/test/test_internals.py index 8d7594a..2aaabf9 100644 --- a/test/test_internals.py +++ b/test/test_internals.py @@ -1,6 +1,20 @@ from core import internals +import sys import os +import subprocess + + +def test_default_music_directory(): + if sys.platform.startswith('linux'): + output = subprocess.check_output(['xdg-user-dir', 'MUSIC']) + expect_directory = output.decode('utf-8').rstrip() + else: + home = os.path.expanduser('~') + expect_directory = os.path.join(home, 'Music') + + directory = internals.get_music_dir() + assert directory == expect_directory class TestPathFilterer: