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,15 +27,20 @@ class YouTube:
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):
if url is not None:
self._infos = YoutubeDL({"quiet": True}).extract_info(url, download=False)
if self._infos is None:
raise RuntimeError(f'Extraction not possible for "{url}"')
self.length = self._infos["duration"]
self.title = self._infos["title"]
self.author = self._infos["channel"]
self.watch_url = url
if url in YouTube.__cache__:
self._infos = YouTube.__cache__[url]
else:
self._infos = YoutubeDL({"quiet": True}).extract_info(url, download=False)
if self._infos is None:
raise RuntimeError(f'Extraction not possible for "{url}"')
self.length = self._infos["duration"]
self.title = self._infos["title"]
self.author = self._infos["channel"]
self.watch_url = url
else:
self.length = 0
self.title = ""
@ -48,12 +53,14 @@ class YouTube:
"""
Construct a YouTube object from yt-dlp results.
"""
obj = YouTube()
obj.length = search_result["duration"]
obj.title = search_result["title"]
obj.author = search_result["channel"]
obj.watch_url = search_result["url"]
return obj
url = search_result["url"]
cls.__cache__[url] = {
"length": search_result["duration"],
"title": search_result["title"],
"author": search_result["channel"],
"watch_url": url,
}
return cls(url)
class Search: