Prevent NullPointerException and use equalsIgnorecase

This commit is contained in:
TobiGr 2021-02-24 14:13:12 +01:00 committed by TiA4f8R
parent 890cbba625
commit d0a1041afd
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
1 changed files with 12 additions and 5 deletions

View File

@ -723,24 +723,31 @@ public class YoutubeStreamExtractor extends StreamExtractor {
// E.g. if a video is age-restricted, the embedded player's playabilityStatus says, // E.g. if a video is age-restricted, the embedded player's playabilityStatus says,
// that the video cannot be played outside of YouTube, // that the video cannot be played outside of YouTube,
// but does not show the original message. // but does not show the original message.
final JsonObject youtubePlayerResponse = playerResponse; 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();
} }
JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus"); if (playerResponse == null && youtubePlayerResponse == null) {
throw new ExtractionException("Could not get playerResponse");
} else if (youtubePlayerResponse == null) {
youtubePlayerResponse = playerResponse;
}
JsonObject playabilityStatus = (playerResponse == null ? youtubePlayerResponse : playerResponse)
.getObject("playabilityStatus");
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.equalsIgnoreCase("ok")) {
playabilityStatus = youtubePlayerResponse.getObject("playabilityStatus"); playabilityStatus = youtubePlayerResponse.getObject("playabilityStatus");
status = playabilityStatus.getString("status"); status = playabilityStatus.getString("status");
final String reason = playabilityStatus.getString("reason"); final String reason = playabilityStatus.getString("reason");
if (status.toLowerCase().equals("login_required")) { if (status.equalsIgnoreCase("login_required")) {
if (reason == null) { if (reason == null) {
final String message = playabilityStatus.getArray("messages").getString(0); final String message = playabilityStatus.getArray("messages").getString(0);
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.")) {
@ -751,7 +758,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
throw new AgeRestrictedContentException("This age-restricted video cannot be watched."); throw new AgeRestrictedContentException("This age-restricted video cannot be watched.");
} }
} }
if (status.toLowerCase().equals("unplayable")) { if (status.equalsIgnoreCase("unplayable")) {
if (reason != null) { if (reason != null) {
if (reason.equals("This video is only available to Music Premium members")) { if (reason.equals("This video is only available to Music Premium members")) {
throw new YoutubeMusicPremiumContentException(); throw new YoutubeMusicPremiumContentException();