Skip to content

Commit 310b345

Browse files
committed
Changed the way of getting subtitles data type, removed language name from Subtitles class
1 parent 79e49ce commit 310b345

8 files changed

Lines changed: 85 additions & 29 deletions

File tree

src/main/java/org/schabi/newpipe/extractor/Subtitles.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package org.schabi.newpipe.extractor;
22

3+
import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
4+
35
public class Subtitles {
4-
private String languageName;
5-
private String languageCode;
6-
private String URL;
6+
private SubtitlesFormat format;
7+
private String languageCode, URL;
78
private boolean autoGenerated;
89

9-
public Subtitles(String languageName, String languageCode, String URL, boolean autoGenerated) {
10-
this.languageName = languageName;
10+
public Subtitles(SubtitlesFormat format, String languageCode, String URL, boolean autoGenerated) {
11+
this.format = format;
1112
this.languageCode = languageCode;
1213
this.URL = URL;
1314
this.autoGenerated = autoGenerated;
1415
}
1516

16-
public String getLanguageName() {
17-
return languageName;
18-
}
17+
public SubtitlesFormat getFileType() { return format; }
1918

2019
public String getLanguageCode() {
2120
return languageCode;

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import org.schabi.newpipe.extractor.utils.Parser;
1313

1414
import java.io.IOException;
15-
import java.util.ArrayList;
16-
import java.util.HashMap;
17-
import java.util.List;
15+
import java.util.*;
1816

1917
public class SoundcloudStreamExtractor extends StreamExtractor {
2018
private JsonObject track;
@@ -150,7 +148,12 @@ public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionExc
150148
}
151149

152150
@Override
153-
public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException {
151+
public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException {
152+
return null;
153+
}
154+
155+
@Override
156+
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException {
154157
return null;
155158
}
156159

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,12 @@ public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionExc
380380
}
381381

382382
@Override
383-
public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException {
383+
public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException {
384+
return getSubtitles(SubtitlesFormat.TTML);
385+
}
386+
387+
@Override
388+
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException {
384389
JsonObject playerConfig = getPlayerConfig(getPageHtml());
385390
String playerResponse = playerConfig.getObject("args").getString("player_response");
386391

@@ -394,19 +399,18 @@ public Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonP
394399
// Should not happen, if there is the "captions" object, it should always has some captions in it
395400
if(captionsSize == 0) return null;
396401

397-
Subtitles[] result = new Subtitles[captionsSize];
402+
List<Subtitles> result = new ArrayList<>();
398403
for (int x = 0; x < captionsSize; x++) {
399404
String baseUrl = captionsArray.getObject(x).getString("baseUrl");
400405

401-
String languageName = captionsArray.getObject(x).getObject("name").getString("simpleText");
402-
String URL = baseUrl.replaceAll("&fmt=[^&]*", "&fmt=vtt");
406+
String extension = format.getExtension();
403407

408+
String URL = baseUrl.replaceAll("&fmt=[^&]*", "&fmt=" + extension);
404409
String captionsLangCode = captionsArray.getObject(x).getString("vssId");
405-
String languageCode = captionsLangCode.replaceAll("(a\\.)|(\\.)", "");
406-
407410
boolean isAutoGenerated = captionsLangCode.startsWith("a.");
411+
String languageCode = captionsLangCode.replaceFirst((isAutoGenerated) ? "a." : ".", "");
408412

409-
result[x] = new Subtitles(languageName, languageCode, URL, isAutoGenerated);
413+
result.add(new Subtitles(format, languageCode, URL, isAutoGenerated));
410414
}
411415

412416
return result;

src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ protected long getTimestampSeconds(String regexPattern) throws ParsingException
112112
public abstract List<AudioStream> getAudioStreams() throws IOException, ExtractionException;
113113
public abstract List<VideoStream> getVideoStreams() throws IOException, ExtractionException;
114114
public abstract List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException;
115-
public abstract Subtitles[] getSubtitles() throws IOException, ExtractionException, JsonParserException;
115+
public abstract List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException, JsonParserException;
116+
117+
public abstract List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException, JsonParserException;
116118

117119
public abstract StreamType getStreamType() throws ParsingException;
118120
public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.schabi.newpipe.extractor.stream;
2+
3+
import org.schabi.newpipe.extractor.Subtitles;
4+
5+
public enum SubtitlesFormat {
6+
// YouTube subtitles formats
7+
// TRANSCRIPT(3) is default YT format based on TTML,
8+
// but unlike VTT or TTML, it is NOT W3 standard
9+
VTT (0x0, "vtt"),
10+
TTML (0x1, "ttml"),
11+
TRANSCRIPT1 (0x2, "srv1"),
12+
TRANSCRIPT2 (0x3, "srv2"),
13+
TRANSCRIPT3 (0x4, "srv3");
14+
15+
private int id;
16+
private String extension;
17+
18+
SubtitlesFormat(int id, String extension) {
19+
this.id = id;
20+
this.extension = extension;
21+
}
22+
23+
public String getExtension() {
24+
return extension;
25+
}
26+
}

src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import org.junit.Test;
66
import org.schabi.newpipe.Downloader;
77
import org.schabi.newpipe.extractor.NewPipe;
8+
import org.schabi.newpipe.extractor.Subtitles;
89
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
910
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1011
import org.schabi.newpipe.extractor.stream.StreamExtractor;
1112
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
1213
import org.schabi.newpipe.extractor.stream.StreamType;
14+
import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
1315

1416
import java.io.IOException;
17+
import java.util.List;
1518

1619
import static org.junit.Assert.*;
1720
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
@@ -104,7 +107,14 @@ public void testGetRelatedVideos() throws ExtractionException, IOException {
104107
}
105108

106109
@Test
107-
public void testGetSubtitles() throws IOException, ExtractionException, JsonParserException {
108-
assertTrue(extractor.getSubtitles() == null);
110+
public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException {
111+
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
112+
assertTrue(extractor.getSubtitlesDefault() == null);
113+
}
114+
115+
@Test
116+
public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException {
117+
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
118+
assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null);
109119
}
110120
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import org.schabi.newpipe.extractor.NewPipe;
88
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
99
import org.schabi.newpipe.extractor.exceptions.ParsingException;
10-
import org.schabi.newpipe.extractor.stream.StreamExtractor;
11-
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
12-
import org.schabi.newpipe.extractor.stream.StreamType;
13-
import org.schabi.newpipe.extractor.stream.VideoStream;
10+
import org.schabi.newpipe.extractor.stream.*;
1411

1512
import java.io.IOException;
1613
import java.util.HashMap;
@@ -151,9 +148,15 @@ public void testGetRelatedVideos() throws ExtractionException, IOException {
151148
assertTrue(relatedVideos.getErrors().isEmpty());
152149
}
153150

151+
@Test
152+
public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException {
153+
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
154+
assertTrue(extractor.getSubtitlesDefault() == null);
155+
}
156+
154157
@Test
155158
public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException {
156159
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
157-
assertTrue(extractor.getSubtitles() == null);
160+
assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null);
158161
}
159162
}

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorRestrictedTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
99
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1010
import org.schabi.newpipe.extractor.stream.StreamExtractor;
11+
import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
1112
import org.schabi.newpipe.extractor.stream.VideoStream;
1213

1314
import java.io.IOException;
@@ -105,8 +106,16 @@ public void testGetVideoStreams() throws IOException, ExtractionException {
105106
}
106107
}
107108

109+
110+
@Test
111+
public void testGetSubtitlesListDefault() throws IOException, ExtractionException, JsonParserException {
112+
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
113+
assertTrue(extractor.getSubtitlesDefault() == null);
114+
}
115+
108116
@Test
109-
public void testGetSubtitles() throws IOException, ExtractionException, JsonParserException {
110-
assertTrue(extractor.getSubtitles() == null);
117+
public void testGetSubtitlesList() throws IOException, ExtractionException, JsonParserException {
118+
// Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null
119+
assertTrue(extractor.getSubtitles(SubtitlesFormat.VTT) == null);
111120
}
112121
}

0 commit comments

Comments
 (0)