From 37e6f8656a8d18e4af418444ea08448ec530c24e Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 24 May 2022 08:53:05 +0700 Subject: [PATCH] yt-dlp base class --- .../home/src/download/yt_dlp_base.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tubearchivist/home/src/download/yt_dlp_base.py diff --git a/tubearchivist/home/src/download/yt_dlp_base.py b/tubearchivist/home/src/download/yt_dlp_base.py new file mode 100644 index 0000000..c265715 --- /dev/null +++ b/tubearchivist/home/src/download/yt_dlp_base.py @@ -0,0 +1,33 @@ +""" +functionality: +- base class to make all calls to yt-dlp +- handle yt-dlp errors +""" + +from time import sleep + +import yt_dlp + + +class YtWrap: + """wrap calls to yt""" + + def __init__(self, obs): + self.obs = obs + + def download(self, url): + """make download request""" + + with yt_dlp.YoutubeDL(self.obs) as ydl: + try: + ydl.download([url]) + except yt_dlp.utils.DownloadError: + print(f"{url}: failed to download, retry...") + sleep(3) + ydl.download([url]) + + def extract(self, url): + """make extract request""" + response = yt_dlp.YoutubeDL(self.obs).extract_info(url) + + return response