Skip to content

Commit 23067f4

Browse files
committed
[PeerTube] Readd support of livestreams
1 parent 12e8e6d commit 23067f4

1 file changed

Lines changed: 70 additions & 36 deletions

File tree

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

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -201,32 +201,38 @@ public String getDashMpdUrl() {
201201
@Override
202202
public String getHlsUrl() {
203203
assertPageFetched();
204-
return json.getObject("files").getString("playlistUrl", EMPTY_STRING);
204+
if (getStreamType() == StreamType.VIDEO_STREAM) {
205+
return json.getObject("files").getString("playlistUrl", EMPTY_STRING);
206+
} else {
207+
return json.getArray("streamingPlaylists").getObject(0).getString("playlistUrl");
208+
}
205209
}
206210

207211
@Override
208212
public List<AudioStream> getAudioStreams() throws ParsingException {
209213
assertPageFetched();
214+
210215
final List<AudioStream> audioStreams = new ArrayList<>();
211-
// Non-HLS streams
212-
try {
213-
audioStreams.addAll(getAudioStreamsFromArray(json.getArray("files"), ""));
214-
} catch (final Exception ignored) {
215-
}
216-
// HLS streams
217-
try {
218-
final JsonArray streamingPlaylists = json.getArray("streamingPlaylists");
219-
for (final Object p : streamingPlaylists) {
220-
if (!(p instanceof JsonObject)) continue;
221-
final JsonObject playlist = (JsonObject) p;
222-
final String playlistUrl = playlist.getString("playlistUrl");
223-
audioStreams.addAll(getAudioStreamsFromArray(playlist.getArray("files"),
224-
playlistUrl));
216+
if (getStreamType() == StreamType.VIDEO_STREAM) {
217+
// Non-HLS streams
218+
try {
219+
audioStreams.addAll(getAudioStreamsFromArray(json.getArray("files"), ""));
220+
} catch (final Exception ignored) {
221+
}
222+
// HLS streams
223+
try {
224+
final JsonArray streamingPlaylists = json.getArray("streamingPlaylists");
225+
for (final Object p : streamingPlaylists) {
226+
if (!(p instanceof JsonObject)) continue;
227+
final JsonObject playlist = (JsonObject) p;
228+
final String playlistUrl = playlist.getString("playlistUrl");
229+
audioStreams.addAll(getAudioStreamsFromArray(playlist.getArray("files"),
230+
playlistUrl));
231+
}
232+
} catch (final Exception e) {
233+
throw new ParsingException("Could not get video streams", e);
225234
}
226-
} catch (final Exception e) {
227-
throw new ParsingException("Could not get video streams", e);
228235
}
229-
230236
return audioStreams;
231237
}
232238

@@ -237,7 +243,9 @@ private List<AudioStream> getAudioStreamsFromArray(final JsonArray streams,
237243
try {
238244
final List<AudioStream> audioStreams = new ArrayList<>();
239245
for (final Object s : streams) {
240-
if (!(s instanceof JsonObject)) continue;
246+
if (!(s instanceof JsonObject)){
247+
continue;
248+
}
241249
final JsonObject stream = (JsonObject) s;
242250
final String url;
243251
final String idSuffix;
@@ -292,24 +300,50 @@ private List<AudioStream> getAudioStreamsFromArray(final JsonArray streams,
292300
@Override
293301
public List<VideoStream> getVideoStreams() throws ExtractionException {
294302
assertPageFetched();
303+
295304
final List<VideoStream> videoStreams = new ArrayList<>();
296-
// Non-HLS streams
297-
try {
298-
videoStreams.addAll(getVideoStreamsFromArray(json.getArray("files"), ""));
299-
} catch (final Exception ignored) {
300-
}
301-
// HLS streams
302-
try {
303-
final JsonArray streamingPlaylists = json.getArray("streamingPlaylists");
304-
for (final Object p : streamingPlaylists) {
305-
if (!(p instanceof JsonObject)) continue;
306-
final JsonObject playlist = (JsonObject) p;
307-
final String playlistUrl = playlist.getString("playlistUrl");
308-
videoStreams.addAll(getVideoStreamsFromArray(playlist.getArray("files"),
309-
playlistUrl));
305+
if (getStreamType() == StreamType.VIDEO_STREAM) {
306+
// Non-HLS streams
307+
try {
308+
videoStreams.addAll(getVideoStreamsFromArray(json.getArray("files"), ""));
309+
} catch (final Exception ignored) {
310+
}
311+
// HLS streams
312+
try {
313+
final JsonArray streamingPlaylists = json.getArray("streamingPlaylists");
314+
for (final Object p : streamingPlaylists) {
315+
if (!(p instanceof JsonObject)) {
316+
continue;
317+
}
318+
final JsonObject playlist = (JsonObject) p;
319+
final String playlistUrl = playlist.getString("playlistUrl");
320+
videoStreams.addAll(getVideoStreamsFromArray(playlist.getArray("files"),
321+
playlistUrl));
322+
}
323+
} catch (final Exception e) {
324+
throw new ParsingException("Could not get video streams", e);
325+
}
326+
} else {
327+
try {
328+
final JsonArray streamingPlaylists = json.getArray("streamingPlaylists");
329+
for (final Object p : streamingPlaylists) {
330+
if (!(p instanceof JsonObject)) {
331+
continue;
332+
}
333+
final JsonObject playlist = (JsonObject) p;
334+
videoStreams.add(new VideoStream(
335+
String.valueOf(playlist.getInt("id", 0)),
336+
playlist.getString("playlistUrl", EMPTY_STRING),
337+
true,
338+
MediaFormat.MPEG_4,
339+
DeliveryMethod.HLS,
340+
"",
341+
false,
342+
null));
343+
}
344+
} catch (final Exception e) {
345+
throw new ParsingException("Could not get video streams", e);
310346
}
311-
} catch (final Exception e) {
312-
throw new ParsingException("Could not get video streams", e);
313347
}
314348

315349
return videoStreams;
@@ -405,7 +439,7 @@ public List<SubtitlesStream> getSubtitles(final MediaFormat format) {
405439

406440
@Override
407441
public StreamType getStreamType() {
408-
return StreamType.VIDEO_STREAM;
442+
return json.getBoolean("isLive") ? StreamType.LIVE_STREAM : StreamType.VIDEO_STREAM;
409443
}
410444

411445
@Nullable

0 commit comments

Comments
 (0)