[Bandcamp] Load more featured pages

This commit is contained in:
Fynn Godau 2021-03-28 12:53:05 +02:00
parent b4dee6d08f
commit dbcf61c6f7
2 changed files with 54 additions and 5 deletions

View File

@ -25,6 +25,7 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
public static final String KIOSK_FEATURED = "Featured";
public static final String FEATURED_API_URL = BASE_API_URL + "/mobile/24/bootstrap_data";
public static final String MORE_FEATURED_API_URL = BASE_API_URL + "/mobile/24/feed_older_logged_out";
private JsonObject json;
@ -56,12 +57,18 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
@Override
public InfoItemsPage<PlaylistInfoItem> getInitialPage() throws IOException, ExtractionException {
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
final JsonArray featuredStories = json.getObject("feed_content")
.getObject("stories")
.getArray("featured");
return extractItems(featuredStories);
}
private InfoItemsPage<PlaylistInfoItem> extractItems(JsonArray featuredStories) {
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
for (int i = 0; i < featuredStories.size(); i++) {
final JsonObject featuredStory = featuredStories.getObject(i);
@ -73,12 +80,37 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory));
}
return new InfoItemsPage<>(c, null);
final JsonObject lastFeaturedStory = featuredStories.getObject(featuredStories.size() - 1);
return new InfoItemsPage<>(c, makeNextPage(lastFeaturedStory));
}
/**
* The query for more featured stories needs parameters from the last featured
* story.
*/
private Page makeNextPage(JsonObject lastFeaturedStory) {
final long lastStoryDate = lastFeaturedStory.getLong("story_date");
final long lastStoryId = lastFeaturedStory.getLong("ntid");
final String lastStoryType = lastFeaturedStory.getString("story_type");
return new Page(
MORE_FEATURED_API_URL + "?story_groups=featured"
+ ':' + lastStoryDate + ':' + lastStoryType + ':' + lastStoryId
);
}
@Override
public InfoItemsPage<PlaylistInfoItem> getPage(Page page) {
return null;
public InfoItemsPage<PlaylistInfoItem> getPage(Page page) throws IOException, ExtractionException {
JsonObject response;
try {
response = JsonParser.object().from(
getDownloader().get(page.getUrl()).responseBody()
);
} catch (final JsonParserException e) {
throw new ParsingException("Could not parse Bandcamp featured API response", e);
}
return extractItems(response.getObject("stories").getArray("featured"));
}
}

View File

@ -6,6 +6,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
import org.schabi.newpipe.extractor.services.BaseListExtractorTest;
@ -16,6 +17,7 @@ import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.Bandcamp;
@ -37,7 +39,7 @@ public class BandcampFeaturedExtractorTest implements BaseListExtractorTest {
@Test
public void testFeaturedCount() throws ExtractionException, IOException {
final List<PlaylistInfoItem> list = extractor.getInitialPage().getItems();
assertTrue(list.size() > 1);
assertTrue(list.size() > 5);
}
@Test
@ -46,6 +48,21 @@ public class BandcampFeaturedExtractorTest implements BaseListExtractorTest {
assertTrue(list.get(0).getUrl().contains("https://"));
}
@Test
public void testMorePages() throws ExtractionException, IOException {
final Page page2 = extractor.getInitialPage().getNextPage();
final Page page3 = extractor.getPage(page2).getNextPage();
assertTrue(extractor.getPage(page2).getItems().size() > 5);
// Compare first item of second page with first item of third page
assertNotEquals(
extractor.getPage(page2).getItems().get(0),
extractor.getPage(page3).getItems().get(0)
);
}
@Override
public void testRelatedItems() throws Exception {
DefaultTests.defaultTestRelatedItems(extractor);