Skip to content

Commit da3cfa9

Browse files
TobiGrAudricV
authored andcommitted
Handle age-restricted videos
1 parent 448b687 commit da3cfa9

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,16 +718,26 @@ public void onFetchPage(@Nonnull final Downloader downloader)
718718
}
719719

720720
playerResponse = initialAjaxJson.getObject(2).getObject("playerResponse", null);
721+
// Save the playerResponse from the youtube.com website,
722+
// because there can be restrictions on the embedded player.
723+
// E.g. if a video is age-restricted, the embedded player's playabilityStatus says,
724+
// that the video cannot be played outside of YouTube,
725+
// but does not show the original message.
726+
final JsonObject youtubePlayerResponse = playerResponse;
727+
721728
if (playerResponse == null || !playerResponse.has("streamingData")) {
722729
// try to get player response by fetching video info page
723730
fetchVideoInfoPage();
724731
}
725732

726-
final JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus");
727-
final String status = playabilityStatus.getString("status");
733+
JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus");
734+
String status = playabilityStatus.getString("status");
728735
// If status exist, and is not "OK", throw the specific exception based on error message
729736
// or a ContentNotAvailableException with the reason text if it's an unknown reason.
730737
if (status != null && !status.toLowerCase().equals("ok")) {
738+
playabilityStatus = youtubePlayerResponse.getObject("playabilityStatus");
739+
status = playabilityStatus.getString("status");
740+
731741
final String reason = playabilityStatus.getString("reason");
732742

733743
if (status.toLowerCase().equals("login_required")) {
@@ -736,7 +746,8 @@ public void onFetchPage(@Nonnull final Downloader downloader)
736746
if (message != null && message.equals("This is a private video. Please sign in to verify that you may see it.")) {
737747
throw new PrivateContentException("This video is private.");
738748
}
739-
} else if (reason.equals("Sign in to confirm your age") && getAgeLimit() == 18) {
749+
} else if (reason.equals("Sign in to confirm your age")) {
750+
// No streams can be fetched, therefore thrown an AgeRestrictedContentException explicitly.
740751
throw new AgeRestrictedContentException("This age-restricted video cannot be watched.");
741752
}
742753
}

0 commit comments

Comments
 (0)