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

119 lines
4.6 KiB
Java
Raw Normal View History

2019-12-22 15:15:12 +01:00
// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later
package org.schabi.newpipe.extractor.services.bandcamp.extractors;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
2020-08-02 16:23:53 +02:00
import org.schabi.newpipe.extractor.Page;
2019-12-22 15:15:12 +01:00
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
2019-12-22 15:15:12 +01:00
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemsCollector;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
2019-12-22 15:15:12 +01:00
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;
public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem> {
2019-12-22 15:15:12 +01:00
public static final String KIOSK_FEATURED = "Featured";
public static final String FEATURED_API_URL = BASE_API_URL + "/mobile/24/bootstrap_data";
2022-03-18 17:50:25 +01:00
public static final String MORE_FEATURED_API_URL
= BASE_API_URL + "/mobile/24/feed_older_logged_out";
2019-12-22 15:15:12 +01:00
private JsonObject json;
2022-03-18 17:50:25 +01:00
public BandcampFeaturedExtractor(final StreamingService streamingService,
final ListLinkHandler listLinkHandler,
final String kioskId) {
2019-12-22 15:15:12 +01:00
super(streamingService, listLinkHandler, kioskId);
}
@Override
2022-03-18 17:50:25 +01:00
public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException {
try {
json = JsonParser.object().from(getDownloader().postWithContentTypeJson(
FEATURED_API_URL,
Collections.emptyMap(),
"{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8))
.responseBody());
} catch (final JsonParserException e) {
throw new ParsingException("Could not parse Bandcamp featured API response", e);
}
2019-12-22 15:15:12 +01:00
}
@Nonnull
@Override
public String getName() throws ParsingException {
return KIOSK_FEATURED;
2019-12-22 15:15:12 +01:00
}
@Nonnull
@Override
2022-03-18 17:50:25 +01:00
public InfoItemsPage<PlaylistInfoItem> getInitialPage()
throws IOException, ExtractionException {
final JsonArray featuredStories = json.getObject("feed_content")
.getObject("stories")
.getArray("featured");
2019-12-22 15:15:12 +01:00
2021-03-28 12:53:05 +02:00
return extractItems(featuredStories);
}
2022-03-18 17:50:25 +01:00
private InfoItemsPage<PlaylistInfoItem> extractItems(final JsonArray featuredStories) {
2021-03-28 12:53:05 +02:00
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
for (int i = 0; i < featuredStories.size(); i++) {
final JsonObject featuredStory = featuredStories.getObject(i);
2019-12-22 15:15:12 +01:00
if (featuredStory.isNull("album_title")) {
// Is not an album, ignore
continue;
2019-12-22 15:15:12 +01:00
}
c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory));
2019-12-22 15:15:12 +01:00
}
2021-03-28 12:53:05 +02:00
final JsonObject lastFeaturedStory = featuredStories.getObject(featuredStories.size() - 1);
2021-03-31 10:43:13 +02:00
return new InfoItemsPage<>(c, getNextPageFrom(lastFeaturedStory));
2021-03-28 12:53:05 +02:00
}
2021-03-28 12:53:05 +02:00
/**
2021-03-31 10:43:13 +02:00
* Next Page can be generated from metadata of last featured story
2021-03-28 12:53:05 +02:00
*/
2022-03-18 17:50:25 +01:00
private Page getNextPageFrom(final JsonObject lastFeaturedStory) {
2021-03-28 12:53:05 +02:00
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
);
2019-12-22 15:15:12 +01:00
}
@Override
2022-03-18 17:50:25 +01:00
public InfoItemsPage<PlaylistInfoItem> getPage(final Page page)
throws IOException, ExtractionException {
2021-03-28 12:53:05 +02:00
2022-03-18 17:50:25 +01:00
final JsonObject response;
2021-03-28 12:53:05 +02:00
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"));
2019-12-22 15:15:12 +01:00
}
}