Skip to content

Commit dd61d66

Browse files
committed
speed up finding decrypt function
1 parent 5eac266 commit dd61d66

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -708,15 +708,8 @@ private String loadDecryptionCode(String playerUrl) throws DecryptException {
708708
}
709709

710710
final String playerCode = downloader.download(playerUrl);
711+
final String decryptionFunctionName = getDecryptionFuncName(playerCode);
711712

712-
final String decryptionFunctionName;
713-
if (Parser.isMatch(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode)) {
714-
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode);
715-
} else if (Parser.isMatch(DECRYPTION_AKAMAIZED_STRING_REGEX, playerCode)) {
716-
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_STRING_REGEX, playerCode);
717-
} else {
718-
decryptionFunctionName = Parser.matchGroup1(DECYRYPTION_SIGNATURE_FUNCTION_REGEX, playerCode);
719-
}
720713
final String functionPattern = "("
721714
+ decryptionFunctionName.replace("$", "\\$")
722715
+ "=function\\([a-zA-Z0-9_]+\\)\\{.+?\\})";
@@ -757,6 +750,27 @@ private String decryptSignature(String encryptedSig, String decryptionCode) thro
757750
return result == null ? "" : result.toString();
758751
}
759752

753+
private String getDecryptionFuncName(String playerCode) throws DecryptException {
754+
String decryptionFunctionName;
755+
// Cascading things in catch is ugly, but its faster than running a match before getting the actual name
756+
// to se if the function can actually be found with the given regex.
757+
// However if this cascading should propably be cleaned up somehow as it looks a bit weird.
758+
try {
759+
decryptionFunctionName = Parser.matchGroup1(DECYRYPTION_SIGNATURE_FUNCTION_REGEX, playerCode);
760+
} catch (Parser.RegexException re) {
761+
try {
762+
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode);
763+
} catch (Parser.RegexException re2) {
764+
try {
765+
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode);
766+
} catch (Parser.RegexException re3) {
767+
throw new DecryptException("Could not find decrypt function with any of the given patterns.", re);
768+
}
769+
}
770+
}
771+
return decryptionFunctionName;
772+
}
773+
760774
@Nonnull
761775
private List<SubtitlesInfo> getAvailableSubtitlesInfo() throws SubtitlesException {
762776
// If the video is age restricted getPlayerConfig will fail

0 commit comments

Comments
 (0)