Skip to content

Commit ed73ae5

Browse files
committed
-Added more decrypt function name matching regex.
-Cleaned up decryption code generation method.
1 parent 119843b commit ed73ae5

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ public String getErrorMessage() {
560560

561561
private static final String VERIFIED_URL_PARAMS = "&has_verified=1&bpctr=9999999999";
562562

563+
private final static String DECYRYPTION_SIGNATURE_FUNCTION_REGEX =
564+
"(\\w+)\\s*=\\s*function\\((\\w+)\\)\\{\\s*\\2=\\s*\\2\\.split\\(\"\"\\)\\s*;";
565+
private final static String DECRYPTION_AKAMAIZED_STRING_REGEX =
566+
"yt\\.akamaized\\.net/\\)\\s*\\|\\|\\s*.*?\\s*c\\s*&&\\s*d\\.set\\([^,]+\\s*,\\s*([a-zA-Z0-9$]+)\\(";
567+
private final static String DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX =
568+
"\\bc\\s*&&\\s*d\\.set\\([^,]+\\s*,\\s*([a-zA-Z0-9$]+)\\(";
569+
563570
private volatile String decryptionCode = "";
564571

565572
private String pageHtml = null;
@@ -682,49 +689,45 @@ private EmbeddedInfo getEmbeddedInfo() throws ParsingException, ReCaptchaExcepti
682689
}
683690

684691
private String loadDecryptionCode(String playerUrl) throws DecryptException {
685-
String decryptionFuncName;
686-
String decryptionFunc;
687-
String helperObjectName;
688-
String helperObject;
689-
String callerFunc = "function " + DECRYPTION_FUNC_NAME + "(a){return %%(a);}";
690-
String decryptionCode;
691-
692692
try {
693693
Downloader downloader = NewPipe.getDownloader();
694694
if (!playerUrl.contains("https://youtube.com")) {
695695
//sometimes the https://youtube.com part does not get send with
696696
//than we have to add it by hand
697697
playerUrl = "https://youtube.com" + playerUrl;
698698
}
699-
String playerCode = downloader.download(playerUrl);
700699

701-
decryptionFuncName = Parser.matchGroup(
702-
// Look for a function with the first line containing pattern of: [var]=[var].split("")
703-
"(\\w+)\\s*=\\s*function\\((\\w+)\\)\\{\\s*\\2=\\s*\\2\\.split\\(\"\"\\)\\s*;",
704-
playerCode, 1);
700+
final String playerCode = downloader.download(playerUrl);
705701

706-
String functionPattern = "("
707-
+ decryptionFuncName.replace("$", "\\$")
702+
final String decryptionFunctionName;
703+
if (Parser.isMatch(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode)) {
704+
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode);
705+
} else if (Parser.isMatch(DECRYPTION_AKAMAIZED_STRING_REGEX, playerCode)) {
706+
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_STRING_REGEX, playerCode);
707+
} else {
708+
decryptionFunctionName = Parser.matchGroup1(DECYRYPTION_SIGNATURE_FUNCTION_REGEX, playerCode);
709+
}
710+
final String functionPattern = "("
711+
+ decryptionFunctionName.replace("$", "\\$")
708712
+ "=function\\([a-zA-Z0-9_]+\\)\\{.+?\\})";
709-
decryptionFunc = "var " + Parser.matchGroup1(functionPattern, playerCode) + ";";
710-
711-
helperObjectName = Parser
712-
.matchGroup1(";([A-Za-z0-9_\\$]{2})\\...\\(", decryptionFunc);
713+
final String decryptionFunction = "var " + Parser.matchGroup1(functionPattern, playerCode) + ";";
713714

714-
String helperPattern = "(var "
715-
+ helperObjectName.replace("$", "\\$") + "=\\{.+?\\}\\};)";
716-
helperObject = Parser.matchGroup1(helperPattern, playerCode.replace("\n", ""));
715+
final String helperObjectName =
716+
Parser.matchGroup1(";([A-Za-z0-9_\\$]{2})\\...\\(", decryptionFunction);
717+
final String helperPattern =
718+
"(var " + helperObjectName.replace("$", "\\$") + "=\\{.+?\\}\\};)";
719+
final String helperObject =
720+
Parser.matchGroup1(helperPattern, playerCode.replace("\n", ""));
717721

722+
final String callerFunction =
723+
"function " + DECRYPTION_FUNC_NAME + "(a){return " + decryptionFunctionName + "(a);}";
718724

719-
callerFunc = callerFunc.replace("%%", decryptionFuncName);
720-
decryptionCode = helperObject + decryptionFunc + callerFunc;
725+
return helperObject + decryptionFunction + callerFunction;
721726
} catch (IOException ioe) {
722727
throw new DecryptException("Could not load decrypt function", ioe);
723728
} catch (Exception e) {
724729
throw new DecryptException("Could not parse decrypt function ", e);
725730
}
726-
727-
return decryptionCode;
728731
}
729732

730733
private String decryptSignature(String encryptedSig, String decryptionCode) throws DecryptException {

0 commit comments

Comments
 (0)