Skip to content

Commit ff61e28

Browse files
authored
Merge pull request #243 from TeamNewPipe/fix/yt_decryption
Fix/yt decryption
2 parents c1e1ac1 + 7b72fd2 commit ff61e28

3 files changed

Lines changed: 24 additions & 22 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void fetchPage() throws IOException, ExtractionException {
5656
}
5757

5858
protected void assertPageFetched() {
59-
if(!pageFetched) throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()");
59+
if (!pageFetched) throw new IllegalStateException("Page is not fetched. Make sure you call fetchPage()");
6060
}
6161

6262
protected boolean isPageFetched() {

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,10 @@ public String getErrorMessage() {
677677

678678
private static final String VERIFIED_URL_PARAMS = "&has_verified=1&bpctr=9999999999";
679679

680-
private final static String DECYRYPTION_SIGNATURE_FUNCTION_REGEX =
680+
private final static String DECRYPTION_SIGNATURE_FUNCTION_REGEX =
681681
"([\\w$]+)\\s*=\\s*function\\((\\w+)\\)\\{\\s*\\2=\\s*\\2\\.split\\(\"\"\\)\\s*;";
682+
private final static String DECRYPTION_SIGNATURE_FUNCTION_REGEX_2 =
683+
"\\b([\\w$]{2})\\s*=\\s*function\\((\\w+)\\)\\{\\s*\\2=\\s*\\2\\.split\\(\"\"\\)\\s*;";
682684
private final static String DECRYPTION_AKAMAIZED_STRING_REGEX =
683685
"yt\\.akamaized\\.net/\\)\\s*\\|\\|\\s*.*?\\s*c\\s*&&\\s*d\\.set\\([^,]+\\s*,\\s*(:encodeURIComponent\\s*\\()([a-zA-Z0-9$]+)\\(";
684686
private final static String DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX =
@@ -697,7 +699,8 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
697699

698700
final String playerUrl;
699701
// Check if the video is age restricted
700-
if (!doc.select("meta[property=\"og:restrictions:age\"]").isEmpty()) {
702+
Elements e = doc.select("meta[property=\"og:restrictions:age\"]");
703+
if (!e.isEmpty()) {
701704
final EmbeddedInfo info = getEmbeddedInfo();
702705
final String videoInfoUrl = getVideoInfoUrl(getId(), info.sts);
703706
final String infoPageResponse = downloader.get(videoInfoUrl, getExtractorLocalization()).responseBody();
@@ -773,7 +776,7 @@ private String getPlayerUrl(JsonObject playerConfig) throws ParsingException {
773776
private JsonObject getPlayerResponse() throws ParsingException {
774777
try {
775778
String playerResponseStr;
776-
if(playerArgs != null) {
779+
if (playerArgs != null) {
777780
playerResponseStr = playerArgs.getString("player_response");
778781
} else {
779782
playerResponseStr = videoInfoPage.get("player_response");
@@ -805,7 +808,7 @@ private EmbeddedInfo getEmbeddedInfo() throws ParsingException, ReCaptchaExcepti
805808
final String sts = Parser.matchGroup1(stsPattern, embedPageContent);
806809
return new EmbeddedInfo(playerUrl, sts);
807810
} catch (Exception i) {
808-
// if it failes we simply reply with no sts as then it does not seem to be necessary
811+
// if it fails we simply reply with no sts as then it does not seem to be necessary
809812
return new EmbeddedInfo(playerUrl, "");
810813
}
811814

@@ -868,30 +871,28 @@ private String decryptSignature(String encryptedSig, String decryptionCode) thro
868871
}
869872

870873
private String getDecryptionFuncName(String playerCode) throws DecryptException {
871-
String decryptionFunctionName;
872-
// Cascading things in catch is ugly, but its faster than running a match before getting the actual name
873-
// to se if the function can actually be found with the given regex.
874-
// However if this cascading should propably be cleaned up somehow as it looks a bit weird.
875-
try {
876-
decryptionFunctionName = Parser.matchGroup1(DECYRYPTION_SIGNATURE_FUNCTION_REGEX, playerCode);
877-
} catch (Parser.RegexException re) {
874+
String[] decryptionFuncNameRegexes = {
875+
DECRYPTION_SIGNATURE_FUNCTION_REGEX_2,
876+
DECRYPTION_SIGNATURE_FUNCTION_REGEX,
877+
DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX,
878+
DECRYPTION_AKAMAIZED_STRING_REGEX
879+
};
880+
Parser.RegexException exception = null;
881+
for (String regex : decryptionFuncNameRegexes) {
878882
try {
879-
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_SHORT_STRING_REGEX, playerCode);
880-
} catch (Parser.RegexException re2) {
881-
try {
882-
decryptionFunctionName = Parser.matchGroup1(DECRYPTION_AKAMAIZED_STRING_REGEX, playerCode);
883-
} catch (Parser.RegexException re3) {
884-
throw new DecryptException("Could not find decrypt function with any of the given patterns.", re);
885-
}
883+
return Parser.matchGroup1(regex, playerCode);
884+
} catch (Parser.RegexException re) {
885+
if (exception == null)
886+
exception = re;
886887
}
887888
}
888-
return decryptionFunctionName;
889+
throw new DecryptException("Could not find decrypt function with any of the given patterns.", exception);
889890
}
890891

891892
@Nonnull
892893
private List<SubtitlesInfo> getAvailableSubtitlesInfo() throws SubtitlesException {
893894
// If the video is age restricted getPlayerConfig will fail
894-
if(isAgeRestricted) return Collections.emptyList();
895+
if (isAgeRestricted) return Collections.emptyList();
895896

896897
final JsonObject captions;
897898
if (!playerResponse.has("captions")) {
@@ -908,7 +909,7 @@ private List<SubtitlesInfo> getAvailableSubtitlesInfo() throws SubtitlesExceptio
908909
// This check is necessary since there may be cases where subtitles metadata do not contain caption track info
909910
// e.g. https://www.youtube.com/watch?v=-Vpwatutnko
910911
final int captionsSize = captionsArray.size();
911-
if(captionsSize == 0) return Collections.emptyList();
912+
if (captionsSize == 0) return Collections.emptyList();
912913

913914
List<SubtitlesInfo> result = new ArrayList<>();
914915
for (int i = 0; i < captionsSize; i++) {

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static String matchGroup(Pattern pat, String input, int group) throws Reg
6767
if (foundMatch) {
6868
return mat.group(group);
6969
} else {
70+
// only pass input to exception message when it is not too long
7071
if (input.length() > 1024) {
7172
throw new RegexException("failed to find pattern \"" + pat.pattern());
7273
} else {

0 commit comments

Comments
 (0)