Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.utils.JavaScript;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.StringUtil;

import javax.annotation.Nonnull;
import java.util.HashMap;
Expand Down Expand Up @@ -33,7 +34,10 @@
*/
public class YoutubeThrottlingDecrypter {

private static final String N_PARAM_REGEX = "[&?]n=([^&]+)";
private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)");
private static final Pattern FUNCTION_NAME_PATTERN = Pattern.compile(
"b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");

private static final Map<String, String> nParams = new HashMap<>();

private final String functionName;
Expand Down Expand Up @@ -62,15 +66,26 @@ public YoutubeThrottlingDecrypter() throws ParsingException {

private String parseDecodeFunctionName(final String playerJsCode)
throws Parser.RegexException {
Pattern pattern = Pattern.compile(
"b=a\\.get\\(\"n\"\\)\\)&&\\(b=(\\w+)\\(b\\),a\\.set\\(\"n\",b\\)");
return Parser.matchGroup1(pattern, playerJsCode);
return Parser.matchGroup1(FUNCTION_NAME_PATTERN, playerJsCode);
}

@Nonnull
private String parseDecodeFunction(final String playerJsCode, final String functionName)
throws Parser.RegexException {
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?;)\n",
try {
return parseWithParenthesisMatching(playerJsCode, functionName);
} catch (Exception e) {
return parseWithRegex(playerJsCode, functionName);
}
}

private String parseWithParenthesisMatching(final String playerJsCode, final String functionName) {
final String functionBase = functionName + "=function";
return functionBase + StringUtil.matchToClosingParenthesis(playerJsCode, functionBase) + ";";
}

private String parseWithRegex(final String playerJsCode, final String functionName) throws Parser.RegexException {
Pattern functionPattern = Pattern.compile(functionName + "=function(.*?}};)\n",
Pattern.DOTALL);
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
}
Expand All @@ -86,12 +101,11 @@ public String apply(final String url) throws Parser.RegexException {
}

private boolean containsNParam(final String url) {
return Parser.isMatch(N_PARAM_REGEX, url);
return Parser.isMatch(N_PARAM_PATTERN, url);
}

private String parseNParam(final String url) throws Parser.RegexException {
Pattern nValuePattern = Pattern.compile(N_PARAM_REGEX);
return Parser.matchGroup1(nValuePattern, url);
return Parser.matchGroup1(N_PARAM_PATTERN, url);
}

private String decryptNParam(final String nParam) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public static boolean isMatch(String pattern, String input) {
return mat.find();
}

public static boolean isMatch(Pattern pattern, String input) {
Matcher mat = pattern.matcher(input);
Comment thread
XiangRongLin marked this conversation as resolved.
Outdated
return mat.find();
}

public static Map<String, String> compatParseMap(final String input) throws UnsupportedEncodingException {
Map<String, String> map = new HashMap<>();
for (String arg : input.split("&")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.schabi.newpipe.extractor.utils;

import edu.umd.cs.findbugs.annotations.NonNull;
Comment thread
XiangRongLin marked this conversation as resolved.
Outdated

public class StringUtil {
Comment thread
XiangRongLin marked this conversation as resolved.
Outdated

private StringUtil() {
}

/**
* @param string The string to search in
* @param start A string from which to start searching.
* @return A substring where each '{' matches a '}'
* @throws IndexOutOfBoundsException If {@ string} does not contain {@code start}
*/
@NonNull
public static String matchToClosingParenthesis(@NonNull final String string, @NonNull final String start) {
int startIndex = string.indexOf(start);
if (startIndex < 0) {
throw new IndexOutOfBoundsException();
}

startIndex += start.length();
int endIndex = startIndex;
while (string.charAt(endIndex) != '{') {
++endIndex;
}
++endIndex;

int openParenthesis = 1;
while (openParenthesis > 0) {
switch (string.charAt(endIndex)) {
case '{':
++openParenthesis;
break;
case '}':
--openParenthesis;
break;
default:
break;
}
++endIndex;
}

return string.substring(startIndex, endIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.EvaluatorException;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
Expand All @@ -11,6 +12,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

public class YoutubeThrottlingDecrypterTest {

Expand All @@ -19,6 +21,22 @@ public void setup() throws IOException {
NewPipe.init(DownloaderTestImpl.getInstance());
}

@Test
public void testExtractFunction__success() throws ParsingException {
String[] videoIds = {"jE1USQrs1rw", "CqxjzfudGAc", "goH-9MfQI7w", "KYIdr_7H5Yw", "J1WeqmGbYeI"};
Comment thread
XiangRongLin marked this conversation as resolved.
Outdated

final String encryptedUrl = "https://r6---sn-4g5ednek.googlevideo.com/videoplayback?expire=1626562120&ei=6AnzYO_YBpql1gLGkb_IBQ&ip=127.0.0.1&id=o-ANhBEf36Z5h-8U9DDddtPDqtS0ZNwf0XJAAigudKI2uI&itag=278&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&source=youtube&requiressl=yes&vprv=1&mime=video%2Fwebm&ns=TvecOReN0vPuXb3j_zq157IG&gir=yes&clen=2915100&dur=270.203&lmt=1608157174907785&keepalive=yes&fexp=24001373,24007246&c=WEB&txp=5535432&n=N9BWSTFT7vvBJrvQ&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&alr=yes&sig=AOq0QJ8wRQIgW6XnUDKPDSxiT0_KE_tDDMpcaCJl2Un5p0Fu9qZNQGkCIQDWxsDHi_s2BEmRqIbd1C5g_gzfihB7RZLsScKWNMwzzA%3D%3D&cpn=9r2yt3BqcYmeb2Yu&cver=2.20210716.00.00&redirect_counter=1&cm2rm=sn-4g5ezy7s&cms_redirect=yes&mh=Y5&mm=34&mn=sn-4g5ednek&ms=ltu&mt=1626540524&mv=m&mvi=6&pl=43&lsparams=mh,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRQIhAIUzxTn9Vw1-vm-_7OQ5-0h1M6AZsY9Bx1FlCCTeMICzAiADtGggbn4Znsrh2EnvyOsGnYdRGcbxn4mW9JMOQiInDQ%3D%3D&range=259165-480735&rn=11&rbuf=20190";

for (String videoId : videoIds) {
Comment thread
XiangRongLin marked this conversation as resolved.
Outdated
try {
final String decryptedUrl = new YoutubeThrottlingDecrypter(videoId).apply(encryptedUrl);
assertNotEquals(encryptedUrl, decryptedUrl);
} catch (EvaluatorException e) {
fail("Failed to extract n param decrypt function for video " + videoId + "\n" + e);
}
}
}

@Test
public void testDecode__success() throws ParsingException {
// URL extracted from browser with the dev tools
Expand Down