Skip to content

Commit

Permalink
[Youtube] bare bones version to solve throttling
Browse files Browse the repository at this point in the history
Done by transforming the parameter "n" from videoplayback urls
ytdl-org/youtube-dl#29326 (comment)
  • Loading branch information
XiangRongLin committed Jul 9, 2021
1 parent f324772 commit 02d6edd
Showing 1 changed file with 23 additions and 1 deletion.
Expand Up @@ -527,9 +527,31 @@ public List<VideoStream> getVideoStreams() throws ExtractionException {
final List<VideoStream> videoStreams = new ArrayList<>();

try {
getDeobfuscationCode();
final String playerCode = NewPipe.getDownloader()
.get(playerJsUrl, getExtractorLocalization()).responseBody();
Pattern pattern = Pattern.compile("b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");
String functionName = Parser.matchGroup1(pattern, playerCode);
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?;)\n", Pattern.DOTALL);
String function = "function " + functionName + Parser.matchGroup1(functionPattern, playerCode);

Context context = Context.enter();
context.setOptimizationLevel(-1);
ScriptableObject scope = context.initSafeStandardObjects();

for (final Map.Entry<String, ItagItem> entry : getItags(FORMATS, ItagItem.ItagType.VIDEO).entrySet()) {
final ItagItem itag = entry.getValue();
final VideoStream videoStream = new VideoStream(entry.getKey(), false, itag);
final String url = entry.getKey();
Pattern nValuePattern = Pattern.compile("[&?]n=([^&]+)");
String nValue = Parser.matchGroup1(nValuePattern, url);

context.evaluateString(scope, function, functionName, 1, null);
final Function jsFunction = (Function) scope.get(functionName, scope);
Object result = jsFunction.call(context, scope, scope, new Object[]{nValue});
String newNValue = Objects.toString(result, nValue);
String newUrl = nValuePattern.matcher(url).replaceFirst(newNValue);
System.out.println("aaaaaa " + nValue + " - " + newNValue);
final VideoStream videoStream = new VideoStream(newUrl, false, itag);
if (!Stream.containSimilarStream(videoStream, videoStreams)) {
videoStreams.add(videoStream);
}
Expand Down

0 comments on commit 02d6edd

Please sign in to comment.