Skip to content

Commit b77c72f

Browse files
authored
Merge pull request TeamNewPipe#832 from Stypox/hotfix-decrypter
Fix YouTube throttling decrypter function parsing
2 parents bebdc55 + dcb7483 commit b77c72f

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.annotation.Nonnull;
99
import java.util.HashMap;
1010
import java.util.Map;
11+
import java.util.regex.Matcher;
1112
import java.util.regex.Pattern;
1213

1314
/**
@@ -36,7 +37,7 @@ public class YoutubeThrottlingDecrypter {
3637

3738
private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)");
3839
private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile(
39-
"b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\S+)\\(b\\),a\\.set\\(\"n\",b\\)");
40+
"\\.get\\(\"n\"\\)\\)&&\\(b=([a-zA-Z0-9$]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9]\\)");
4041

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

9899
private static String parseDecodeFunctionName(final String playerJsCode)
99100
throws Parser.RegexException {
100-
String functionName = Parser.matchGroup1(FUNCTION_NAME_PATTERN, playerJsCode);
101-
final int arrayStartBrace = functionName.indexOf("[");
102-
103-
if (arrayStartBrace > 0) {
104-
final String arrayVarName = functionName.substring(0, arrayStartBrace);
105-
final String order = functionName.substring(
106-
arrayStartBrace + 1, functionName.indexOf("]"));
107-
final int arrayNum = Integer.parseInt(order);
108-
final Pattern arrayPattern = Pattern.compile(
109-
String.format("var %s=\\[(.+?)\\];", arrayVarName));
110-
final String arrayStr = Parser.matchGroup1(arrayPattern, playerJsCode);
111-
final String[] names = arrayStr.split(",");
112-
functionName = names[arrayNum];
101+
final Matcher matcher = FUNCTION_NAME_PATTERN.matcher(playerJsCode);
102+
final boolean foundMatch = matcher.find();
103+
if (!foundMatch) {
104+
throw new Parser.RegexException("Failed to find pattern \""
105+
+ FUNCTION_NAME_PATTERN + "\"");
113106
}
114-
return functionName;
107+
108+
final String functionName = matcher.group(1);
109+
if (matcher.groupCount() == 1) {
110+
return functionName;
111+
}
112+
113+
final int arrayNum = Integer.parseInt(matcher.group(2));
114+
final Pattern arrayPattern = Pattern.compile(
115+
"var " + Pattern.quote(functionName) + "\\s*=\\s*\\[(.+?)];");
116+
final String arrayStr = Parser.matchGroup1(arrayPattern, playerJsCode);
117+
final String[] names = arrayStr.split(",");
118+
return names[arrayNum];
115119
}
116120

117121
@Nonnull

0 commit comments

Comments
 (0)