Skip to content

Commit abcee87

Browse files
committed
[YouTube] Fix throttling parameter decryption function regex
- Quote the function name, as it may contain special regex symbols, such as dollar; - Support multiple lines; - Use what looks like the end of the function for the end of the regex (this part is inspired from yt-dlp throttling parameter decryption regex); - Move the throttling function body regex into a private and static constant.
1 parent 7244be7 commit abcee87

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingDecrypter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public final class YoutubeThrottlingDecrypter {
4040
private static final Pattern DECRYPT_FUNCTION_NAME_PATTERN = Pattern.compile(
4141
"\\.get\\(\"n\"\\)\\)&&\\(b=([a-zA-Z0-9$]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9]\\)");
4242

43+
// Escape the curly end brace to allow compatibility with Android's regex engine
44+
// See https://stackoverflow.com/q/45074813
45+
@SuppressWarnings("RegExpRedundantEscape")
46+
private static final String DECRYPT_FUNCTION_BODY_REGEX =
47+
"=\\s*function([\\S\\s]*?\\}\\s*return [\\w$]+?\\.join\\(\"\"\\)\\s*\\};)";
48+
4349
private static final Map<String, String> N_PARAMS_CACHE = new HashMap<>();
4450
private static String decryptFunction;
4551
private static String decryptFunctionName;
@@ -128,11 +134,9 @@ private static String parseDecodeFunction(final String playerJsCode, final Strin
128134
@Nonnull
129135
private static String parseWithRegex(final String playerJsCode, final String functionName)
130136
throws Parser.RegexException {
131-
// Escape the curly end brace to allow compatibility with Android's regex engine
132-
// See https://stackoverflow.com/q/45074813
133-
//noinspection RegExpRedundantEscape
134-
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?\\};)\n",
135-
Pattern.DOTALL);
137+
// Quote the function name, as it may contain special regex characters such as dollar
138+
final Pattern functionPattern = Pattern.compile(
139+
Pattern.quote(functionName) + DECRYPT_FUNCTION_BODY_REGEX, Pattern.DOTALL);
136140
return validateFunction("function "
137141
+ functionName
138142
+ Parser.matchGroup1(functionPattern, playerJsCode));

0 commit comments

Comments
 (0)