From 13ef11e9c8a1c60aaf8e68b63ad2fb88723569f9 Mon Sep 17 00:00:00 2001 From: Fynn Godau Date: Sun, 22 Dec 2019 15:15:12 +0100 Subject: [PATCH] Bandcamp featured kiosk --- .../services/bandcamp/BandcampService.java | 34 +++++-- .../extractors/BandcampFeaturedExtractor.java | 91 +++++++++++++++++++ .../BandcampFeaturedLinkHandlerFactory.java | 34 +++++++ .../BandcampFeaturedExtractorTest.java | 39 ++++++++ 4 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampFeaturedLinkHandlerFactory.java create mode 100644 extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java index 103ff6ec4..658101bd3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampService.java @@ -6,22 +6,23 @@ import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.comments.CommentsExtractor; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.kiosk.KioskExtractor; import org.schabi.newpipe.extractor.kiosk.KioskList; import org.schabi.newpipe.extractor.linkhandler.*; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.services.bandcamp.extractors.*; -import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampChannelLinkHandlerFactory; -import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampPlaylistLinkHandlerFactory; -import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampSearchQueryHandlerFactory; -import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampStreamLinkHandlerFactory; +import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.*; import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; +import java.io.IOException; import java.util.Collections; import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.AUDIO; +import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampFeaturedExtractor.KIOSK_FEATURED; +import static org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampFeaturedLinkHandlerFactory.FEATURED_API_URL; public class BandcampService extends StreamingService { @@ -76,7 +77,24 @@ public class BandcampService extends StreamingService { @Override public KioskList getKioskList() throws ExtractionException { - return new KioskList(this); + + KioskList kioskList = new KioskList(this); + try { + kioskList + .addKioskEntry(new KioskList.KioskExtractorFactory() { + @Override + public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String kioskId) throws ExtractionException { + return new BandcampFeaturedExtractor(BandcampService.this, new BandcampFeaturedLinkHandlerFactory().fromUrl(FEATURED_API_URL), kioskId); + } + }, new BandcampFeaturedLinkHandlerFactory(), KIOSK_FEATURED); + + kioskList.setDefaultKiosk(KIOSK_FEATURED); + + } catch (Exception e) { + throw new ExtractionException(e); + } + + return kioskList; } @Override @@ -85,17 +103,17 @@ public class BandcampService extends StreamingService { } @Override - public PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) throws ExtractionException { + public PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) { return new BandcampPlaylistExtractor(this, linkHandler); } @Override - public StreamExtractor getStreamExtractor(LinkHandler linkHandler) throws ExtractionException { + public StreamExtractor getStreamExtractor(LinkHandler linkHandler) { return new BandcampStreamExtractor(this, linkHandler); } @Override - public CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) throws ExtractionException { + public CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) { return null; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java new file mode 100644 index 000000000..5e8f21d49 --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java @@ -0,0 +1,91 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp.extractors; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.schabi.newpipe.extractor.Collector; +import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.InfoItemsCollector; +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; +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemsCollector; +import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector; +import org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampFeaturedLinkHandlerFactory; + +import javax.annotation.Nonnull; +import java.io.IOException; + +import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampChannelExtractor.getImageUrl; +import static org.schabi.newpipe.extractor.services.bandcamp.linkHandler.BandcampFeaturedLinkHandlerFactory.FEATURED_API_URL; + +public class BandcampFeaturedExtractor extends KioskExtractor { + + public static final String KIOSK_FEATURED = "Featured"; + + public BandcampFeaturedExtractor(StreamingService streamingService, ListLinkHandler listLinkHandler, String kioskId) { + super(streamingService, listLinkHandler, kioskId); + } + + @Override + public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException { + + } + + @Nonnull + @Override + public String getName() throws ParsingException { + return null; + } + + @Nonnull + @Override + public InfoItemsPage getInitialPage() throws IOException, ExtractionException { + + InfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId()); + + JSONObject json = new JSONObject( + getDownloader().post( + FEATURED_API_URL, null, "{\"platform\":\"\",\"version\":0}".getBytes() + ).responseBody() + ); + + JSONArray featuredStories = json.getJSONObject("feed_content") + .getJSONObject("stories") + .getJSONArray("featured"); + + for (int i = 0; i < featuredStories.length(); i++) { + JSONObject featuredStory = featuredStories.getJSONObject(i); + + if (featuredStory.isNull("album_title")) { + // Is not an album, ignore + continue; + } + + c.commit(new BandcampPlaylistInfoItemExtractor( + featuredStory.getString("album_title"), + featuredStory.getString("band_name"), + featuredStory.getString("item_url"), + featuredStory.has("art_id") ? getImageUrl(featuredStory.getLong("art_id"), true) : "", + featuredStory.getInt("num_streamable_tracks") + )); + } + + return new InfoItemsPage(c, null); + } + + @Override + public String getNextPageUrl() { + return null; + } + + @Override + public InfoItemsPage getPage(String pageUrl) { + return null; + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampFeaturedLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampFeaturedLinkHandlerFactory.java new file mode 100644 index 000000000..3fdcb33ba --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampFeaturedLinkHandlerFactory.java @@ -0,0 +1,34 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp.linkHandler; + +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; + +import java.util.List; + +import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampFeaturedExtractor.KIOSK_FEATURED; + +public class BandcampFeaturedLinkHandlerFactory extends ListLinkHandlerFactory { + + /** + * This kiosk doesn't have a corresponding website + */ + public static final String FEATURED_API_URL = "https://bandcamp.com/api/mobile/24/bootstrap_data"; + + @Override + public String getUrl(String id, List contentFilter, String sortFilter) { + if (id.equals(KIOSK_FEATURED)) return FEATURED_API_URL; + else return null; + } + + @Override + public String getId(String url) { + return KIOSK_FEATURED; + } + + @Override + public boolean onAcceptUrl(String url) { + return url.equals(FEATURED_API_URL); + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java new file mode 100644 index 000000000..258098d33 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java @@ -0,0 +1,39 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.schabi.newpipe.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.bandcamp.extractors.BandcampFeaturedExtractor; + +import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.assertTrue; +import static org.schabi.newpipe.extractor.ServiceList.bandcamp; + +/** + * Tests for {@link BandcampFeaturedExtractor} + */ +public class BandcampFeaturedExtractorTest { + + private static BandcampFeaturedExtractor extractor; + + @BeforeClass + public static void setUp() throws ExtractionException, IOException { + NewPipe.init(DownloaderTestImpl.getInstance()); + extractor = (BandcampFeaturedExtractor) bandcamp + .getKioskList().getDefaultKioskExtractor(); + } + + @Test + public void testKioskItems() throws ExtractionException, IOException { + List list = extractor.getInitialPage().getItems(); + assertTrue(list.size() > 1); + } + +}