]>
Commit | Line | Data |
---|---|---|
19a03940 | 1 | import contextlib |
a0e07d31 PH |
2 | import json |
3 | import os | |
4 | import re | |
5 | import shutil | |
6 | import traceback | |
da8d2de2 | 7 | import urllib.parse |
a0e07d31 | 8 | |
5e01315a | 9 | from .utils import expand_path, traverse_obj, version_tuple, write_json_file |
10 | from .version import __version__ | |
a0e07d31 PH |
11 | |
12 | ||
86e5f3ed | 13 | class Cache: |
a0e07d31 PH |
14 | def __init__(self, ydl): |
15 | self._ydl = ydl | |
16 | ||
17 | def _get_root_dir(self): | |
18 | res = self._ydl.params.get('cachedir') | |
19 | if res is None: | |
ac668111 | 20 | cache_root = os.getenv('XDG_CACHE_HOME', '~/.cache') |
7a5c1cfe | 21 | res = os.path.join(cache_root, 'yt-dlp') |
590bc6f6 | 22 | return expand_path(res) |
a0e07d31 PH |
23 | |
24 | def _get_cache_fn(self, section, key, dtype): | |
da8d2de2 | 25 | assert re.match(r'^[\w.-]+$', section), f'invalid section {section!r}' |
26 | key = urllib.parse.quote(key, safe='').replace('%', ',') # encode non-ascii characters | |
27 | return os.path.join(self._get_root_dir(), section, f'{key}.{dtype}') | |
a0e07d31 PH |
28 | |
29 | @property | |
30 | def enabled(self): | |
31 | return self._ydl.params.get('cachedir') is not False | |
32 | ||
33 | def store(self, section, key, data, dtype='json'): | |
34 | assert dtype in ('json',) | |
35 | ||
36 | if not self.enabled: | |
37 | return | |
38 | ||
39 | fn = self._get_cache_fn(section, key, dtype) | |
40 | try: | |
b25d6cb9 | 41 | os.makedirs(os.path.dirname(fn), exist_ok=True) |
e6f21b3d | 42 | self._ydl.write_debug(f'Saving {section}.{key} to cache') |
5e01315a | 43 | write_json_file({'yt-dlp_version': __version__, 'data': data}, fn) |
a0e07d31 PH |
44 | except Exception: |
45 | tb = traceback.format_exc() | |
86e5f3ed | 46 | self._ydl.report_warning(f'Writing cache to {fn!r} failed: {tb}') |
a0e07d31 | 47 | |
d81ba7d4 | 48 | def _validate(self, data, min_ver): |
5e01315a | 49 | version = traverse_obj(data, 'yt-dlp_version') |
50 | if not version: # Backward compatibility | |
51 | data, version = {'data': data}, '2022.08.19' | |
d81ba7d4 | 52 | if not min_ver or version_tuple(version) >= version_tuple(min_ver): |
5e01315a | 53 | return data['data'] |
d81ba7d4 | 54 | self._ydl.write_debug(f'Discarding old cache from version {version} (needs {min_ver})') |
5e01315a | 55 | |
d81ba7d4 | 56 | def load(self, section, key, dtype='json', default=None, *, min_ver=None): |
a0e07d31 PH |
57 | assert dtype in ('json',) |
58 | ||
59 | if not self.enabled: | |
60 | return default | |
61 | ||
62 | cache_fn = self._get_cache_fn(section, key, dtype) | |
19a03940 | 63 | with contextlib.suppress(OSError): |
a0e07d31 | 64 | try: |
86e5f3ed | 65 | with open(cache_fn, encoding='utf-8') as cachef: |
e6f21b3d | 66 | self._ydl.write_debug(f'Loading {section}.{key} from cache') |
d81ba7d4 | 67 | return self._validate(json.load(cachef), min_ver) |
5e01315a | 68 | except (ValueError, KeyError): |
a0e07d31 PH |
69 | try: |
70 | file_size = os.path.getsize(cache_fn) | |
86e5f3ed | 71 | except OSError as oe: |
a0e07d31 | 72 | file_size = str(oe) |
86e5f3ed | 73 | self._ydl.report_warning(f'Cache retrieval from {cache_fn} failed ({file_size})') |
a0e07d31 PH |
74 | |
75 | return default | |
76 | ||
77 | def remove(self): | |
78 | if not self.enabled: | |
79 | self._ydl.to_screen('Cache is disabled (Did you combine --no-cache-dir and --rm-cache-dir?)') | |
80 | return | |
81 | ||
82 | cachedir = self._get_root_dir() | |
83 | if not any((term in cachedir) for term in ('cache', 'tmp')): | |
84 | raise Exception('Not removing directory %s - this does not look like a cache dir' % cachedir) | |
85 | ||
86 | self._ydl.to_screen( | |
87 | 'Removing cache dir %s .' % cachedir, skip_eol=True) | |
88 | if os.path.exists(cachedir): | |
89 | self._ydl.to_screen('.', skip_eol=True) | |
90 | shutil.rmtree(cachedir) | |
91 | self._ydl.to_screen('.') |