From da3cfa967d5f6e49c4c3e047ba95428ea84c7da8 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Wed, 24 Feb 2021 12:06:19 +0100 Subject: [PATCH] Handle age-restricted videos --- .../extractors/YoutubeStreamExtractor.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java index ffe0b04fc..47739985e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java @@ -718,16 +718,26 @@ public class YoutubeStreamExtractor extends StreamExtractor { } playerResponse = initialAjaxJson.getObject(2).getObject("playerResponse", null); + // Save the playerResponse from the youtube.com website, + // because there can be restrictions on the embedded player. + // E.g. if a video is age-restricted, the embedded player's playabilityStatus says, + // that the video cannot be played outside of YouTube, + // but does not show the original message. + final JsonObject youtubePlayerResponse = playerResponse; + if (playerResponse == null || !playerResponse.has("streamingData")) { // try to get player response by fetching video info page fetchVideoInfoPage(); } - final JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus"); - final String status = playabilityStatus.getString("status"); + JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus"); + String status = playabilityStatus.getString("status"); // If status exist, and is not "OK", throw the specific exception based on error message // or a ContentNotAvailableException with the reason text if it's an unknown reason. if (status != null && !status.toLowerCase().equals("ok")) { + playabilityStatus = youtubePlayerResponse.getObject("playabilityStatus"); + status = playabilityStatus.getString("status"); + final String reason = playabilityStatus.getString("reason"); if (status.toLowerCase().equals("login_required")) { @@ -736,7 +746,8 @@ public class YoutubeStreamExtractor extends StreamExtractor { if (message != null && message.equals("This is a private video. Please sign in to verify that you may see it.")) { throw new PrivateContentException("This video is private."); } - } else if (reason.equals("Sign in to confirm your age") && getAgeLimit() == 18) { + } else if (reason.equals("Sign in to confirm your age")) { + // No streams can be fetched, therefore thrown an AgeRestrictedContentException explicitly. throw new AgeRestrictedContentException("This age-restricted video cannot be watched."); } }