cache.py 3.3 KB

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