Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex error when parsing the Youtube JavaScript code #785

Merged
merged 3 commits into from
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class YoutubeThrottlingDecrypter {

private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)");
private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile(
"b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");
"b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\S+)\\(b\\),a\\.set\\(\"n\",b\\)");

private static final Map<String, String> nParams = new HashMap<>();

Expand Down Expand Up @@ -66,7 +66,21 @@ public YoutubeThrottlingDecrypter() throws ParsingException {

private String parseDecodeFunctionName(final String playerJsCode)
throws Parser.RegexException {
return Parser.matchGroup1(FUNCTION_NAME_PATTERN, playerJsCode);
String functionName = Parser.matchGroup1(FUNCTION_NAME_PATTERN, playerJsCode);
final int arrayStartBrace = functionName.indexOf("[");

if (arrayStartBrace > 0) {
final String arrayVarName = functionName.substring(0, arrayStartBrace);
final String order = functionName.substring(
arrayStartBrace + 1, functionName.indexOf("]"));
final int arrayNum = Integer.parseInt(order);
final Pattern arrayPattern = Pattern.compile(
String.format("var %s=\\[(.+?)\\];", arrayVarName));
final String arrayStr = Parser.matchGroup1(arrayPattern, playerJsCode);
final String[] names = arrayStr.split(",");
functionName = names[arrayNum];
}
return functionName;
}

@Nonnull
Expand All @@ -87,15 +101,15 @@ private String parseWithParenthesisMatching(final String playerJsCode, final Str

@Nonnull
private String parseWithRegex(final String playerJsCode, final String functionName) throws Parser.RegexException {
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?}};)\n",
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?}};)\n",
Pattern.DOTALL);
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
}

public String apply(final String url) throws Parser.RegexException {
if (containsNParam(url)) {
String oldNParam = parseNParam(url);
String newNParam = decryptNParam(oldNParam);
final String oldNParam = parseNParam(url);
final String newNParam = decryptNParam(oldNParam);
return replaceNParam(url, oldNParam, newNParam);
} else {
return url;
Expand Down