Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YouTube] Realign n-sig processing with yt-dlp #32695

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
compat_urllib_parse_unquote,
compat_urllib_parse_unquote_plus,
compat_urllib_parse_urlencode,
compat_urllib_request,
)


Expand Down Expand Up @@ -135,6 +136,19 @@ def test_compat_casefold(self):
self.assertEqual(compat_casefold('\u03a3'), '\u03c3')
self.assertEqual(compat_casefold('A\u0345\u03a3'), 'a\u03b9\u03c3')

def test_compat_urllib_request_Request(self):
self.assertEqual(
compat_urllib_request.Request('http://127.0.0.1', method='PUT').get_method(),
'PUT')

class PUTrequest(compat_urllib_request.Request):
def get_method(self):
return 'PUT'

self.assertEqual(
PUTrequest('http://127.0.0.1').get_method(),
'PUT')


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions test/test_subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class TestNRKSubtitles(BaseTestSubtitles):
def test_allsubtitles(self):
self.DL.params['writesubtitles'] = True
self.DL.params['allsubtitles'] = True
self.DL.params['format'] = 'best/bestvideo'
subtitles = self.getSubtitles()
self.assertEqual(set(subtitles.keys()), set(['nb-ttv']))
self.assertEqual(md5(subtitles['nb-ttv']), '67e06ff02d0deaf975e68f6cb8f6a149')
Expand Down
27 changes: 17 additions & 10 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,26 @@

# Also fix up lack of method arg in old Pythons
try:
_req = compat_urllib_request.Request
_req('http://127.0.0.1', method='GET')
type(compat_urllib_request.Request('http://127.0.0.1', method='GET'))
except TypeError:
class _request(object):
def __new__(cls, url, *args, **kwargs):
method = kwargs.pop('method', None)
r = _req(url, *args, **kwargs)
if method:
r.get_method = types.MethodType(lambda _: method, r)
return r
def _add_init_method_arg(cls):

compat_urllib_request.Request = _request
init = cls.__init__

def wrapped_init(self, *args, **kwargs):
method = kwargs.pop('method', 'GET')
init(self, *args, **kwargs)
if any(callable(x.__dict__.get('get_method')) for x in (self.__class__, self) if x != cls):
# allow instance or its subclass to override get_method()
return
if self.has_data() and method == 'GET':
method = 'POST'
self.get_method = types.MethodType(lambda _: method, self)

cls.__init__ = wrapped_init

_add_init_method_arg(compat_urllib_request.Request)
del _add_init_method_arg

try:
import urllib.error as compat_urllib_error
Expand Down
56 changes: 53 additions & 3 deletions youtube_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -942,14 +950,47 @@ def _parse_json(self, json_string, video_id, transform_source=None, fatal=True):
else:
self.report_warning(errmsg + str(ve))

def report_warning(self, msg, video_id=None):
def __ie_msg(self, *msg):
return '[{0}] {1}'.format(self.IE_NAME, ''.join(msg))

# msg, video_id=None, *args, only_once=False, **kwargs
def report_warning(self, msg, *args, **kwargs):
if len(args) > 0:
video_id = args[0]
args = args[1:]
else:
video_id = kwargs.pop('video_id', None)
idstr = '' if video_id is None else '%s: ' % video_id
self._downloader.report_warning(
'[%s] %s%s' % (self.IE_NAME, idstr, msg))
self.__ie_msg(idstr, msg), *args, **kwargs)

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):
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."""
Expand Down Expand Up @@ -977,6 +1018,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):
Expand Down
Loading