Skip to content

Commit 96b4c97

Browse files
authored
Merge pull request #109 from karyogamy/sig-fix-update
Decryption Signature Regex Update
2 parents 217d13b + ed73ae5 commit 96b4c97

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
@@ -561,6 +561,13 @@ public String getErrorMessage() {
561561

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

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

566573
private String pageHtml = null;
@@ -683,49 +690,45 @@ private EmbeddedInfo getEmbeddedInfo() throws ParsingException, ReCaptchaExcepti
683690
}
684691

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)