ytdl-org / youtube-dl

Command-line program to download videos from YouTube.com and other video sites
http://ytdl-org.github.io/youtube-dl/
The Unlicense
131.97k stars 10.01k forks source link

Windows platform: Consider using %APPDATA% or %LOCALAPPDATA% for application-generated files #28163

Open denizoezmen opened 3 years ago

denizoezmen commented 3 years ago

Checklist

Description

When downloading files from certain URLs on the Windows platform, youtube-dl (or one of its libraries?) will generate a ".cache" directory directly in the root of the user's profile directory (e.g. C:\Users\<username>\.cache).

Example:

youtube-dl --no-check-certificate https://soundcloud.com/laurence_chapman/heavens-vault

will create a file called C:\Users\<username>\.cache\youtube-dl\soundcloud\client_id.json

Unlike on *nix platforms, file and directory names starting with a dot are not hidden by default on the Windows platform. Using multiple applications that exhibit this behaviour thus clutters up the user's profile directory.

On Windows, application-specific data should usually be placed in the profile directory's AppData\Roaming subdirectory (%APPDATA%) for "permanent" data that would also be considered in a roaming profile, or the AppData\Local subdirectory (%LOCALAPPDATA%) for mere temporary files.

Please consider using these directories instead (if feasible).

rautamiekka commented 3 years ago

It's more than feasible, gallery-dl has done that by default (other places're used in order too).

pukkandan commented 3 years ago

User can change it using --cache-dir

denizoezmen commented 3 years ago

Thank you for the suggestion. However, I wouldn't classify this as a solution, since usage of the correct (platform-depent) directories intended for application data should be the default behaviour.

dirkf commented 2 years ago
if params.get('cache_dir') is None and platform == 'Windows':
    params['cache_dir'] = ...
pukkandan commented 2 years ago

This is not the intended use of site.userbase and it may not always point to the appdata. Better to expand the relevant env vars

https://github.com/ytdl-org/youtube-dl/blob/a0068bd6bec16008bda7a39caecccbf84881c603/youtube_dl/options.py#L70

That said, I don't know if this change is justified. When a user updates to a version with this change, the old .cache directory will remain, and even --rm-cache-dir will no longer remove it. Additional checks may need to be done to handle this

dirkf commented 2 years ago

OTOH compat_getenv('APPDATA') won't find ~/.cache.

I share the concern about the pain of moving the default cache to a new location. Perhaps the old default should be maintained if present?

So

--- old/youtube-dl/youtube_dl/cache.py
+++ new/youtube-dl/youtube_dl/cache.py
@@ -19,11 +19,26 @@
     def __init__(self, ydl):
         self._ydl = ydl

-    def _get_root_dir(self):
+    def _get_old_root_dir(self):
         res = self._ydl.params.get('cachedir')
         if res is None:
             cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
             res = os.path.join(cache_root, 'youtube-dl')
         return expand_path(res)
+
+    def _get_root_dir(self):
+        res = self._get_old_root_dir()
+        if os.path.exists(res):
+            return res
+        res = self._ydl.params.get('cachedir')
+        if res is None:
+            # LOCALAPPDATA "new" in Windows 7
+            res = compat_getenv('APPDATA')
+            if res is not None:
+                # assumption: setting APPDATA means this is the answer
+                return os.path.join(res, 'youtube-dl', 'cache')
+            res = compat_getenv('XDG_CACHE_HOME', '~/.cache')
+            res = os.path.join(res, 'youtube-dl')
+        return expand_path(res)

     def _get_cache_fn(self, section, key, dtype):