]> jfr.im git - yt-dlp.git/commitdiff
[compat] Don't ignore `HOME` (if set) on windows
authorpukkandan <redacted>
Wed, 15 Sep 2021 19:04:18 +0000 (00:34 +0530)
committerpukkandan <redacted>
Wed, 15 Sep 2021 19:58:54 +0000 (01:28 +0530)
Related: #792

README.md
yt_dlp/compat.py

index d9daee69e605b494e3a97ef5307f218a53179b80..8ffb20a8c1f99c60525629eb56aafe5172ecdc2b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -897,7 +897,7 @@ # CONFIGURATION
     * `~/yt-dlp.conf`
     * `~/yt-dlp.conf.txt`
 
-    Note that `~` points to `C:\Users\<user name>` on windows. Also, `%XDG_CONFIG_HOME%` defaults to `~/.config` if undefined
+    `%XDG_CONFIG_HOME%` defaults to `~/.config` if undefined. On windows, `~` points to %HOME% if present, `%USERPROFILE%` (generally `C:\Users\<user name>`) or `%HOMEDRIVE%%HOMEPATH%`.
 1. **System Configuration**: `/etc/yt-dlp.conf`
 
 For example, with the following configuration file yt-dlp will always extract the audio, not copy the mtime, use a proxy and save all videos under `YouTube` directory in your home directory:
index ab1a3ba44c4a393fd35ad3f37f03d6f32454a08e..363c2d57a2ac1e48815b784589626f8e0eb9b7b2 100644 (file)
@@ -130,6 +130,24 @@ def compat_asyncio_run(coro):
     asyncio.run = compat_asyncio_run
 
 
+# Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl
+# See https://github.com/yt-dlp/yt-dlp/issues/792
+# https://docs.python.org/3/library/os.path.html#os.path.expanduser
+if compat_os_name in ('nt', 'ce') and 'HOME' in os.environ:
+    _userhome = os.environ['HOME']
+
+    def compat_expanduser(path):
+        if not path.startswith('~'):
+            return path
+        i = path.replace('\\', '/', 1).find('/')  # ~user
+        if i < 0:
+            i = len(path)
+        userhome = os.path.join(os.path.dirname(_userhome), path[1:i]) if i > 1 else _userhome
+        return userhome + path[i:]
+else:
+    compat_expanduser = os.path.expanduser
+
+
 #  Deprecated
 
 compat_basestring = str
@@ -152,7 +170,6 @@ def compat_asyncio_run(coro):
 compat_cookies_SimpleCookie = compat_cookies.SimpleCookie
 compat_etree_Element = etree.Element
 compat_etree_register_namespace = etree.register_namespace
-compat_expanduser = os.path.expanduser
 compat_get_terminal_size = shutil.get_terminal_size
 compat_getenv = os.getenv
 compat_getpass = getpass.getpass