Skip to content

Commit

Permalink
Merge pull request TeamNewPipe#832 from Stypox/hotfix-decrypter
Browse files Browse the repository at this point in the history
Fix YouTube throttling decrypter function parsing
  • Loading branch information
TobiGr authored Apr 15, 2022
2 parents bebdc55 + dcb7483 commit b77c72f
Showing 1 changed file with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -36,7 +37,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=(\\S+)\\(b\\),a\\.set\\(\"n\",b\\)");
"\\.get\\(\"n\"\\)\\)&&\\(b=([a-zA-Z0-9$]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9]\\)");

private static final Map<String, String> N_PARAMS_CACHE = new HashMap<>();
@SuppressWarnings("StaticVariableName") private static String FUNCTION;
Expand Down Expand Up @@ -97,21 +98,24 @@ public static String apply(final String url, final String videoId) throws Parsin

private static String parseDecodeFunctionName(final String playerJsCode)
throws Parser.RegexException {
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];
final Matcher matcher = FUNCTION_NAME_PATTERN.matcher(playerJsCode);
final boolean foundMatch = matcher.find();
if (!foundMatch) {
throw new Parser.RegexException("Failed to find pattern \""
+ FUNCTION_NAME_PATTERN + "\"");
}
return functionName;

final String functionName = matcher.group(1);
if (matcher.groupCount() == 1) {
return functionName;
}

final int arrayNum = Integer.parseInt(matcher.group(2));
final Pattern arrayPattern = Pattern.compile(
"var " + Pattern.quote(functionName) + "\\s*=\\s*\\[(.+?)];");
final String arrayStr = Parser.matchGroup1(arrayPattern, playerJsCode);
final String[] names = arrayStr.split(",");
return names[arrayNum];
}

@Nonnull
Expand Down

0 comments on commit b77c72f

Please sign in to comment.