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 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:
self._infos = YoutubeDL({"quiet": True}).extract_info(url, download=False) if url in YouTube.__cache__:
if self._infos is None: self._infos = YouTube.__cache__[url]
raise RuntimeError(f'Extraction not possible for "{url}"') else:
self.length = self._infos["duration"] self._infos = YoutubeDL({"quiet": True}).extract_info(url, download=False)
self.title = self._infos["title"] if self._infos is None:
self.author = self._infos["channel"] raise RuntimeError(f'Extraction not possible for "{url}"')
self.watch_url = url self.length = self._infos["duration"]
self.title = self._infos["title"]
self.author = self._infos["channel"]
self.watch_url = url
else: else:
self.length = 0 self.length = 0
self.title = "" self.title = ""
@ -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: