@@ -1878,6 +1878,20 @@ def _decrypt_nsig(self, n, video_id, player_url):
1878
1878
1879
1879
def _extract_n_function_name (self , jscode ):
1880
1880
func_name , idx = None , None
1881
+
1882
+ def generic_n_function_search (func_name = None ):
1883
+ return self ._search_regex (
1884
+ r'''(?xs)
1885
+ (?:(?<=[^\w$])|^) # instead of \b, which ignores $
1886
+ (?P<name>%s)\s*=\s*function\((?!\d)[a-zA-Z\d_$]+\)
1887
+ \s*\{(?:(?!};).)+?(?:
1888
+ ["']enhanced_except_ |
1889
+ return\s*(?P<q>"|')[a-zA-Z\d-]+_w8_(?P=q)\s*\+\s*[\w$]+
1890
+ )
1891
+ ''' % (func_name or r'(?!\d)[a-zA-Z\d_$]+' ,), jscode ,
1892
+ 'Initial JS player n function name' , group = 'name' ,
1893
+ default = None if func_name else NO_DEFAULT )
1894
+
1881
1895
# these special cases are redundant and probably obsolete (2025-04):
1882
1896
# they make the tests run ~10% faster without fallback warnings
1883
1897
r"""
@@ -1918,26 +1932,22 @@ def _extract_n_function_name(self, jscode):
1918
1932
(?(idx)|\[\s*)(?P<nfunc>(?!\d)[\w$]+)(?(idx)|\s*\])
1919
1933
\s*?[;\n]
1920
1934
''' , jscode ):
1921
- func_name = self ._search_regex (
1935
+ fn = self ._search_regex (
1922
1936
r'[;,]\s*(function\s+)?({0})(?(1)|\s*=\s*function)\s*\((?!\d)[\w$]+\)\s*\{1}(?!\s*return\s)' .format (
1923
1937
re .escape (m .group ('nfunc' )), '{' ),
1924
1938
jscode , 'Initial JS player n function name (2)' , group = 2 , default = None )
1925
- if func_name :
1939
+ if fn :
1940
+ func_name = fn
1926
1941
idx = m .group ('idx' )
1927
- break
1942
+ if generic_n_function_search (func_name ):
1943
+ # don't look any further
1944
+ break
1928
1945
1929
1946
# thx bashonly: yt-dlp/yt-dlp/pull/10611
1930
1947
if not func_name :
1931
1948
self .report_warning ('Falling back to generic n function search' , only_once = True )
1932
- return self ._search_regex (
1933
- r'''(?xs)
1934
- (?:(?<=[^\w$])|^) # instead of \b, which ignores $
1935
- (?P<name>(?!\d)[a-zA-Z\d_$]+)\s*=\s*function\((?!\d)[a-zA-Z\d_$]+\)
1936
- \s*\{(?:(?!};).)+?(?:
1937
- ["']enhanced_except_ |
1938
- return\s*(?P<q>"|')[a-zA-Z\d-]+_w8_(?P=q)\s*\+\s*[\w$]+
1939
- )
1940
- ''' , jscode , 'Initial JS player n function name' , group = 'name' )
1949
+ return generic_n_function_search ()
1950
+
1941
1951
if not idx :
1942
1952
return func_name
1943
1953
0 commit comments