From 2c7acc74f55f834aab43da362481dc455869779e Mon Sep 17 00:00:00 2001 From: tonakriz Date: Fri, 24 Nov 2017 13:57:54 +0100 Subject: [PATCH] Fixed exceptions as requested --- .../soundcloud/SoundcloudStreamExtractor.java | 4 ++-- .../services/youtube/YoutubeStreamExtractor.java | 16 +++++++++++----- .../extractor/stream/StreamExtractor.java | 5 ++--- .../SoundcloudStreamExtractorDefaultTest.java | 4 ++-- .../YoutubeStreamExtractorDefaultTest.java | 4 ++-- .../YoutubeStreamExtractorRestrictedTest.java | 4 ++-- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java index b4702c64..90404282 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java @@ -148,12 +148,12 @@ public class SoundcloudStreamExtractor extends StreamExtractor { } @Override - public List getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException { + public List getSubtitlesDefault() throws IOException, ExtractionException { return null; } @Override - public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException { + public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException { return null; } diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index 00ee2433..40e10226 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -380,19 +380,25 @@ public class YoutubeStreamExtractor extends StreamExtractor { } @Override - public List getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException { + public List getSubtitlesDefault() throws IOException, ExtractionException { return getSubtitles(SubtitlesFormat.TTML); } @Override - public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException { + public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException { JsonObject playerConfig = getPlayerConfig(getPageHtml()); String playerResponse = playerConfig.getObject("args").getString("player_response"); - // Captions does not exist, return null - if (!JsonParser.object().from(playerResponse).has("captions")) return null; + JsonObject captions; + try { + // Captions does not exist, return null + if (!JsonParser.object().from(playerResponse).has("captions")) return null; - JsonObject captions = JsonParser.object().from(playerResponse).getObject("captions"); + captions = JsonParser.object().from(playerResponse).getObject("captions"); + } catch (JsonParserException e) { + // Failed to parse subtitles + return null; + } JsonArray captionsArray = captions.getObject("playerCaptionsTracklistRenderer").getArray("captionTracks"); int captionsSize = captionsArray.size(); diff --git a/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java b/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java index a7c38da9..55bb2037 100644 --- a/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java @@ -112,9 +112,8 @@ public abstract class StreamExtractor extends Extractor { public abstract List getAudioStreams() throws IOException, ExtractionException; public abstract List getVideoStreams() throws IOException, ExtractionException; public abstract List getVideoOnlyStreams() throws IOException, ExtractionException; - public abstract List getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException; - - public abstract List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException; + public abstract List getSubtitlesDefault() throws IOException, ExtractionException; + public abstract List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException; public abstract StreamType getStreamType() throws ParsingException; public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException; diff --git a/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java b/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java index 9cf1c24b..fe23298b 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java @@ -107,13 +107,13 @@ public class SoundcloudStreamExtractorDefaultTest { } @Test - public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException { + public void testGetSubtitlesListDefault() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitlesDefault() == null); } @Test - public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException { + public void testGetSubtitlesList() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null); } diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorDefaultTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorDefaultTest.java index fbc4d68d..6e15b499 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorDefaultTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorDefaultTest.java @@ -149,13 +149,13 @@ public class YoutubeStreamExtractorDefaultTest { } @Test - public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException { + public void testGetSubtitlesListDefault() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitlesDefault() == null); } @Test - public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException { + public void testGetSubtitlesList() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null); } diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorRestrictedTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorRestrictedTest.java index b0f1c30f..65a11ebe 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorRestrictedTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorRestrictedTest.java @@ -108,13 +108,13 @@ public class YoutubeStreamExtractorRestrictedTest { @Test - public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException { + public void testGetSubtitlesListDefault() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitlesDefault() == null); } @Test - public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException { + public void testGetSubtitlesList() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null); }