Skip to content

Commit 318f600

Browse files
committed
add subtitles support for peertube
1 parent bb5ad49 commit 318f600

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
4040

4141

4242
private JsonObject json;
43+
private List<SubtitlesStream> subtitles = new ArrayList<>();
4344

4445
public PeertubeStreamExtractor(StreamingService service, LinkHandler linkHandler, Localization localization) {
4546
super(service, linkHandler, localization);
@@ -173,12 +174,18 @@ public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionExc
173174

174175
@Override
175176
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
176-
return Collections.emptyList();
177+
return subtitles;
177178
}
178179

179180
@Override
180181
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
181-
return Collections.emptyList();
182+
List<SubtitlesStream> filteredSubs = new ArrayList<>();
183+
for(SubtitlesStream sub: subtitles) {
184+
if(sub.getFormat() == format) {
185+
filteredSubs.add(sub);
186+
}
187+
}
188+
return filteredSubs;
182189
}
183190

184191
@Override
@@ -274,6 +281,8 @@ public void onFetchPage(Downloader downloader) throws IOException, ExtractionExc
274281
}else {
275282
throw new ExtractionException("Unable to extract peertube channel data");
276283
}
284+
285+
loadSubtitles();
277286
}
278287

279288
private void setInitialData(String responseBody) throws ExtractionException {
@@ -285,6 +294,28 @@ private void setInitialData(String responseBody) throws ExtractionException {
285294
if(null == json) throw new ExtractionException("Unable to extract peertube stream data");
286295
PeertubeParsingHelper.validate(json);
287296
}
297+
298+
private void loadSubtitles() {
299+
if (subtitles.isEmpty()) {
300+
try {
301+
DownloadResponse response = getDownloader().get(getUrl() + "/captions");
302+
JsonObject captionsJson = JsonParser.object().from(response.getResponseBody());
303+
JsonArray captions = JsonUtils.getArray(captionsJson, "data");
304+
for(Object c: captions) {
305+
if(c instanceof JsonObject) {
306+
JsonObject caption = (JsonObject)c;
307+
String url = ServiceList.PeerTube.getBaseUrl() + JsonUtils.getString(caption, "captionPath");
308+
String languageCode = JsonUtils.getString(caption, "language.id");
309+
String ext = url.substring(url.lastIndexOf(".") + 1);
310+
MediaFormat fmt = MediaFormat.getFromSuffix(ext);
311+
if(fmt != null && languageCode != null) subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
312+
}
313+
}
314+
} catch (Exception e) {
315+
// ignore all exceptions
316+
}
317+
}
318+
}
288319

289320
@Override
290321
public String getName() throws ParsingException {

0 commit comments

Comments
 (0)