2022-05-24 01:53:05 +00:00
|
|
|
"""
|
|
|
|
functionality:
|
|
|
|
- base class to make all calls to yt-dlp
|
|
|
|
- handle yt-dlp errors
|
|
|
|
"""
|
|
|
|
|
|
|
|
import yt_dlp
|
|
|
|
|
|
|
|
|
|
|
|
class YtWrap:
|
|
|
|
"""wrap calls to yt"""
|
|
|
|
|
2022-05-24 03:00:40 +00:00
|
|
|
OBS_BASE = {
|
|
|
|
"default_search": "ytsearch",
|
|
|
|
"quiet": True,
|
|
|
|
"check_formats": "selected",
|
|
|
|
"socket_timeout": 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, obs_request):
|
|
|
|
self.obs_request = obs_request
|
|
|
|
self.build_obs()
|
|
|
|
|
|
|
|
def build_obs(self):
|
|
|
|
"""build yt-dlp obs"""
|
|
|
|
self.obs = self.OBS_BASE.copy()
|
|
|
|
self.obs.update(self.obs_request)
|
2022-05-24 01:53:05 +00:00
|
|
|
|
|
|
|
def download(self, url):
|
|
|
|
"""make download request"""
|
|
|
|
with yt_dlp.YoutubeDL(self.obs) as ydl:
|
|
|
|
try:
|
|
|
|
ydl.download([url])
|
|
|
|
except yt_dlp.utils.DownloadError:
|
2022-05-24 03:18:33 +00:00
|
|
|
print(f"{url}: failed to download.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2022-05-24 01:53:05 +00:00
|
|
|
|
|
|
|
def extract(self, url):
|
|
|
|
"""make extract request"""
|
2022-05-24 03:00:40 +00:00
|
|
|
try:
|
|
|
|
response = yt_dlp.YoutubeDL(self.obs).extract_info(url)
|
|
|
|
except (yt_dlp.utils.ExtractorError, yt_dlp.utils.DownloadError):
|
|
|
|
print(f"{url}: failed to get info from youtube")
|
|
|
|
response = False
|
2022-05-24 01:53:05 +00:00
|
|
|
|
|
|
|
return response
|