Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load more featured pages and fix featured cover arts not loading #589

Merged
merged 3 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,12 +57,18 @@ public String getName() throws ParsingException {
@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);

Expand All @@ -73,12 +80,36 @@ public InfoItemsPage<PlaylistInfoItem> getInitialPage() throws IOException, Extr
c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory));
}

return new InfoItemsPage<>(c, null);
final JsonObject lastFeaturedStory = featuredStories.getObject(featuredStories.size() - 1);

return new InfoItemsPage<>(c, getNextPageFrom(lastFeaturedStory));
}

/**
* Next Page can be generated from metadata of last featured story
*/
private Page getNextPageFrom(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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public String getUrl() {

@Override
public String getThumbnailUrl() {
return featuredStory.has("art_id") ? getImageUrl(featuredStory.getLong("art_id"), true) : "";
return featuredStory.has("art_id") ? getImageUrl(featuredStory.getLong("art_id"), true)
: getImageUrl(featuredStory.getLong("item_art_id"), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -16,6 +17,7 @@
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;

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

@Test
Expand All @@ -46,6 +48,21 @@ public void testHttps() throws ExtractionException, IOException {
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);
Expand Down