Skip to content

Commit f7bbf5a

Browse files
authored
[ie/youtube] nsig code improvements and cleanup (yt-dlp#13280)
Authored by: bashonly
1 parent b5be29f commit f7bbf5a

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

yt_dlp/extractor/youtube/_video.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ def _decrypt_nsig(self, s, video_id, player_url):
22282228

22292229
def _extract_n_function_name(self, jscode, player_url=None):
22302230
varname, global_list = self._interpret_player_js_global_var(jscode, player_url)
2231-
if debug_str := traverse_obj(global_list, (lambda _, v: v.endswith('_w8_'), any)):
2231+
if debug_str := traverse_obj(global_list, (lambda _, v: v.endswith('-_w8_'), any)):
22322232
funcname = self._search_regex(
22332233
r'''(?xs)
22342234
[;\n](?:
@@ -2289,8 +2289,8 @@ def _extract_n_function_name(self, jscode, player_url=None):
22892289
rf'var {re.escape(funcname)}\s*=\s*(\[.+?\])\s*[,;]', jscode,
22902290
f'Initial JS player n function list ({funcname}.{idx})')))[int(idx)]
22912291

2292-
def _extract_player_js_global_var(self, jscode, player_url):
2293-
"""Returns tuple of strings: variable assignment code, variable name, variable value code"""
2292+
def _interpret_player_js_global_var(self, jscode, player_url):
2293+
"""Returns tuple of: variable name string, variable value list"""
22942294
extract_global_var = self._cached(self._search_regex, 'js global array', player_url)
22952295
varcode, varname, varvalue = extract_global_var(
22962296
r'''(?x)
@@ -2308,27 +2308,23 @@ def _extract_player_js_global_var(self, jscode, player_url):
23082308
self.write_debug(join_nonempty(
23092309
'No global array variable found in player JS',
23102310
player_url and f' player = {player_url}', delim='\n'), only_once=True)
2311-
return varcode, varname, varvalue
2311+
return None, None
23122312

2313-
def _interpret_player_js_global_var(self, jscode, player_url):
2314-
"""Returns tuple of: variable name string, variable value list"""
2315-
_, varname, array_code = self._extract_player_js_global_var(jscode, player_url)
2316-
jsi = JSInterpreter(array_code)
2313+
jsi = JSInterpreter(varcode)
23172314
interpret_global_var = self._cached(jsi.interpret_expression, 'js global list', player_url)
2318-
return varname, interpret_global_var(array_code, {}, allow_recursion=10)
2315+
return varname, interpret_global_var(varvalue, {}, allow_recursion=10)
23192316

23202317
def _fixup_n_function_code(self, argnames, nsig_code, jscode, player_url):
2321-
varcode, varname, _ = self._extract_player_js_global_var(jscode, player_url)
2322-
if varcode and varname:
2323-
nsig_code = varcode + '; ' + nsig_code
2324-
_, global_list = self._interpret_player_js_global_var(jscode, player_url)
2318+
varname, global_list = self._interpret_player_js_global_var(jscode, player_url)
2319+
if varname and global_list:
2320+
nsig_code = f'var {varname}={json.dumps(global_list)}; {nsig_code}'
23252321
else:
23262322
varname = 'dlp_wins'
23272323
global_list = []
23282324

23292325
undefined_idx = global_list.index('undefined') if 'undefined' in global_list else r'\d+'
23302326
fixed_code = re.sub(
2331-
rf'''(?x)
2327+
fr'''(?x)
23322328
;\s*if\s*\(\s*typeof\s+[a-zA-Z0-9_$]+\s*===?\s*(?:
23332329
(["\'])undefined\1|
23342330
{re.escape(varname)}\[{undefined_idx}\]

0 commit comments

Comments
 (0)