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 2 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,19 @@ 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);
int arrayStartBrace = functionName.indexOf("[");

if (arrayStartBrace > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are both paths of the if case covered by tests? Because I don't quite understand why this is needed and cannot be adjusted in the regex

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition is always true for the tests we have currently

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested a few more video ids and the condition was still true. However, I do not see a reason to remove the condition. I'd keep it as it is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branching doesn't depend on video, but on the player version. To test the other case, you have to force an older player like https://www.youtube.com/s/player/8040e515/player_ias.vflset/en_US/base.js

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

@Nonnull
Expand Down