self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50])
def test_builtins(self):
+ jsi = JSInterpreter('''
+ function x() { return NaN }
+ ''')
+ self.assertTrue(math.isnan(jsi.call_function('x')))
+
jsi = JSInterpreter('''
function x() { return new Date('Wednesday 31 December 1969 18:01:26 MDT') - 0; }
''')
tb = traceback.format_exc()
self._ydl.report_warning(f'Writing cache to {fn!r} failed: {tb}')
- def _validate(self, data, after):
+ def _validate(self, data, min_ver):
version = traverse_obj(data, 'yt-dlp_version')
if not version: # Backward compatibility
data, version = {'data': data}, '2022.08.19'
- if not after or version_tuple(version) > version_tuple(after):
+ if not min_ver or version_tuple(version) >= version_tuple(min_ver):
return data['data']
- self._ydl.write_debug(f'Discarding old cache from version {version} (need {after})')
+ self._ydl.write_debug(f'Discarding old cache from version {version} (needs {min_ver})')
- def load(self, section, key, dtype='json', default=None, *, after=None):
+ def load(self, section, key, dtype='json', default=None, *, min_ver=None):
assert dtype in ('json',)
if not self.enabled:
try:
with open(cache_fn, encoding='utf-8') as cachef:
self._ydl.write_debug(f'Loading {section}.{key} from cache')
- return self._validate(json.load(cachef), after)
+ return self._validate(json.load(cachef), min_ver)
except (ValueError, KeyError):
try:
file_size = os.path.getsize(cache_fn)
This class is experimental.
"""
+ INSTALL_HINT = 'Please download it from https://phantomjs.org/download.html'
+
_BASE_JS = R'''
phantom.onError = function(msg, trace) {{
var msgStack = ['PHANTOM ERROR: ' + msg];
self.exe = check_executable('phantomjs', ['-v'])
if not self.exe:
- raise ExtractorError(
- 'PhantomJS not found, Please download it from https://phantomjs.org/download.html', expected=True)
+ raise ExtractorError(f'PhantomJS not found, {self.INSTALL_HINT}', expected=True)
self.extractor = extractor
except Exception as e:
raise ExtractorError(f'{note} failed: Unable to run PhantomJS binary', cause=e)
if returncode:
- raise ExtractorError(f'{note} failed:\n{stderr.strip()}')
+ raise ExtractorError(f'{note} failed with returncode {returncode}:\n{stderr.strip()}')
return stdout
def _extract_n_function_code(self, video_id, player_url):
player_id = self._extract_player_info(player_url)
- func_code = self.cache.load('youtube-nsig', player_id, after='2022.08.19.1')
+ func_code = self.cache.load('youtube-nsig', player_id, min_ver='2022.08.19.2')
jscode = func_code or self._load_player(video_id, player_url)
jsi = JSInterpreter(jscode)
except ExtractorError as e:
phantomjs_hint = ''
if isinstance(e, JSInterpreter.Exception):
- phantomjs_hint = f' Install {self._downloader._format_err("PhantomJS", self._downloader.Styles.EMPHASIS)} to workaround the issue\n'
+ phantomjs_hint = (f' Install {self._downloader._format_err("PhantomJS", self._downloader.Styles.EMPHASIS)} '
+ f'to workaround the issue. {PhantomJSwrapper.INSTALL_HINT}\n')
self.report_warning(
f'nsig extraction failed: You may experience throttling for some formats\n{phantomjs_hint}'
f' n = {query["n"][0]} ; player = {player_url}', video_id=video_id, only_once=True)
def interpret_statement(self, stmt, local_vars, allow_recursion, *args, **kwargs):
if cls.ENABLED and stmt.strip():
cls.write(stmt, level=allow_recursion)
- ret, should_ret = f(self, stmt, local_vars, allow_recursion, *args, **kwargs)
+ try:
+ ret, should_ret = f(self, stmt, local_vars, allow_recursion, *args, **kwargs)
+ except Exception as e:
+ if cls.ENABLED:
+ if isinstance(e, ExtractorError):
+ e = e.orig_msg
+ cls.write('=> Raises:', e, '<-|', stmt, level=allow_recursion)
+ raise
if cls.ENABLED and stmt.strip():
cls.write(['->', '=>'][should_ret], repr(ret), '<-|', stmt, level=allow_recursion)
return ret, should_ret
@staticmethod
def _separate(expr, delim=',', max_split=None):
- OP_CHARS = '+-*/%&|^=<>!,;{}()[]:'
+ OP_CHARS = '+-*/%&|^=<>!,;{}:'
if not expr:
return
counters = {k: 0 for k in _MATCHING_PARENS.values()}
(?P<op>{"|".join(map(re.escape, set(_OPERATORS) - _COMP_OPERATORS))})?
=(?!=)(?P<expr>.*)$
)|(?P<return>
- (?!if|return|true|false|null|undefined)(?P<name>{_NAME_RE})$
+ (?!if|return|true|false|null|undefined|NaN)(?P<name>{_NAME_RE})$
)|(?P<indexing>
(?P<in>{_NAME_RE})\[(?P<idx>.+)\]$
)|(?P<attribute>
raise JS_Continue()
elif expr == 'undefined':
return JS_Undefined, should_return
+ elif expr == 'NaN':
+ return float('NaN'), should_return
elif m and m.group('return'):
return local_vars.get(m.group('name'), JS_Undefined), should_return
global_stack[0].update(itertools.zip_longest(argnames, args, fillvalue=None))
global_stack[0].update(kwargs)
var_stack = LocalNameSpace(*global_stack)
- ret, should_abort = self.interpret_statement(code.replace('\n', ''), var_stack, allow_recursion - 1)
+ ret, should_abort = self.interpret_statement(code.replace('\n', ' '), var_stack, allow_recursion - 1)
if should_abort:
return ret
return resf