cache metadata for search results... TODO at some point they need to be removed, but not today...

This commit is contained in:
Christoph Stahl 2024-06-17 17:04:10 +02:00
parent 56ab58586a
commit 55939887f3

View file

@ -27,8 +27,13 @@ class YouTube:
A minimal compatibility layer for the YouTube object of pytube, implemented via yt-dlp A minimal compatibility layer for the YouTube object of pytube, implemented via yt-dlp
""" """
__cache__ = {} # TODO: this may grow fast... but atm it fixed youtubes anti bot measures
def __init__(self, url: Optional[str] = None): def __init__(self, url: Optional[str] = None):
if url is not None: if url is not None:
if url in YouTube.__cache__:
self._infos = YouTube.__cache__[url]
else:
self._infos = YoutubeDL({"quiet": True}).extract_info(url, download=False) self._infos = YoutubeDL({"quiet": True}).extract_info(url, download=False)
if self._infos is None: if self._infos is None:
raise RuntimeError(f'Extraction not possible for "{url}"') raise RuntimeError(f'Extraction not possible for "{url}"')
@ -48,12 +53,14 @@ class YouTube:
""" """
Construct a YouTube object from yt-dlp results. Construct a YouTube object from yt-dlp results.
""" """
obj = YouTube() url = search_result["url"]
obj.length = search_result["duration"] cls.__cache__[url] = {
obj.title = search_result["title"] "length": search_result["duration"],
obj.author = search_result["channel"] "title": search_result["title"],
obj.watch_url = search_result["url"] "author": search_result["channel"],
return obj "watch_url": url,
}
return cls(url)
class Search: class Search: