Skip to content

Commit 5686a6f

Browse files
mauriciocolliTobiGr
authored andcommitted
[YouTube] Detect when a stream is deleted or doesn't exist
Added a test case as well.
1 parent e65333c commit 5686a6f

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.schabi.newpipe.extractor.NewPipe;
1212
import org.schabi.newpipe.extractor.StreamingService;
1313
import org.schabi.newpipe.extractor.downloader.Downloader;
14+
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
1415
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1516
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1617
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
@@ -32,6 +33,7 @@
3233
import org.schabi.newpipe.extractor.stream.StreamType;
3334
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
3435
import org.schabi.newpipe.extractor.stream.VideoStream;
36+
import org.schabi.newpipe.extractor.utils.JsonUtils;
3537
import org.schabi.newpipe.extractor.utils.Parser;
3638
import org.schabi.newpipe.extractor.utils.Utils;
3739

@@ -619,6 +621,13 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
619621

620622
playerResponse = getPlayerResponse();
621623

624+
final JsonObject playabilityStatus = playerResponse.getObject("playabilityStatus", JsonUtils.DEFAULT_EMPTY);
625+
final String status = playabilityStatus.getString("status");
626+
if (status != null && status.toLowerCase().equals("error")) {
627+
final String reason = playabilityStatus.getString("reason");
628+
throw new ContentNotAvailableException("Got error: \"" + reason + "\"");
629+
}
630+
622631
if (decryptionCode.isEmpty()) {
623632
decryptionCode = loadDecryptionCode(playerUrl);
624633
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212

1313
public class JsonUtils {
14+
public static final JsonObject DEFAULT_EMPTY = new JsonObject();
1415

1516
private JsonUtils() {
1617
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.schabi.newpipe.extractor.ExtractorAsserts;
88
import org.schabi.newpipe.extractor.MediaFormat;
99
import org.schabi.newpipe.extractor.NewPipe;
10+
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
1011
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1112
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1213
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
@@ -56,6 +57,27 @@
5657
*/
5758
public class YoutubeStreamExtractorDefaultTest {
5859

60+
public static class NotAvailable {
61+
@BeforeClass
62+
public static void setUp() {
63+
NewPipe.init(DownloaderTestImpl.getInstance());
64+
}
65+
66+
@Test(expected = ContentNotAvailableException.class)
67+
public void nonExistentFetch() throws Exception {
68+
final StreamExtractor extractor =
69+
YouTube.getStreamExtractor("https://www.youtube.com/watch?v=don-t-exist");
70+
extractor.fetchPage();
71+
}
72+
73+
@Test(expected = ParsingException.class)
74+
public void invalidId() throws Exception {
75+
final StreamExtractor extractor =
76+
YouTube.getStreamExtractor("https://www.youtube.com/watch?v=INVALID_ID_INVALID_ID");
77+
extractor.fetchPage();
78+
}
79+
}
80+
5981
/**
6082
* Test for {@link StreamExtractor}
6183
*/

0 commit comments

Comments
 (0)