Use propper structure in KioskExtractors

Made BandCampRadioExtractor a Kiosk which holds StreamInfoItems and not InfoItems.
This commit is contained in:
TobiGr 2021-02-16 21:09:35 +01:00
parent 54b8e54f80
commit 54aa5b3042
4 changed files with 45 additions and 54 deletions

View File

@ -24,6 +24,8 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
public static final String KIOSK_FEATURED = "Featured";
public static final String FEATURED_API_URL = "https://bandcamp.com/api/mobile/24/bootstrap_data";
private JsonObject json;
public BandcampFeaturedExtractor(final StreamingService streamingService, final ListLinkHandler listLinkHandler,
final String kioskId) {
super(streamingService, listLinkHandler, kioskId);
@ -31,7 +33,15 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
@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);
}
}
@Nonnull
@ -46,35 +56,23 @@ public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem>
final PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId());
try {
final JsonArray featuredStories = json.getObject("feed_content")
.getObject("stories")
.getArray("featured");
for (int i = 0; i < featuredStories.size(); i++) {
final JsonObject featuredStory = featuredStories.getObject(i);
final JsonObject json = JsonParser.object().from(
getDownloader().post(
FEATURED_API_URL, null, "{\"platform\":\"\",\"version\":0}".getBytes()
).responseBody()
);
final JsonArray featuredStories = json.getObject("feed_content")
.getObject("stories")
.getArray("featured");
for (int i = 0; i < featuredStories.size(); i++) {
final JsonObject featuredStory = featuredStories.getObject(i);
if (featuredStory.isNull("album_title")) {
// Is not an album, ignore
continue;
}
c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory));
if (featuredStory.isNull("album_title")) {
// Is not an album, ignore
continue;
}
return new InfoItemsPage<>(c, null);
} catch (final JsonParserException e) {
e.printStackTrace();
throw new ParsingException("JSON error", e);
c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory));
}
return new InfoItemsPage<>(c, null);
}
@Override

View File

@ -6,8 +6,6 @@ import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.InfoItemsCollector;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
@ -15,16 +13,19 @@ 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.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import javax.annotation.Nonnull;
import java.io.IOException;
public class BandcampRadioExtractor extends KioskExtractor<InfoItem> {
public class BandcampRadioExtractor extends KioskExtractor<StreamInfoItem> {
public static final String KIOSK_RADIO = "Radio";
public static final String RADIO_API_URL = "https://bandcamp.com/api/bcweekly/1/list";
private JsonObject json = null;
public BandcampRadioExtractor(final StreamingService streamingService, final ListLinkHandler linkHandler,
final String kioskId) {
super(streamingService, linkHandler, kioskId);
@ -32,7 +33,12 @@ public class BandcampRadioExtractor extends KioskExtractor<InfoItem> {
@Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException {
try {
json = JsonParser.object().from(
getDownloader().get(RADIO_API_URL).responseBody());
} catch (final JsonParserException e) {
throw new ExtractionException("Could not parse Bandcamp Radio API response", e);
}
}
@Nonnull
@ -43,36 +49,21 @@ public class BandcampRadioExtractor extends KioskExtractor<InfoItem> {
@Nonnull
@Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
final InfoItemsCollector c = new StreamInfoItemsCollector(getServiceId());
public InfoItemsPage<StreamInfoItem> getInitialPage() {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
try {
final JsonArray radioShows = json.getArray("results");
final JsonObject json = JsonParser.object().from(
getDownloader().get(
RADIO_API_URL
).responseBody()
);
final JsonArray radioShows = json.getArray("results");
for (int i = 0; i < radioShows.size(); i++) {
final JsonObject radioShow = radioShows.getObject(i);
c.commit(
new BandcampRadioInfoItemExtractor(radioShow)
);
}
} catch (final JsonParserException e) {
e.printStackTrace();
for (int i = 0; i < radioShows.size(); i++) {
final JsonObject radioShow = radioShows.getObject(i);
collector.commit(new BandcampRadioInfoItemExtractor(radioShow));
}
return new InfoItemsPage<InfoItem>(c, null);
return new InfoItemsPage<>(collector, null);
}
@Override
public InfoItemsPage<InfoItem> getPage(final Page page) {
public InfoItemsPage<StreamInfoItem> getPage(final Page page) {
return null;
}
}

View File

@ -31,6 +31,7 @@ public class BandcampFeaturedExtractorTest implements BaseListExtractorTest {
NewPipe.init(DownloaderTestImpl.getInstance());
extractor = (BandcampFeaturedExtractor) Bandcamp
.getKioskList().getDefaultKioskExtractor();
extractor.fetchPage();
}
@Test

View File

@ -5,11 +5,11 @@ package org.schabi.newpipe.extractor.services.bandcamp;
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.downloader.DownloaderTestImpl;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.BaseListExtractorTest;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampRadioExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.io.IOException;
import java.util.List;
@ -31,11 +31,12 @@ public class BandcampRadioExtractorTest implements BaseListExtractorTest {
extractor = (BandcampRadioExtractor) Bandcamp
.getKioskList()
.getExtractorById("Radio", null);
extractor.fetchPage();
}
@Test
public void testRadioCount() throws ExtractionException, IOException {
final List<InfoItem> list = Bandcamp.getKioskList().getExtractorById("Radio", null).getInitialPage().getItems();
final List<StreamInfoItem> list = extractor.getInitialPage().getItems();
assertTrue(list.size() > 300);
}