Skip to content

Commit dbcf61c

Browse files
committed
[Bandcamp] Load more featured pages
1 parent b4dee6d commit dbcf61c

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
2525

2626
public static final String KIOSK_FEATURED = "Featured";
2727
public static final String FEATURED_API_URL = BASE_API_URL + "/mobile/24/bootstrap_data";
28+
public static final String MORE_FEATURED_API_URL = BASE_API_URL + "/mobile/24/feed_older_logged_out";
2829

2930
private JsonObject json;
3031

@@ -56,12 +57,18 @@ public String getName() throws ParsingException {
5657
@Override
5758
public InfoItemsPage<PlaylistInfoItem> getInitialPage() throws IOException, ExtractionException {
5859

59-
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
6060

6161
final JsonArray featuredStories = json.getObject("feed_content")
6262
.getObject("stories")
6363
.getArray("featured");
6464

65+
return extractItems(featuredStories);
66+
}
67+
68+
private InfoItemsPage<PlaylistInfoItem> extractItems(JsonArray featuredStories) {
69+
70+
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
71+
6572
for (int i = 0; i < featuredStories.size(); i++) {
6673
final JsonObject featuredStory = featuredStories.getObject(i);
6774

@@ -73,12 +80,37 @@ public InfoItemsPage<PlaylistInfoItem> getInitialPage() throws IOException, Extr
7380
c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory));
7481
}
7582

76-
return new InfoItemsPage<>(c, null);
83+
final JsonObject lastFeaturedStory = featuredStories.getObject(featuredStories.size() - 1);
84+
85+
return new InfoItemsPage<>(c, makeNextPage(lastFeaturedStory));
86+
}
7787

88+
/**
89+
* The query for more featured stories needs parameters from the last featured
90+
* story.
91+
*/
92+
private Page makeNextPage(JsonObject lastFeaturedStory) {
93+
final long lastStoryDate = lastFeaturedStory.getLong("story_date");
94+
final long lastStoryId = lastFeaturedStory.getLong("ntid");
95+
final String lastStoryType = lastFeaturedStory.getString("story_type");
96+
return new Page(
97+
MORE_FEATURED_API_URL + "?story_groups=featured"
98+
+ ':' + lastStoryDate + ':' + lastStoryType + ':' + lastStoryId
99+
);
78100
}
79101

80102
@Override
81-
public InfoItemsPage<PlaylistInfoItem> getPage(Page page) {
82-
return null;
103+
public InfoItemsPage<PlaylistInfoItem> getPage(Page page) throws IOException, ExtractionException {
104+
105+
JsonObject response;
106+
try {
107+
response = JsonParser.object().from(
108+
getDownloader().get(page.getUrl()).responseBody()
109+
);
110+
} catch (final JsonParserException e) {
111+
throw new ParsingException("Could not parse Bandcamp featured API response", e);
112+
}
113+
114+
return extractItems(response.getObject("stories").getArray("featured"));
83115
}
84116
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77
import org.schabi.newpipe.downloader.DownloaderTestImpl;
88
import org.schabi.newpipe.extractor.NewPipe;
9+
import org.schabi.newpipe.extractor.Page;
910
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1011
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
1112
import org.schabi.newpipe.extractor.services.BaseListExtractorTest;
@@ -16,6 +17,7 @@
1617
import java.util.List;
1718

1819
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
1921
import static org.junit.Assert.assertTrue;
2022
import static org.schabi.newpipe.extractor.ServiceList.Bandcamp;
2123

@@ -37,7 +39,7 @@ public static void setUp() throws ExtractionException, IOException {
3739
@Test
3840
public void testFeaturedCount() throws ExtractionException, IOException {
3941
final List<PlaylistInfoItem> list = extractor.getInitialPage().getItems();
40-
assertTrue(list.size() > 1);
42+
assertTrue(list.size() > 5);
4143
}
4244

4345
@Test
@@ -46,6 +48,21 @@ public void testHttps() throws ExtractionException, IOException {
4648
assertTrue(list.get(0).getUrl().contains("https://"));
4749
}
4850

51+
@Test
52+
public void testMorePages() throws ExtractionException, IOException {
53+
54+
final Page page2 = extractor.getInitialPage().getNextPage();
55+
final Page page3 = extractor.getPage(page2).getNextPage();
56+
57+
assertTrue(extractor.getPage(page2).getItems().size() > 5);
58+
59+
// Compare first item of second page with first item of third page
60+
assertNotEquals(
61+
extractor.getPage(page2).getItems().get(0),
62+
extractor.getPage(page3).getItems().get(0)
63+
);
64+
}
65+
4966
@Override
5067
public void testRelatedItems() throws Exception {
5168
DefaultTests.defaultTestRelatedItems(extractor);

0 commit comments

Comments
 (0)