From 5831eaa57f928a4d32772d012c4082e519f9a134 Mon Sep 17 00:00:00 2001 From: dirkf Date: Mon, 15 Jan 2024 18:32:06 +0000 Subject: [PATCH] [InfoExtractor] Support some warning and `._downloader` shortcut methods from yt-dlp --- youtube_dl/extractor/common.py | 48 +++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index 0eca9f84490..9e834d5c0df 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -596,6 +596,14 @@ def set_downloader(self, downloader): """Sets the downloader for this IE.""" self._downloader = downloader + @property + def cache(self): + return self._downloader.cache + + @property + def cookiejar(self): + return self._downloader.cookiejar + def _real_initialize(self): """Real initialization process. Redefine in subclasses.""" pass @@ -947,9 +955,38 @@ def report_warning(self, msg, video_id=None): self._downloader.report_warning( '[%s] %s%s' % (self.IE_NAME, idstr, msg)) + def __ie_msg(self, msg): + return '[{0}] {1}'.format(self.IE_NAME, msg) + def to_screen(self, msg): """Print msg to screen, prefixing it with '[ie_name]'""" - self._downloader.to_screen('[%s] %s' % (self.IE_NAME, msg)) + self._downloader.to_screen(self.__ie_msg(msg)) + + def write_debug(self, msg, only_once=False, _cache=[]): + '''Log debug message or Print message to stderr''' + if not self.get_param('verbose', False): + return + message = '[debug] ' + self.__ie_msg(msg) + logger = self.get_param('logger') + if logger: + logger.debug(message) + else: + if only_once and hash(message) in _cache: + return + self._downloader.to_stderr(message) + _cache.append(hash(message)) + + # name, default=None, *args, **kwargs + def get_param(self, name, *args, **kwargs): + default, args = (args[0], args[1:]) if len(args) > 0 else (kwargs.pop('default', None), args) + if self._downloader: + return self._downloader.params.get(name, default, *args, **kwargs) + return default + + def report_drm(self, video_id, partial=NO_DEFAULT): + if partial is not NO_DEFAULT: + self._downloader.deprecation_warning('InfoExtractor.report_drm no longer accepts the argument partial') + self.raise_no_formats('This video is DRM protected', expected=True, video_id=video_id) def report_extraction(self, id_or_name): """Report information extraction.""" @@ -977,6 +1014,15 @@ def raise_login_required(msg='This video is only available for registered users' def raise_geo_restricted(msg='This video is not available from your location due to geo restriction', countries=None): raise GeoRestrictedError(msg, countries=countries) + def raise_no_formats(self, msg, expected=False, video_id=None): + if expected and ( + self.get_param('ignore_no_formats_error') or self.get_param('wait_for_video')): + self.report_warning(msg, video_id) + elif isinstance(msg, ExtractorError): + raise msg + else: + raise ExtractorError(msg, expected=expected, video_id=video_id) + # Methods for following #608 @staticmethod def url_result(url, ie=None, video_id=None, video_title=None):