Skip to content

Commit 124a9e1

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 fe43242 commit 124a9e1

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-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
@@ -165,15 +165,12 @@ public String getDashMpdUrl() throws ParsingException {
165165
@Override
166166
public String getHlsUrl() {
167167
// TODO: There are multiple HLS streams.
168-
// Make getHlsUrl() and getDashMpdUrl() return lists of VideoStreams, so the user can choose a resolution.
168+
// Make getHlsUrl() and getDashMpdUrl() return lists of VideoStreams
169+
// to allow selecting a resolution.
169170
for (int s = 0; s < room.getArray("streams").size(); s++) {
170171
final JsonObject stream = room.getArray("streams").getObject(s);
171-
if (stream.getString("type").equals("video")) {
172-
final String resolution = stream.getArray("videoSize").getInt(0) + "x"
173-
+ stream.getArray("videoSize").getInt(1);
174-
if (stream.has("hls")) {
175-
return stream.getObject("urls").getObject("hls").getString("url");
176-
}
172+
if (stream.getString("type").equals("video") && stream.getObject("urls").has("hls")) {
173+
return stream.getObject("urls").getObject("hls").getString("url");
177174
}
178175
}
179176
return "";
@@ -186,8 +183,11 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
186183
final JsonObject stream = room.getArray("streams").getObject(s);
187184
if (stream.getString("type").equals("audio")) {
188185
for (final String type : stream.getObject("urls").keySet()) {
189-
final JsonObject url = stream.getObject("urls").getObject(type);
190-
audioStreams.add(new AudioStream(url.getString("url"), MediaFormat.getFromSuffix(type), -1));
186+
if (!type.equals("hls")) {
187+
final JsonObject url = stream.getObject("urls").getObject(type);
188+
audioStreams.add(new AudioStream(
189+
url.getString("url"), MediaFormat.getFromSuffix(type), -1));
190+
}
191191
}
192192
}
193193
}

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

0 commit comments

Comments
 (0)