NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeTrendingExtractor.java

89 lines
3.7 KiB
Java
Raw Normal View History

2018-10-11 21:10:22 +02:00
package org.schabi.newpipe.extractor.services.peertube.extractors;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
2020-05-02 08:21:47 +02:00
2018-10-11 21:10:22 +02:00
import org.schabi.newpipe.extractor.StreamingService;
2019-11-19 22:38:17 +01:00
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Response;
2018-10-11 21:10:22 +02:00
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.services.peertube.PeertubeParsingHelper;
2018-10-11 21:10:22 +02:00
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.JsonUtils;
2020-05-02 08:21:47 +02:00
import org.schabi.newpipe.extractor.utils.Utils;
2018-10-11 21:10:22 +02:00
import java.io.IOException;
2018-10-11 21:10:22 +02:00
import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
2018-10-11 21:10:22 +02:00
public class PeertubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
public PeertubeTrendingExtractor(final StreamingService streamingService, final ListLinkHandler linkHandler, final String kioskId) {
2019-11-19 22:38:17 +01:00
super(streamingService, linkHandler, kioskId);
2018-10-11 21:10:22 +02:00
}
@Override
public String getName() throws ParsingException {
return getId();
}
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
final String pageUrl = getUrl() + "&" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE;
return getPage(pageUrl);
2018-10-11 21:10:22 +02:00
}
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException {
final JsonArray contents;
2018-10-11 21:10:22 +02:00
try {
contents = (JsonArray) JsonUtils.getValue(json, "data");
} catch (Exception e) {
throw new ParsingException("Unable to extract kiosk info", e);
2018-10-11 21:10:22 +02:00
}
final String baseUrl = getBaseUrl();
for (final Object c : contents) {
if (c instanceof JsonObject) {
2018-10-11 21:10:22 +02:00
final JsonObject item = (JsonObject) c;
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
2018-10-11 21:10:22 +02:00
collector.commit(extractor);
}
}
}
@Override
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
final Response response = getDownloader().get(pageUrl);
2018-10-11 21:10:22 +02:00
JsonObject json = null;
2020-05-02 08:21:47 +02:00
if (response != null && !Utils.isBlank(response.responseBody())) {
2018-10-11 21:10:22 +02:00
try {
2019-11-19 22:38:17 +01:00
json = JsonParser.object().from(response.responseBody());
2018-10-11 21:10:22 +02:00
} catch (Exception e) {
throw new ParsingException("Could not parse json data for kiosk info", e);
}
}
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
final long total;
if (json != null) {
final Number number = JsonUtils.getNumber(json, "total");
total = number.longValue();
collectStreamsFrom(collector, json);
2018-10-11 21:10:22 +02:00
} else {
throw new ExtractionException("Unable to get peertube kiosk info");
}
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPageUrl(pageUrl, total));
2018-10-11 21:10:22 +02:00
}
@Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException { }
2018-10-11 21:10:22 +02:00
}