Skip to content

Commit 752825c

Browse files
committed
[media.ccc.de] Fix live streams extraction
The API was slightly changed and only HLS streams are provided anymore. Add a basic test for to check whether the required streamInfo fields are available. Closes #766
1 parent 10f6cc7 commit 752825c

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCLiveStreamExtractor.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,12 @@ public String getUploaderName() throws ParsingException {
9393
@Override
9494
public String getHlsUrl() {
9595
// TODO: There are multiple HLS streams.
96-
// Make getHlsUrl() and getDashMpdUrl() return lists of VideoStreams, so the user can choose a resolution.
96+
// Make getHlsUrl() and getDashMpdUrl() return lists of VideoStreams
97+
// to allow selecting a resolution.
9798
for (int s = 0; s < room.getArray("streams").size(); s++) {
9899
final JsonObject stream = room.getArray("streams").getObject(s);
99-
if (stream.getString("type").equals("video")) {
100-
final String resolution = stream.getArray("videoSize").getInt(0) + "x"
101-
+ stream.getArray("videoSize").getInt(1);
102-
if (stream.has("hls")) {
103-
return stream.getObject("urls").getObject("hls").getString("url");
104-
}
100+
if (stream.getString("type").equals("video") && stream.getObject("urls").has("hls")) {
101+
return stream.getObject("urls").getObject("hls").getString("url");
105102
}
106103
}
107104
return "";
@@ -114,8 +111,11 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
114111
final JsonObject stream = room.getArray("streams").getObject(s);
115112
if (stream.getString("type").equals("audio")) {
116113
for (final String type : stream.getObject("urls").keySet()) {
117-
final JsonObject url = stream.getObject("urls").getObject(type);
118-
audioStreams.add(new AudioStream(url.getString("url"), MediaFormat.getFromSuffix(type), -1));
114+
if (!type.equals("hls")) {
115+
final JsonObject url = stream.getObject("urls").getObject(type);
116+
audioStreams.add(new AudioStream(
117+
url.getString("url"), MediaFormat.getFromSuffix(type), -1));
118+
}
119119
}
120120
}
121121
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ private static StreamInfo extractStreams(StreamInfo streamInfo, StreamExtractor
139139
} catch (Exception e) {
140140
streamInfo.addError(new ExtractionException("Couldn't get audio streams", e));
141141
}
142-
/* Extract video stream url */
142+
/* Extract video streams */
143143
try {
144144
streamInfo.setVideoStreams(extractor.getVideoStreams());
145145
} catch (Exception e) {
146146
streamInfo.addError(new ExtractionException("Couldn't get video streams", e));
147147
}
148-
/* Extract video only stream url */
148+
/* Extract video only streams */
149149
try {
150150
streamInfo.setVideoOnlyStreams(extractor.getVideoOnlyStreams());
151151
} catch (Exception e) {
@@ -181,13 +181,16 @@ private static StreamInfo extractStreams(StreamInfo streamInfo, StreamExtractor
181181

182182
// Either audio or video has to be available, otherwise we didn't get a stream
183183
// (since videoOnly are optional, they don't count).
184-
if ((streamInfo.videoStreams.isEmpty()) && (streamInfo.audioStreams.isEmpty())) {
184+
if (streamInfo.videoStreams.isEmpty()
185+
&& streamInfo.audioStreams.isEmpty()
186+
&& isNullOrEmpty(streamInfo.getHlsUrl())
187+
&& isNullOrEmpty(streamInfo.getDashMpdUrl())) {
185188

186189
if (dashMpdError != null) {
187190
// If we don't have any video or audio and the dashMpd 'errored', add it to the
188191
// error list
189-
// (it's optional and it don't get added automatically, but it's good to have
190-
// some additional error context)
192+
// (It's optional, and it is not added automatically,
193+
// but it's good to have some additional error context).
191194
streamInfo.addError(dashMpdError);
192195
}
193196

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.schabi.newpipe.extractor.services.media_ccc;
2+
3+
import org.junit.Assert;
4+
import org.junit.Assume;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
import org.schabi.newpipe.downloader.DownloaderTestImpl;
8+
import org.schabi.newpipe.extractor.InfoItem;
9+
import org.schabi.newpipe.extractor.NewPipe;
10+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
11+
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
12+
import org.schabi.newpipe.extractor.stream.StreamExtractor;
13+
import org.schabi.newpipe.extractor.stream.StreamInfo;
14+
15+
import java.io.IOException;
16+
import java.util.List;
17+
18+
import static org.schabi.newpipe.extractor.ServiceList.MediaCCC;
19+
20+
public class MediaCCCLiveStreamExtractorTest {
21+
22+
private static KioskExtractor liveKiosk;
23+
private static StreamExtractor extractor;
24+
25+
private static List<InfoItem> liveItems;
26+
27+
@BeforeClass
28+
public static void setUp() throws Exception {
29+
NewPipe.init(DownloaderTestImpl.getInstance());
30+
liveKiosk = MediaCCC.getKioskList().getExtractorById("live", null);
31+
liveKiosk.fetchPage();
32+
liveItems = liveKiosk.getInitialPage().getItems();
33+
Assume.assumeFalse(
34+
"Received an empty list of live streams. Skipping MediaCCCLiveStreamExtractorTest",
35+
liveItems.isEmpty());
36+
}
37+
38+
@Test
39+
public void testRequiredStreamInfo() {
40+
// Try to get the StreamInfo for each live stream.
41+
// If some required info is not present an exception will be thrown.
42+
try {
43+
for (final InfoItem item : liveItems) {
44+
StreamInfo.getInfo(item.getUrl());
45+
}
46+
} catch (ExtractionException | IOException e) {
47+
e.printStackTrace();
48+
Assert.fail("An exception was thrown while getting a StreamInfo for a livestream.");
49+
}
50+
}
51+
52+
}

0 commit comments

Comments
 (0)