Skip to content

Commit dbdd9ed

Browse files
authored
Merge pull request #195 from Stypox/livestream-fix
Fix livestreams
2 parents acf2b4c + 1a16722 commit dbdd9ed

5 files changed

Lines changed: 147 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public StreamType getStreamType() throws ParsingException {
523523
assertPageFetched();
524524
try {
525525
if (playerArgs != null && (playerArgs.has("ps") && playerArgs.get("ps").toString().equals("live") ||
526-
playerResponse.getObject("streamingData").getArray(FORMATS).isEmpty())) {
526+
(!playerResponse.getObject("streamingData").has(FORMATS)))) {
527527
return StreamType.LIVE_STREAM;
528528
}
529529
} catch (Exception e) {
@@ -915,8 +915,12 @@ private static String getVideoInfoUrl(final String id, final String sts) {
915915

916916
private Map<String, ItagItem> getItags(String streamingDataKey, ItagItem.ItagType itagTypeWanted) throws ParsingException {
917917
Map<String, ItagItem> urlAndItags = new LinkedHashMap<>();
918+
JsonObject streamingData = playerResponse.getObject("streamingData");
919+
if (!streamingData.has(streamingDataKey)) {
920+
return urlAndItags;
921+
}
918922

919-
JsonArray formats = playerResponse.getObject("streamingData").getArray(streamingDataKey);
923+
JsonArray formats = streamingData.getArray(streamingDataKey);
920924
for (int i = 0; i != formats.size(); ++i) {
921925
JsonObject formatData = formats.getObject(i);
922926
int itag = formatData.getInt("itag");

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorAgeRestrictedTest.java renamed to extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.schabi.newpipe.extractor.services.youtube;
1+
package org.schabi.newpipe.extractor.services.youtube.stream;
22

33
import org.junit.BeforeClass;
44
import org.junit.Ignore;

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractorControversialTest.java renamed to extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.schabi.newpipe.extractor.services.youtube;
1+
package org.schabi.newpipe.extractor.services.youtube.stream;
22

33
import org.junit.BeforeClass;
44
import org.junit.Ignore;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.schabi.newpipe.extractor.services.youtube;
1+
package org.schabi.newpipe.extractor.services.youtube.stream;
22

33
import org.junit.BeforeClass;
44
import org.junit.Test;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.schabi.newpipe.extractor.services.youtube.stream;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.Test;
5+
import org.schabi.newpipe.Downloader;
6+
import org.schabi.newpipe.extractor.MediaFormat;
7+
import org.schabi.newpipe.extractor.NewPipe;
8+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
9+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
10+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
11+
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
12+
import org.schabi.newpipe.extractor.stream.StreamType;
13+
import org.schabi.newpipe.extractor.stream.VideoStream;
14+
import org.schabi.newpipe.extractor.utils.Localization;
15+
import org.schabi.newpipe.extractor.utils.Utils;
16+
17+
import java.io.IOException;
18+
19+
import static org.junit.Assert.*;
20+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
21+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
22+
23+
public class YoutubeStreamExtractorLivestreamTest {
24+
private static YoutubeStreamExtractor extractor;
25+
26+
@BeforeClass
27+
public static void setUp() throws Exception {
28+
NewPipe.init(Downloader.getInstance(), new Localization("GB", "en"));
29+
extractor = (YoutubeStreamExtractor) YouTube
30+
.getStreamExtractor("https://www.youtube.com/watch?v=EcEMX-63PKY");
31+
extractor.fetchPage();
32+
}
33+
34+
@Test
35+
public void testGetInvalidTimeStamp() throws ParsingException {
36+
assertTrue(extractor.getTimeStamp() + "",
37+
extractor.getTimeStamp() <= 0);
38+
}
39+
40+
@Test
41+
public void testGetTitle() throws ParsingException {
42+
assertFalse(extractor.getName().isEmpty());
43+
}
44+
45+
@Test
46+
public void testGetDescription() throws ParsingException {
47+
assertNotNull(extractor.getDescription());
48+
assertFalse(extractor.getDescription().isEmpty());
49+
}
50+
51+
@Test
52+
public void testGetFullLinksInDescription() throws ParsingException {
53+
assertTrue(extractor.getDescription().contains("https://www.instagram.com/nathalie.baraton/"));
54+
assertFalse(extractor.getDescription().contains("https://www.instagram.com/nathalie.ba..."));
55+
}
56+
57+
@Test
58+
public void testGetUploaderName() throws ParsingException {
59+
assertNotNull(extractor.getUploaderName());
60+
assertFalse(extractor.getUploaderName().isEmpty());
61+
}
62+
63+
64+
@Test
65+
public void testGetLength() throws ParsingException {
66+
assertEquals(0, extractor.getLength());
67+
}
68+
69+
@Test
70+
public void testGetViewCount() throws ParsingException {
71+
long count = extractor.getViewCount();
72+
assertTrue(Long.toString(count), count >= 7148995);
73+
}
74+
75+
@Test
76+
public void testGetUploadDate() throws ParsingException {
77+
assertTrue(extractor.getUploadDate().length() > 0);
78+
}
79+
80+
@Test
81+
public void testGetUploaderUrl() throws ParsingException {
82+
assertEquals("https://www.youtube.com/channel/UCSJ4gkVC6NrvII8umztf0Ow", extractor.getUploaderUrl());
83+
}
84+
85+
@Test
86+
public void testGetThumbnailUrl() throws ParsingException {
87+
assertIsSecureUrl(extractor.getThumbnailUrl());
88+
}
89+
90+
@Test
91+
public void testGetUploaderAvatarUrl() throws ParsingException {
92+
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
93+
}
94+
95+
@Test
96+
public void testGetAudioStreams() throws ExtractionException {
97+
assertFalse(extractor.getAudioStreams().isEmpty());
98+
}
99+
100+
@Test
101+
public void testGetVideoStreams() throws ExtractionException {
102+
for (VideoStream s : extractor.getVideoStreams()) {
103+
assertIsSecureUrl(s.url);
104+
assertTrue(s.resolution.length() > 0);
105+
assertTrue(Integer.toString(s.getFormatId()),
106+
0 <= s.getFormatId() && s.getFormatId() <= 0x100);
107+
}
108+
}
109+
110+
@Test
111+
public void testStreamType() throws ParsingException {
112+
assertSame(extractor.getStreamType(), StreamType.LIVE_STREAM);
113+
}
114+
115+
@Test
116+
public void testGetDashMpd() throws ParsingException {
117+
// we dont expect this particular video to have a DASH file. For this purpouse we use a different test class.
118+
assertTrue(extractor.getDashMpdUrl(), extractor.getDashMpdUrl().isEmpty());
119+
}
120+
121+
@Test
122+
public void testGetRelatedVideos() throws ExtractionException, IOException {
123+
StreamInfoItemsCollector relatedVideos = extractor.getRelatedStreams();
124+
Utils.printErrors(relatedVideos.getErrors());
125+
assertFalse(relatedVideos.getItems().isEmpty());
126+
assertTrue(relatedVideos.getErrors().isEmpty());
127+
}
128+
129+
@Test
130+
public void testGetSubtitlesListDefault() throws IOException, ExtractionException {
131+
assertTrue(extractor.getSubtitlesDefault().isEmpty());
132+
}
133+
134+
@Test
135+
public void testGetSubtitlesList() throws IOException, ExtractionException {
136+
assertTrue(extractor.getSubtitles(MediaFormat.TTML).isEmpty());
137+
}
138+
}

0 commit comments

Comments
 (0)