feat: catching yt-dl errors

This commit is contained in:
Hazel 2024-04-12 16:00:51 +02:00
parent 6936c9da9d
commit 70b86b5c47

View File

@ -11,6 +11,7 @@ from functools import lru_cache
import youtube_dl import youtube_dl
from youtube_dl.extractor.youtube import YoutubeIE from youtube_dl.extractor.youtube import YoutubeIE
from youtube_dl.utils import DownloadError
from ...utils.exception.config import SettingValueError from ...utils.exception.config import SettingValueError
from ...utils.config import main_settings, youtube_settings, logging_settings from ...utils.config import main_settings, youtube_settings, logging_settings
@ -201,6 +202,7 @@ class YoutubeMusic(SuperYouTube):
self.yt_ie = MusicKrakenYoutubeIE(downloader=self.ydl, main_instance=self) self.yt_ie = MusicKrakenYoutubeIE(downloader=self.ydl, main_instance=self)
self.download_values_by_url: dict = {} self.download_values_by_url: dict = {}
self.not_download: Dict[str, DownloadError] = {}
def _fetch_from_main_page(self): def _fetch_from_main_page(self):
""" """
@ -483,7 +485,13 @@ class YoutubeMusic(SuperYouTube):
def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song: def fetch_song(self, source: Source, stop_at_level: int = 1) -> Song:
ydl_res: dict = self.ydl.extract_info(url=source.url, download=False) ydl_res: dict = {}
try:
ydl_res: dict = self.ydl.extract_info(url=source.url, download=False)
except DownloadError as e:
self.not_download[source.hash_url] = e
self.LOGGER.error(f"Couldn't fetch song from {source.url}. {e}")
return Song()
self.fetch_media_url(source=source, ydl_res=ydl_res) self.fetch_media_url(source=source, ydl_res=ydl_res)
@ -556,17 +564,20 @@ class YoutubeMusic(SuperYouTube):
def download_song_to_target(self, source: Source, target: Target, desc: str = None) -> DownloadResult: def download_song_to_target(self, source: Source, target: Target, desc: str = None) -> DownloadResult:
media = self.fetch_media_url(source) media = self.fetch_media_url(source)
result = self.download_connection.stream_into( if source.hash_url not in self.not_download:
media["url"], result = self.download_connection.stream_into(
target, media["url"],
name=desc, target,
raw_url=True, name=desc,
raw_headers=True, raw_url=True,
disable_cache=True, raw_headers=True,
headers=media.get("headers", {}), disable_cache=True,
# chunk_size=media.get("chunk_size", main_settings["chunk_size"]), headers=media.get("headers", {}),
method="GET", # chunk_size=media.get("chunk_size", main_settings["chunk_size"]),
) method="GET",
)
else:
result = DownloadResult(error_message=str(self.not_download[source.hash_url]))
if result.is_fatal_error: if result.is_fatal_error:
result.merge(super().download_song_to_target(source=source, target=target, desc=desc)) result.merge(super().download_song_to_target(source=source, target=target, desc=desc))