Skip to content

Commit a86a301

Browse files
committed
[Youtube] bare bones version to solve throttling
Done by transforming the parameter "n" from videoplayback urls ytdl-org/youtube-dl#29326 (comment)
1 parent c38a06e commit a86a301

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.time.OffsetDateTime;
4040
import java.time.format.DateTimeFormatter;
4141
import java.util.*;
42+
import java.util.regex.Pattern;
4243

4344
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
4445
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
@@ -527,9 +528,31 @@ public List<VideoStream> getVideoStreams() throws ExtractionException {
527528
final List<VideoStream> videoStreams = new ArrayList<>();
528529

529530
try {
531+
getDeobfuscationCode();
532+
final String playerCode = NewPipe.getDownloader()
533+
.get(playerJsUrl, getExtractorLocalization()).responseBody();
534+
Pattern pattern = Pattern.compile("b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");
535+
String functionName = Parser.matchGroup1(pattern, playerCode);
536+
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?;)\n", Pattern.DOTALL);
537+
String function = "function " + functionName + Parser.matchGroup1(functionPattern, playerCode);
538+
539+
Context context = Context.enter();
540+
context.setOptimizationLevel(-1);
541+
ScriptableObject scope = context.initSafeStandardObjects();
542+
530543
for (final Map.Entry<String, ItagItem> entry : getItags(FORMATS, ItagItem.ItagType.VIDEO).entrySet()) {
531544
final ItagItem itag = entry.getValue();
532-
final VideoStream videoStream = new VideoStream(entry.getKey(), false, itag);
545+
final String url = entry.getKey();
546+
Pattern nValuePattern = Pattern.compile("[&?]n=([^&]+)");
547+
String nValue = Parser.matchGroup1(nValuePattern, url);
548+
549+
context.evaluateString(scope, function, functionName, 1, null);
550+
final Function jsFunction = (Function) scope.get(functionName, scope);
551+
Object result = jsFunction.call(context, scope, scope, new Object[]{nValue});
552+
String newNValue = Objects.toString(result, nValue);
553+
String newUrl = nValuePattern.matcher(url).replaceFirst(newNValue);
554+
System.out.println("aaaaaa " + nValue + " - " + newNValue);
555+
final VideoStream videoStream = new VideoStream(newUrl, false, itag);
533556
if (!Stream.containSimilarStream(videoStream, videoStreams)) {
534557
videoStreams.add(videoStream);
535558
}

0 commit comments

Comments
 (0)