Changed the way of getting subtitles data type, removed language name from Subtitles class

This commit is contained in:
tonakriz 2017-11-23 16:33:03 +01:00
parent 79e49ce374
commit 310b34558b
8 changed files with 85 additions and 29 deletions

View File

@ -1,21 +1,20 @@
package org.schabi.newpipe.extractor; package org.schabi.newpipe.extractor;
import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
public class Subtitles { public class Subtitles {
private String languageName; private SubtitlesFormat format;
private String languageCode; private String languageCode, URL;
private String URL;
private boolean autoGenerated; private boolean autoGenerated;
public Subtitles(String languageName, String languageCode, String URL, boolean autoGenerated) { public Subtitles(SubtitlesFormat format, String languageCode, String URL, boolean autoGenerated) {
this.languageName = languageName; this.format = format;
this.languageCode = languageCode; this.languageCode = languageCode;
this.URL = URL; this.URL = URL;
this.autoGenerated = autoGenerated; this.autoGenerated = autoGenerated;
} }
public String getLanguageName() { public SubtitlesFormat getFileType() { return format; }
return languageName;
}
public String getLanguageCode() { public String getLanguageCode() {
return languageCode; return languageCode;

View File

@ -12,9 +12,7 @@ import org.schabi.newpipe.extractor.stream.*;
import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
public class SoundcloudStreamExtractor extends StreamExtractor { public class SoundcloudStreamExtractor extends StreamExtractor {
private JsonObject track; private JsonObject track;
@ -150,7 +148,12 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
} }
@Override @Override
public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException { public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException {
return null;
}
@Override
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException {
return null; return null;
} }

View File

@ -380,7 +380,12 @@ public class YoutubeStreamExtractor extends StreamExtractor {
} }
@Override @Override
public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException { public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException {
return getSubtitles(SubtitlesFormat.TTML);
}
@Override
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException {
JsonObject playerConfig = getPlayerConfig(getPageHtml()); JsonObject playerConfig = getPlayerConfig(getPageHtml());
String playerResponse = playerConfig.getObject("args").getString("player_response"); 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 // Should not happen, if there is the "captions" object, it should always has some captions in it
if(captionsSize == 0) return null; if(captionsSize == 0) return null;
Subtitles[] result = new Subtitles[captionsSize]; List<Subtitles> result = new ArrayList<>();
for (int x = 0; x < captionsSize; x++) { for (int x = 0; x < captionsSize; x++) {
String baseUrl = captionsArray.getObject(x).getString("baseUrl"); String baseUrl = captionsArray.getObject(x).getString("baseUrl");
String languageName = captionsArray.getObject(x).getObject("name").getString("simpleText"); String extension = format.getExtension();
String URL = baseUrl.replaceAll("&fmt=[^&]*", "&fmt=vtt");
String URL = baseUrl.replaceAll("&fmt=[^&]*", "&fmt=" + extension);
String captionsLangCode = captionsArray.getObject(x).getString("vssId"); String captionsLangCode = captionsArray.getObject(x).getString("vssId");
String languageCode = captionsLangCode.replaceAll("(a\\.)|(\\.)", "");
boolean isAutoGenerated = captionsLangCode.startsWith("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; return result;

View File

@ -112,7 +112,9 @@ public abstract class StreamExtractor extends Extractor {
public abstract List<AudioStream> getAudioStreams() throws IOException, ExtractionException; public abstract List<AudioStream> getAudioStreams() throws IOException, ExtractionException;
public abstract List<VideoStream> getVideoStreams() throws IOException, ExtractionException; public abstract List<VideoStream> getVideoStreams() throws IOException, ExtractionException;
public abstract List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException; public abstract List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException;
public abstract Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException; public abstract List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException;
public abstract List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException;
public abstract StreamType getStreamType() throws ParsingException; public abstract StreamType getStreamType() throws ParsingException;
public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException; public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException;

View File

@ -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;
}
}

View File

@ -5,13 +5,16 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.schabi.newpipe.Downloader; import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.NewPipe; 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.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
@ -104,7 +107,14 @@ public class SoundcloudStreamExtractorDefaultTest {
} }
@Test @Test
public void testGetSubtitles() throws IOException, ExtractionException, JsonParserException { public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException {
assertTrue(extractor.getSubtitles() == null); // 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);
} }
} }

View File

@ -7,10 +7,7 @@ import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.*;
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.stream.VideoStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
@ -151,9 +148,15 @@ public class YoutubeStreamExtractorDefaultTest {
assertTrue(relatedVideos.getErrors().isEmpty()); 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 @Test
public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException { public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException {
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
assertTrue(extractor.getSubtitles() == null); assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null);
} }
} }

View File

@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.stream.VideoStream;
import java.io.IOException; import java.io.IOException;
@ -105,8 +106,16 @@ public class YoutubeStreamExtractorRestrictedTest {
} }
} }
@Test @Test
public void testGetSubtitles() throws IOException, ExtractionException, JsonParserException { public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException {
assertTrue(extractor.getSubtitles() == null); // 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);
} }
} }