Fix YouTube throttling decrypter function parsing

This commit is contained in:
Stypox 2022-04-15 13:00:45 +02:00
parent bebdc55ad4
commit dcb7483dcf
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 19 additions and 15 deletions

View File

@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.utils.StringUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -36,7 +37,7 @@ public class YoutubeThrottlingDecrypter {
private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)"); private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)");
private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile( 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<>(); private static final Map<String, String> N_PARAMS_CACHE = new HashMap<>();
@SuppressWarnings("StaticVariableName") private static String FUNCTION; @SuppressWarnings("StaticVariableName") private static String FUNCTION;
@ -97,21 +98,24 @@ public class YoutubeThrottlingDecrypter {
private static String parseDecodeFunctionName(final String playerJsCode) private static String parseDecodeFunctionName(final String playerJsCode)
throws Parser.RegexException { throws Parser.RegexException {
String functionName = Parser.matchGroup1(FUNCTION_NAME_PATTERN, playerJsCode); final Matcher matcher = FUNCTION_NAME_PATTERN.matcher(playerJsCode);
final int arrayStartBrace = functionName.indexOf("["); final boolean foundMatch = matcher.find();
if (!foundMatch) {
if (arrayStartBrace > 0) { throw new Parser.RegexException("Failed to find pattern \""
final String arrayVarName = functionName.substring(0, arrayStartBrace); + FUNCTION_NAME_PATTERN + "\"");
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;
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 @Nonnull