]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/youtube.py
[test] Fix test_youtube_signature
[yt-dlp.git] / yt_dlp / extractor / youtube.py
index fd62d716a2feb85b682b6c4aea812450845ee4ab..59449278deb2f8768a4d5475ba5acf3158bd5039 100644 (file)
@@ -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<nfunc>[a-zA-Z0-9$]+)(?:\[(?P<idx>\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<nfunc>[a-zA-Z0-9$]+)(?:\[(?P<idx>\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