Change default music folder (#225)

* Get default music folder via xdg-user-dirs

* Add a test
This commit is contained in:
Ritiek Malhotra
2018-04-22 21:14:47 +05:30
committed by GitHub
parent 7e48ece898
commit dfcb07ed45
4 changed files with 41 additions and 1 deletions

View File

@@ -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',

View File

@@ -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\<user>\Music or /Users/<user>/Music
# respectively is sufficient.
# On Linux, default to /home/<user>/Music if the above method failed.
return os.path.join(home, 'Music')