From 55939887f3de292237d1163e636ba04ee7115309 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Mon, 17 Jun 2024 17:04:10 +0200 Subject: [PATCH] cache metadata for search results... TODO at some point they need to be removed, but not today... --- syng/sources/youtube.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/syng/sources/youtube.py b/syng/sources/youtube.py index 2668ed8..8e86888 100644 --- a/syng/sources/youtube.py +++ b/syng/sources/youtube.py @@ -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: