From 310b34558bbe5b7290c2149479dacd2bde576374 Mon Sep 17 00:00:00 2001 From: tonakriz Date: Thu, 23 Nov 2017 16:33:03 +0100 Subject: [PATCH] Changed the way of getting subtitles data type, removed language name from Subtitles class --- .../schabi/newpipe/extractor/Subtitles.java | 15 +++++------ .../soundcloud/SoundcloudStreamExtractor.java | 11 +++++--- .../youtube/YoutubeStreamExtractor.java | 18 ++++++++----- .../extractor/stream/StreamExtractor.java | 4 ++- .../extractor/stream/SubtitlesFormat.java | 26 +++++++++++++++++++ .../SoundcloudStreamExtractorDefaultTest.java | 14 ++++++++-- .../YoutubeStreamExtractorDefaultTest.java | 13 ++++++---- .../YoutubeStreamExtractorRestrictedTest.java | 13 ++++++++-- 8 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesFormat.java diff --git a/src/main/java/org/schabi/newpipe/extractor/Subtitles.java b/src/main/java/org/schabi/newpipe/extractor/Subtitles.java index 12e1dcbc3..0542b385e 100644 --- a/src/main/java/org/schabi/newpipe/extractor/Subtitles.java +++ b/src/main/java/org/schabi/newpipe/extractor/Subtitles.java @@ -1,21 +1,20 @@ package org.schabi.newpipe.extractor; +import org.schabi.newpipe.extractor.stream.SubtitlesFormat; + public class Subtitles { - private String languageName; - private String languageCode; - private String URL; + private SubtitlesFormat format; + private String languageCode, URL; private boolean autoGenerated; - public Subtitles(String languageName, String languageCode, String URL, boolean autoGenerated) { - this.languageName = languageName; + public Subtitles(SubtitlesFormat format, String languageCode, String URL, boolean autoGenerated) { + this.format = format; this.languageCode = languageCode; this.URL = URL; this.autoGenerated = autoGenerated; } - public String getLanguageName() { - return languageName; - } + public SubtitlesFormat getFileType() { return format; } public String getLanguageCode() { return languageCode; 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 caff60625..b4702c64d 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 @@ -12,9 +12,7 @@ import org.schabi.newpipe.extractor.stream.*; import org.schabi.newpipe.extractor.utils.Parser; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.util.*; public class SoundcloudStreamExtractor extends StreamExtractor { private JsonObject track; @@ -150,7 +148,12 @@ public class SoundcloudStreamExtractor extends StreamExtractor { } @Override - public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException { + public List getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException { + return null; + } + + @Override + public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException { 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 e246a50e0..00ee2433d 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,7 +380,12 @@ public class YoutubeStreamExtractor extends StreamExtractor { } @Override - public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException { + public List getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException { + return getSubtitles(SubtitlesFormat.TTML); + } + + @Override + public List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException { JsonObject playerConfig = getPlayerConfig(getPageHtml()); String playerResponse = playerConfig.getObject("args").getString("player_response"); @@ -394,19 +399,18 @@ public class YoutubeStreamExtractor extends StreamExtractor { // Should not happen, if there is the "captions" object, it should always has some captions in it if(captionsSize == 0) return null; - Subtitles[] result = new Subtitles[captionsSize]; + List result = new ArrayList<>(); for (int x = 0; x < captionsSize; x++) { String baseUrl = captionsArray.getObject(x).getString("baseUrl"); - String languageName = captionsArray.getObject(x).getObject("name").getString("simpleText"); - String URL = baseUrl.replaceAll("&fmt=[^&]*", "&fmt=vtt"); + String extension = format.getExtension(); + String URL = baseUrl.replaceAll("&fmt=[^&]*", "&fmt=" + extension); String captionsLangCode = captionsArray.getObject(x).getString("vssId"); - String languageCode = captionsLangCode.replaceAll("(a\\.)|(\\.)", ""); - boolean isAutoGenerated = captionsLangCode.startsWith("a."); + String languageCode = captionsLangCode.replaceFirst((isAutoGenerated) ? "a." : ".", ""); - result[x] = new Subtitles(languageName, languageCode, URL, isAutoGenerated); + result.add(new Subtitles(format, languageCode, URL, isAutoGenerated)); } return result; 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 bf048d72d..a7c38da93 100644 --- a/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java @@ -112,7 +112,9 @@ 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 Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException; + public abstract List getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException; + + public abstract List getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException; public abstract StreamType getStreamType() throws ParsingException; public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException; diff --git a/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesFormat.java b/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesFormat.java new file mode 100644 index 000000000..d0a0482b4 --- /dev/null +++ b/src/main/java/org/schabi/newpipe/extractor/stream/SubtitlesFormat.java @@ -0,0 +1,26 @@ +package org.schabi.newpipe.extractor.stream; + +import org.schabi.newpipe.extractor.Subtitles; + +public enum SubtitlesFormat { + // YouTube subtitles formats + // TRANSCRIPT(3) is default YT format based on TTML, + // but unlike VTT or TTML, it is NOT W3 standard + VTT (0x0, "vtt"), + TTML (0x1, "ttml"), + TRANSCRIPT1 (0x2, "srv1"), + TRANSCRIPT2 (0x3, "srv2"), + TRANSCRIPT3 (0x4, "srv3"); + + private int id; + private String extension; + + SubtitlesFormat(int id, String extension) { + this.id = id; + this.extension = extension; + } + + public String getExtension() { + return extension; + } +} 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 fad575e78..9cf1c24ba 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 @@ -5,13 +5,16 @@ import org.junit.Before; import org.junit.Test; import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.Subtitles; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; import org.schabi.newpipe.extractor.stream.StreamType; +import org.schabi.newpipe.extractor.stream.SubtitlesFormat; import java.io.IOException; +import java.util.List; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; @@ -104,7 +107,14 @@ public class SoundcloudStreamExtractorDefaultTest { } @Test - public void testGetSubtitles() throws IOException, ExtractionException, JsonParserException { - assertTrue(extractor.getSubtitles() == null); + public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException { + // 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 { + // 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 e6459f372..fbc4d68dd 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 @@ -7,10 +7,7 @@ import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; -import org.schabi.newpipe.extractor.stream.StreamExtractor; -import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; -import org.schabi.newpipe.extractor.stream.StreamType; -import org.schabi.newpipe.extractor.stream.VideoStream; +import org.schabi.newpipe.extractor.stream.*; import java.io.IOException; import java.util.HashMap; @@ -151,9 +148,15 @@ public class YoutubeStreamExtractorDefaultTest { assertTrue(relatedVideos.getErrors().isEmpty()); } + @Test + public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException { + // 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 { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null - assertTrue(extractor.getSubtitles() == 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 587bb9972..b0f1c30f4 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 @@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.stream.StreamExtractor; +import org.schabi.newpipe.extractor.stream.SubtitlesFormat; import org.schabi.newpipe.extractor.stream.VideoStream; import java.io.IOException; @@ -105,8 +106,16 @@ public class YoutubeStreamExtractorRestrictedTest { } } + @Test - public void testGetSubtitles() throws IOException, ExtractionException, JsonParserException { - assertTrue(extractor.getSubtitles() == null); + public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException { + // 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 { + // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null + assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null); } }