Skip to content

Commit 9edd0f2

Browse files
authored
Merge pull request #288 from Stypox/fix-unlisted-video
[YouTube] Fix videos without next stream throwing error and add test for unlisted videos
2 parents aea0962 + 957b78e commit 9edd0f2

2 files changed

Lines changed: 178 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,18 @@ public StreamInfoItem getNextStream() throws ExtractionException {
523523
if (getAgeLimit() != NO_AGE_LIMIT) return null;
524524

525525
try {
526-
final JsonObject videoInfo = initialData.getObject("contents").getObject("twoColumnWatchNextResults")
527-
.getObject("secondaryResults").getObject("secondaryResults").getArray("results")
528-
.getObject(0).getObject("compactAutoplayRenderer").getArray("contents")
529-
.getObject(0).getObject("compactVideoRenderer");
526+
final JsonObject firstWatchNextItem = initialData.getObject("contents")
527+
.getObject("twoColumnWatchNextResults").getObject("secondaryResults")
528+
.getObject("secondaryResults").getArray("results").getObject(0);
529+
530+
if (!firstWatchNextItem.has("compactAutoplayRenderer")) {
531+
// there is no "next" stream
532+
return null;
533+
}
534+
535+
final JsonObject videoInfo = firstWatchNextItem.getObject("compactAutoplayRenderer")
536+
.getArray("contents").getObject(0).getObject("compactVideoRenderer");
537+
530538
final TimeAgoParser timeAgoParser = getTimeAgoParser();
531539
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
532540

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package org.schabi.newpipe.extractor.services.youtube.stream;
2+
3+
import org.junit.Assert;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import org.schabi.newpipe.DownloaderTestImpl;
7+
import org.schabi.newpipe.extractor.MediaFormat;
8+
import org.schabi.newpipe.extractor.NewPipe;
9+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
10+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
11+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
12+
import org.schabi.newpipe.extractor.stream.AudioStream;
13+
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
14+
import org.schabi.newpipe.extractor.stream.StreamType;
15+
import org.schabi.newpipe.extractor.stream.VideoStream;
16+
import org.schabi.newpipe.extractor.utils.Utils;
17+
18+
import java.text.ParseException;
19+
import java.text.SimpleDateFormat;
20+
import java.util.Calendar;
21+
import java.util.List;
22+
23+
import static org.junit.Assert.*;
24+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
25+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
26+
27+
public class YoutubeStreamExtractorUnlistedTest {
28+
private static YoutubeStreamExtractor extractor;
29+
30+
@BeforeClass
31+
public static void setUp() throws Exception {
32+
NewPipe.init(DownloaderTestImpl.getInstance());
33+
extractor = (YoutubeStreamExtractor) YouTube
34+
.getStreamExtractor("https://www.youtube.com/watch?v=udsB8KnIJTg");
35+
extractor.fetchPage();
36+
}
37+
38+
@Test
39+
public void testGetInvalidTimeStamp() throws ParsingException {
40+
assertTrue(extractor.getTimeStamp() + "",
41+
extractor.getTimeStamp() <= 0);
42+
}
43+
44+
@Test
45+
public void testGetTitle() throws ParsingException {
46+
assertFalse(extractor.getName().isEmpty());
47+
}
48+
49+
@Test
50+
public void testGetDescription() throws ParsingException {
51+
assertNotNull(extractor.getDescription());
52+
assertFalse(extractor.getDescription().getContent().isEmpty());
53+
}
54+
55+
@Test
56+
public void testGetFullLinksInDescription() throws ParsingException {
57+
assertTrue(extractor.getDescription().getContent().contains("https://www.youtube.com/user/Roccowschiptune"));
58+
}
59+
60+
@Test
61+
public void testGetUploaderName() throws ParsingException {
62+
assertNotNull(extractor.getUploaderName());
63+
assertFalse(extractor.getUploaderName().isEmpty());
64+
}
65+
66+
67+
@Test
68+
public void testGetLength() throws ParsingException {
69+
assertEquals(2488, extractor.getLength());
70+
}
71+
72+
@Test
73+
public void testGetViewCount() throws ParsingException {
74+
long count = extractor.getViewCount();
75+
assertTrue(Long.toString(count), count >= /* specific to that video */ 1225);
76+
}
77+
78+
@Test
79+
public void testGetTextualUploadDate() throws ParsingException {
80+
Assert.assertEquals("2017-09-22", extractor.getTextualUploadDate());
81+
}
82+
83+
@Test
84+
public void testGetUploadDate() throws ParsingException, ParseException {
85+
final Calendar instance = Calendar.getInstance();
86+
instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2017-09-22"));
87+
assertNotNull(extractor.getUploadDate());
88+
assertEquals(instance, extractor.getUploadDate().date());
89+
}
90+
91+
@Test
92+
public void testGetUploaderUrl() throws ParsingException {
93+
assertEquals("https://www.youtube.com/channel/UCPysfiuOv4VKBeXFFPhKXyw", extractor.getUploaderUrl());
94+
}
95+
96+
@Test
97+
public void testGetThumbnailUrl() throws ParsingException {
98+
assertIsSecureUrl(extractor.getThumbnailUrl());
99+
}
100+
101+
@Test
102+
public void testGetUploaderAvatarUrl() throws ParsingException {
103+
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
104+
}
105+
106+
@Test
107+
public void testGetAudioStreams() throws ExtractionException {
108+
List<AudioStream> audioStreams = extractor.getAudioStreams();
109+
assertFalse(audioStreams.isEmpty());
110+
for (AudioStream s : audioStreams) {
111+
assertIsSecureUrl(s.url);
112+
assertTrue(Integer.toString(s.getFormatId()),
113+
0x100 <= s.getFormatId() && s.getFormatId() < 0x1000);
114+
}
115+
}
116+
117+
@Test
118+
public void testGetVideoStreams() throws ExtractionException {
119+
for (VideoStream s : extractor.getVideoStreams()) {
120+
assertIsSecureUrl(s.url);
121+
assertTrue(s.resolution.length() > 0);
122+
assertTrue(Integer.toString(s.getFormatId()),
123+
0 <= s.getFormatId() && s.getFormatId() < 0x100);
124+
}
125+
}
126+
127+
@Test
128+
public void testStreamType() throws ParsingException {
129+
assertSame(StreamType.VIDEO_STREAM, extractor.getStreamType());
130+
}
131+
132+
@Test
133+
public void testGetNextVideo() throws ExtractionException {
134+
assertNull(extractor.getNextStream());
135+
}
136+
137+
@Test
138+
public void testGetRelatedVideos() throws ExtractionException {
139+
StreamInfoItemsCollector relatedVideos = extractor.getRelatedStreams();
140+
Utils.printErrors(relatedVideos.getErrors());
141+
assertFalse(relatedVideos.getItems().isEmpty());
142+
assertTrue(relatedVideos.getErrors().isEmpty());
143+
}
144+
145+
@Test
146+
public void testGetSubtitlesListDefault() {
147+
assertFalse(extractor.getSubtitlesDefault().isEmpty());
148+
}
149+
150+
@Test
151+
public void testGetSubtitlesList() {
152+
assertFalse(extractor.getSubtitles(MediaFormat.TTML).isEmpty());
153+
}
154+
155+
@Test
156+
public void testGetLikeCount() throws ParsingException {
157+
long likeCount = extractor.getLikeCount();
158+
assertTrue("" + likeCount, likeCount >= 96);
159+
}
160+
161+
@Test
162+
public void testGetDislikeCount() throws ParsingException {
163+
long dislikeCount = extractor.getDislikeCount();
164+
assertTrue("" + dislikeCount, dislikeCount >= 0);
165+
}
166+
}

0 commit comments

Comments
 (0)