Bandcamp featured kiosk

This commit is contained in:
Fynn Godau 2019-12-22 15:15:12 +01:00
parent db02850d4f
commit 13ef11e9c8
4 changed files with 190 additions and 8 deletions

View File

@ -6,22 +6,23 @@ import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.comments.CommentsExtractor; import org.schabi.newpipe.extractor.comments.CommentsExtractor;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; 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.kiosk.KioskList;
import org.schabi.newpipe.extractor.linkhandler.*; import org.schabi.newpipe.extractor.linkhandler.*;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.*; 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.*;
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.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.AUDIO; 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 { public class BandcampService extends StreamingService {
@ -76,7 +77,24 @@ public class BandcampService extends StreamingService {
@Override @Override
public KioskList getKioskList() throws ExtractionException { 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 @Override
@ -85,17 +103,17 @@ public class BandcampService extends StreamingService {
} }
@Override @Override
public PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) throws ExtractionException { public PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) {
return new BandcampPlaylistExtractor(this, linkHandler); return new BandcampPlaylistExtractor(this, linkHandler);
} }
@Override @Override
public StreamExtractor getStreamExtractor(LinkHandler linkHandler) throws ExtractionException { public StreamExtractor getStreamExtractor(LinkHandler linkHandler) {
return new BandcampStreamExtractor(this, linkHandler); return new BandcampStreamExtractor(this, linkHandler);
} }
@Override @Override
public CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) throws ExtractionException { public CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) {
return null; return null;
} }
} }

View File

@ -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<InfoItem> {
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<InfoItem> 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<InfoItem>(c, null);
}
@Override
public String getNextPageUrl() {
return null;
}
@Override
public InfoItemsPage getPage(String pageUrl) {
return null;
}
}

View File

@ -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<String> 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);
}
}

View File

@ -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<InfoItem> list = extractor.getInitialPage().getItems();
assertTrue(list.size() > 1);
}
}