From 71ee6ad5e2d6a78ce095feb931fea1d23d374602 Mon Sep 17 00:00:00 2001 From: Sam Redmond Date: Tue, 9 Oct 2018 20:25:45 +1300 Subject: [PATCH] Windows - 'My Music' folder won't be assumed to be on C drive (#387) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Windows - 'My Music' folder won't be assumed to be on C drive Windows has a nice registry check to get the absolute path of the 'My Music' folder. This helps because some people change their location of their music folder. * Updated according to suggestions Let me know if there are anymore improvements 👍 * Fixups --- spotdl/internals.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spotdl/internals.py b/spotdl/internals.py index 76fc6d9..a36c7a2 100755 --- a/spotdl/internals.py +++ b/spotdl/internals.py @@ -4,6 +4,10 @@ from logzero import logger as log from spotdl import const +try: + import winreg +except ImportError: + pass try: from slugify import SLUG_OK, slugify @@ -174,8 +178,7 @@ def get_unique_tracks(text_file): return lines - -# a hacky way to user's localized music directory +# a hacky way to get user's localized music directory # (thanks @linusg, issue #203) def get_music_dir(): home = os.path.expanduser('~') @@ -191,6 +194,15 @@ def get_music_dir(): if line.startswith('XDG_MUSIC_DIR'): return os.path.expandvars(line.strip().split('=')[1].strip('"')) + # Windows / Cygwin + # Queries registry for 'My Music' folder path (as this can be changed) + if 'win' in sys.platform: + try: + key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", 0, winreg.KEY_ALL_ACCESS) + return winreg.QueryValueEx(key, "My Music")[0] + except (FileNotFoundError, NameError): + pass + # 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