From 90a1df305b628c78a497cf4010fb68cad856a314 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Sun, 21 Aug 2022 00:51:03 +0530 Subject: [PATCH] [test] Fix test_youtube_signature --- test/test_youtube_signature.py | 4 ++++ yt_dlp/extractor/youtube.py | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/test/test_youtube_signature.py b/test/test_youtube_signature.py index f1859a2fc..4b526ff2e 100644 --- a/test/test_youtube_signature.py +++ b/test/test_youtube_signature.py @@ -110,6 +110,10 @@ 'https://www.youtube.com/s/player/1f7d5369/player_ias.vflset/en_US/base.js', 'batNX7sYqIJdkJ', 'IhOkL_zxbkOZBw', ), + ( + 'https://www.youtube.com/s/player/009f1d77/player_ias.vflset/en_US/base.js', + '5dwFHw8aFWQUQtffRq', 'audescmLUzI3jw', + ), ] diff --git a/yt_dlp/extractor/youtube.py b/yt_dlp/extractor/youtube.py index fd62d716a..59449278d 100644 --- a/yt_dlp/extractor/youtube.py +++ b/yt_dlp/extractor/youtube.py @@ -2646,6 +2646,17 @@ def _decrypt_nsig(self, s, video_id, player_url): self.write_debug(f'Decrypted nsig {s} => {ret}') return ret + def _extract_n_function_name(self, jscode): + funcname, idx = self._search_regex( + r'\.get\("n"\)\)&&\(b=(?P[a-zA-Z0-9$]+)(?:\[(?P\d+)\])?\([a-zA-Z0-9]\)', + jscode, 'Initial JS player n function name', group=('nfunc', 'idx')) + if not idx: + return funcname + + return json.loads(js_to_json(self._search_regex( + rf'var {re.escape(funcname)}\s*=\s*(\[.+?\]);', jscode, + f'Initial JS player n function list ({funcname}.{idx})')))[int(idx)] + 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) @@ -2655,15 +2666,7 @@ def _extract_n_function_code(self, video_id, player_url): if func_code: return jsi, player_id, func_code - funcname, idx = self._search_regex( - r'\.get\("n"\)\)&&\(b=(?P[a-zA-Z0-9$]+)(?:\[(?P\d+)\])?\([a-zA-Z0-9]\)', - jscode, 'Initial JS player n function name', group=('nfunc', 'idx')) - if idx: - funcname = json.loads(js_to_json(self._search_regex( - rf'var {re.escape(funcname)}\s*=\s*(\[.+?\]);', jscode, - f'Initial JS player n function list ({funcname}.{idx})')))[int(idx)] - - func_code = jsi.extract_function_code(funcname) + func_code = jsi.extract_function_code(self._extract_n_function_name(jscode)) self.cache.store('youtube-nsig', player_id, func_code) return jsi, player_id, func_code