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

85 lines
3.0 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 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";
2019-12-22 15:15:12 +01:00
private JsonObject json;
public BandcampFeaturedExtractor(final StreamingService streamingService, final ListLinkHandler listLinkHandler,
final String kioskId) {
2019-12-22 15:15:12 +01:00
super(streamingService, listLinkHandler, kioskId);
}
@Override
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
try {
json = JsonParser.object().from(
getDownloader().post(
FEATURED_API_URL, null, "{\"platform\":\"\",\"version\":0}".getBytes()
).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
public InfoItemsPage<PlaylistInfoItem> getInitialPage() throws IOException, ExtractionException {
2019-12-22 15:15:12 +01:00
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
2019-12-22 15:15:12 +01:00
final JsonArray featuredStories = json.getObject("feed_content")
.getObject("stories")
.getArray("featured");
2019-12-22 15:15:12 +01:00
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
}
return new InfoItemsPage<>(c, null);
2019-12-22 15:15:12 +01:00
}
@Override
2020-08-02 16:23:53 +02:00
public InfoItemsPage<PlaylistInfoItem> getPage(Page page) {
2019-12-22 15:15:12 +01:00
return null;
}
}