Skip to content

Commit 5bceff0

Browse files
committed
[YouTube] Fix extraction of next page url for the last page of playlist
1 parent 8347e14 commit 5bceff0

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,17 @@ private Page getNextPageFrom(final JsonArray contents) {
225225
return null;
226226
}
227227

228-
final String continuation = contents.getObject(contents.size() - 1)
229-
.getObject("continuationItemRenderer")
230-
.getObject("continuationEndpoint")
231-
.getObject("continuationCommand")
232-
.getString("token");
233-
return new Page("https://www.youtube.com/browse_ajax?continuation=" + continuation);
228+
final JsonObject lastElement = contents.getObject(contents.size() - 1);
229+
if (lastElement.has("continuationItemRenderer")) {
230+
final String continuation = lastElement
231+
.getObject("continuationItemRenderer")
232+
.getObject("continuationEndpoint")
233+
.getObject("continuationCommand")
234+
.getString("token");
235+
return new Page("https://www.youtube.com/browse_ajax?continuation=" + continuation);
236+
} else {
237+
return null;
238+
}
234239
}
235240

236241
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonArray videos) {

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubePlaylistExtractorTest.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1515
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
1616
import org.schabi.newpipe.extractor.services.BasePlaylistExtractorTest;
17+
import org.schabi.newpipe.extractor.services.youtube.YoutubePlaylistExtractorTest.ContinuationsTests;
1718
import org.schabi.newpipe.extractor.services.youtube.YoutubePlaylistExtractorTest.HugePlaylist;
1819
import org.schabi.newpipe.extractor.services.youtube.YoutubePlaylistExtractorTest.LearningPlaylist;
1920
import org.schabi.newpipe.extractor.services.youtube.YoutubePlaylistExtractorTest.NotAvailable;
2021
import org.schabi.newpipe.extractor.services.youtube.YoutubePlaylistExtractorTest.TimelessPopHits;
2122
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubePlaylistExtractor;
2223
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
2324

25+
import static junit.framework.TestCase.assertFalse;
2426
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNull;
2528
import static org.junit.Assert.assertTrue;
2629
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
2730
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
@@ -31,7 +34,8 @@
3134
* Test for {@link YoutubePlaylistExtractor}
3235
*/
3336
@RunWith(Suite.class)
34-
@SuiteClasses({NotAvailable.class, TimelessPopHits.class, HugePlaylist.class, LearningPlaylist.class})
37+
@SuiteClasses({NotAvailable.class, TimelessPopHits.class, HugePlaylist.class,
38+
LearningPlaylist.class, ContinuationsTests.class})
3539
public class YoutubePlaylistExtractorTest {
3640

3741
public static class NotAvailable {
@@ -123,7 +127,7 @@ public void testThumbnailUrl() throws Exception {
123127

124128
@Ignore
125129
@Test
126-
public void testBannerUrl() throws Exception {
130+
public void testBannerUrl() {
127131
final String bannerUrl = extractor.getBannerUrl();
128132
assertIsSecureUrl(bannerUrl);
129133
assertTrue(bannerUrl, bannerUrl.contains("yt"));
@@ -236,7 +240,7 @@ public void testThumbnailUrl() throws Exception {
236240

237241
@Ignore
238242
@Test
239-
public void testBannerUrl() throws Exception {
243+
public void testBannerUrl() {
240244
final String bannerUrl = extractor.getBannerUrl();
241245
assertIsSecureUrl(bannerUrl);
242246
assertTrue(bannerUrl, bannerUrl.contains("yt"));
@@ -333,7 +337,7 @@ public void testThumbnailUrl() throws Exception {
333337

334338
@Ignore
335339
@Test
336-
public void testBannerUrl() throws Exception {
340+
public void testBannerUrl() {
337341
final String bannerUrl = extractor.getBannerUrl();
338342
assertIsSecureUrl(bannerUrl);
339343
assertTrue(bannerUrl, bannerUrl.contains("yt"));
@@ -361,4 +365,34 @@ public void testStreamCount() throws Exception {
361365
assertTrue("Error in the streams count", extractor.getStreamCount() > 40);
362366
}
363367
}
368+
369+
public static class ContinuationsTests {
370+
371+
@BeforeClass
372+
public static void setUp() {
373+
NewPipe.init(DownloaderTestImpl.getInstance());
374+
}
375+
376+
@Test
377+
public void testNoContinuations() throws Exception {
378+
final YoutubePlaylistExtractor extractor = (YoutubePlaylistExtractor) YouTube
379+
.getPlaylistExtractor(
380+
"https://www.youtube.com/playlist?list=PLXJg25X-OulsVsnvZ7RVtSDW-id9_RzAO");
381+
extractor.fetchPage();
382+
383+
assertNoMoreItems(extractor);
384+
}
385+
386+
@Test
387+
public void testOnlySingleContinuation() throws Exception {
388+
final YoutubePlaylistExtractor extractor = (YoutubePlaylistExtractor) YouTube
389+
.getPlaylistExtractor(
390+
"https://www.youtube.com/playlist?list=PLjgwFL8urN2DFRuRkFTkmtHjyoNWHHdZX");
391+
extractor.fetchPage();
392+
393+
final ListExtractor.InfoItemsPage<StreamInfoItem> page = defaultTestMoreItems(
394+
extractor);
395+
assertFalse("More items available when it shouldn't", page.hasNextPage());
396+
}
397+
}
364398
}

0 commit comments

Comments
 (0)