Handle age-restricted videos

This commit is contained in:
TobiGr 2021-02-24 12:06:19 +01:00 committed by TiA4f8R
parent 448b68700c
commit da3cfa967d
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
1 changed files with 14 additions and 3 deletions

View File

@ -718,16 +718,26 @@ public class YoutubeStreamExtractor extends StreamExtractor {
} }
playerResponse = initialAjaxJson.getObject(2).getObject("playerResponse", null); 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")) { if (playerResponse == null || !playerResponse.has("streamingData")) {
// try to get player response by fetching video info page // try to get player response by fetching video info page
fetchVideoInfoPage(); fetchVideoInfoPage();
} }
final JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus"); JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus");
final String status = playabilityStatus.getString("status"); String status = playabilityStatus.getString("status");
// If status exist, and is not "OK", throw the specific exception based on error message // 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. // or a ContentNotAvailableException with the reason text if it's an unknown reason.
if (status != null && !status.toLowerCase().equals("ok")) { if (status != null && !status.toLowerCase().equals("ok")) {
playabilityStatus = youtubePlayerResponse.getObject("playabilityStatus");
status = playabilityStatus.getString("status");
final String reason = playabilityStatus.getString("reason"); final String reason = playabilityStatus.getString("reason");
if (status.toLowerCase().equals("login_required")) { 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.")) { 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."); 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."); throw new AgeRestrictedContentException("This age-restricted video cannot be watched.");
} }
} }