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

110 lines
3.9 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.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.InfoItem;
import org.schabi.newpipe.extractor.MetaInfo;
2020-04-15 14:09:46 +02:00
import org.schabi.newpipe.extractor.Page;
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.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.MultiInfoItemsCollector;
2018-10-11 21:10:22 +02:00
import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
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;
import java.util.Collections;
import java.util.List;
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;
2020-06-13 20:25:38 +02:00
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeSearchExtractor extends SearchExtractor {
2020-12-23 12:59:37 +01:00
// if we should use PeertubeSepiaStreamInfoItemExtractor
2022-03-18 10:25:16 +01:00
private final boolean sepia;
2020-12-23 12:59:37 +01:00
2022-03-18 10:25:16 +01:00
public PeertubeSearchExtractor(final StreamingService service,
final SearchQueryHandler linkHandler) {
2020-12-23 12:59:37 +01:00
this(service, linkHandler, false);
}
2022-03-18 10:25:16 +01:00
public PeertubeSearchExtractor(final StreamingService service,
final SearchQueryHandler linkHandler,
final boolean sepia) {
2019-11-19 22:38:17 +01:00
super(service, linkHandler);
2020-12-23 12:59:37 +01:00
this.sepia = sepia;
2018-10-11 21:10:22 +02:00
}
@Nonnull
2018-10-11 21:10:22 +02:00
@Override
public String getSearchSuggestion() {
return "";
2018-10-11 21:10:22 +02:00
}
@Override
public boolean isCorrectedSearch() {
return false;
2018-10-11 21:10:22 +02:00
}
@Nonnull
@Override
public List<MetaInfo> getMetaInfo() {
return Collections.emptyList();
}
@Nonnull
2018-10-11 21:10:22 +02:00
@Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
2022-03-18 10:25:16 +01:00
return getPage(new Page(getUrl() + "&" + START_KEY + "=0&"
+ COUNT_KEY + "=" + ITEMS_PER_PAGE));
2018-10-11 21:10:22 +02:00
}
@Override
2022-03-18 10:25:16 +01:00
public InfoItemsPage<InfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL");
}
2020-04-15 14:09:46 +02:00
final Response response = getDownloader().get(page.getUrl());
2018-10-11 21:10:22 +02:00
JsonObject json = null;
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());
2022-03-18 10:25:16 +01:00
} catch (final Exception e) {
2018-10-11 21:10:22 +02:00
throw new ParsingException("Could not parse json data for search info", e);
}
}
if (json != null) {
2020-04-15 14:09:46 +02:00
PeertubeParsingHelper.validate(json);
2020-06-13 20:25:38 +02:00
final long total = json.getLong("total");
2020-04-15 14:09:46 +02:00
final MultiInfoItemsCollector collector = new MultiInfoItemsCollector(getServiceId());
2020-12-23 12:59:37 +01:00
collectStreamsFrom(collector, json, getBaseUrl(), sepia);
2020-04-15 14:09:46 +02:00
2022-03-18 10:25:16 +01:00
return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
2018-10-11 21:10:22 +02:00
} else {
2020-04-15 14:09:46 +02:00
throw new ExtractionException("Unable to get PeerTube search info");
2018-10-11 21:10:22 +02:00
}
}
@Override
2022-03-18 10:25:16 +01:00
public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException {
}
2018-10-11 21:10:22 +02:00
}