1
1
package org .schabi .newpipe .extractor .services .youtube ;
2
2
3
+ import static org .schabi .newpipe .extractor .utils .Parser .matchMultiplePatterns ;
4
+
3
5
import org .schabi .newpipe .extractor .exceptions .ParsingException ;
4
6
import org .schabi .newpipe .extractor .utils .JavaScript ;
5
7
import org .schabi .newpipe .extractor .utils .Parser ;
@@ -18,10 +20,33 @@ final class YoutubeThrottlingParameterUtils {
18
20
19
21
private static final Pattern THROTTLING_PARAM_PATTERN = Pattern .compile ("[&?]n=([^&]+)" );
20
22
21
- private static final Pattern DEOBFUSCATION_FUNCTION_NAME_PATTERN = Pattern .compile (
22
- // CHECKSTYLE:OFF
23
- "\\ .get\\ (\" n\" \\ )\\ )&&\\ ([a-zA-Z0-9$_]=([a-zA-Z0-9$_]+)(?:\\ [(\\ d+)])?\\ ([a-zA-Z0-9$_]\\ )" );
24
- // CHECKSTYLE:ON
23
+ private static final String SINGLE_CHAR_VARIABLE_REGEX = "[a-zA-Z0-9$_]" ;
24
+
25
+ private static final String FUNCTION_NAME_REGEX = SINGLE_CHAR_VARIABLE_REGEX + "+" ;
26
+
27
+ private static final String ARRAY_ACCESS_REGEX = "\\ [(\\ d+)]" ;
28
+
29
+ /**
30
+ * The first regex matches this, where we want BDa:
31
+ * <p>
32
+ * (b=String.fromCharCode(110),c=a.get(b))&&(c=<strong>BDa</strong><strong>[0]</strong>(c)
33
+ * <p>
34
+ * Array access is optional, but needs to be handled, since the actual function is inside the
35
+ * array.
36
+ */
37
+ // CHECKSTYLE:OFF
38
+ private static final Pattern [] DEOBFUSCATION_FUNCTION_NAME_REGEXES = {
39
+ Pattern .compile ("\\ (" + SINGLE_CHAR_VARIABLE_REGEX + "=String\\ .fromCharCode\\ (110\\ ),"
40
+ + SINGLE_CHAR_VARIABLE_REGEX + "=" + SINGLE_CHAR_VARIABLE_REGEX + "\\ .get\\ ("
41
+ + SINGLE_CHAR_VARIABLE_REGEX + "\\ )\\ )" + "&&\\ (" + SINGLE_CHAR_VARIABLE_REGEX
42
+ + "=(" + FUNCTION_NAME_REGEX + ")" + "(?:" + ARRAY_ACCESS_REGEX + ")?\\ ("
43
+ + SINGLE_CHAR_VARIABLE_REGEX + "\\ )" ),
44
+ Pattern .compile ("\\ .get\\ (\" n\" \\ )\\ )&&\\ (" + SINGLE_CHAR_VARIABLE_REGEX
45
+ + "=(" + FUNCTION_NAME_REGEX + ")(?:" + ARRAY_ACCESS_REGEX + ")?\\ ("
46
+ + SINGLE_CHAR_VARIABLE_REGEX + "\\ )" ),
47
+ };
48
+ // CHECKSTYLE:ON
49
+
25
50
26
51
// Escape the curly end brace to allow compatibility with Android's regex engine
27
52
// See https://stackoverflow.com/q/45074813
@@ -48,11 +73,13 @@ private YoutubeThrottlingParameterUtils() {
48
73
@ Nonnull
49
74
static String getDeobfuscationFunctionName (@ Nonnull final String javaScriptPlayerCode )
50
75
throws ParsingException {
51
- final Matcher matcher = DEOBFUSCATION_FUNCTION_NAME_PATTERN .matcher (javaScriptPlayerCode );
52
- if (!matcher .find ()) {
53
- throw new ParsingException ("Failed to find deobfuscation function name pattern \" "
54
- + DEOBFUSCATION_FUNCTION_NAME_PATTERN
55
- + "\" in the base JavaScript player code" );
76
+ final Matcher matcher ;
77
+ try {
78
+ matcher = matchMultiplePatterns (DEOBFUSCATION_FUNCTION_NAME_REGEXES ,
79
+ javaScriptPlayerCode );
80
+ } catch (final Parser .RegexException e ) {
81
+ throw new ParsingException ("Could not find deobfuscation function with any of the "
82
+ + "known patterns in the base JavaScript player code" , e );
56
83
}
57
84
58
85
final String functionName = matcher .group (1 );
0 commit comments