109 lines
3.9 KiB
Java
109 lines
3.9 KiB
Java
package org.schabi.newpipe.extractor.services.peertube.extractors;
|
|
|
|
import com.grack.nanojson.JsonArray;
|
|
import com.grack.nanojson.JsonObject;
|
|
import com.grack.nanojson.JsonParser;
|
|
import org.jsoup.helper.StringUtil;
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
|
import org.schabi.newpipe.extractor.InfoItemExtractor;
|
|
import org.schabi.newpipe.extractor.InfoItemsCollector;
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
import org.schabi.newpipe.extractor.downloader.Response;
|
|
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.search.InfoItemsSearchCollector;
|
|
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
|
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
|
import org.schabi.newpipe.extractor.utils.Parser;
|
|
import org.schabi.newpipe.extractor.utils.Parser.RegexException;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.io.IOException;
|
|
|
|
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*;
|
|
|
|
public class PeertubeSearchExtractor extends SearchExtractor {
|
|
|
|
private InfoItemsPage<InfoItem> initPage;
|
|
private long total;
|
|
|
|
public PeertubeSearchExtractor(StreamingService service, SearchQueryHandler linkHandler) {
|
|
super(service, linkHandler);
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getSearchSuggestion() throws ParsingException {
|
|
return "";
|
|
}
|
|
|
|
@Override
|
|
public boolean isCorrectedSearch() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
|
super.fetchPage();
|
|
return initPage;
|
|
}
|
|
|
|
private InfoItemsCollector<InfoItem, InfoItemExtractor> collectStreamsFrom(JsonObject json) throws ParsingException {
|
|
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
|
|
|
|
JsonArray contents;
|
|
try {
|
|
contents = (JsonArray) JsonUtils.getValue(json, "data");
|
|
} catch (Exception e) {
|
|
throw new ParsingException("unable to extract search info", e);
|
|
}
|
|
|
|
String baseUrl = getBaseUrl();
|
|
for (Object c : contents) {
|
|
if (c instanceof JsonObject) {
|
|
final JsonObject item = (JsonObject) c;
|
|
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
|
|
collector.commit(extractor);
|
|
}
|
|
}
|
|
|
|
return collector;
|
|
|
|
}
|
|
|
|
@Override
|
|
public String getNextPageUrl() throws IOException, ExtractionException {
|
|
super.fetchPage();
|
|
return initPage.getNextPageUrl();
|
|
}
|
|
|
|
@Override
|
|
public InfoItemsPage<InfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
|
|
Response response = getDownloader().get(pageUrl);
|
|
JsonObject json = null;
|
|
if (null != response && !StringUtil.isBlank(response.responseBody())) {
|
|
try {
|
|
json = JsonParser.object().from(response.responseBody());
|
|
} catch (Exception e) {
|
|
throw new ParsingException("Could not parse json data for search info", e);
|
|
}
|
|
}
|
|
|
|
if (json != null) {
|
|
total = JsonUtils.getNumber(json, "total").longValue();
|
|
return new InfoItemsPage<>(collectStreamsFrom(json), PeertubeParsingHelper.getNextPageUrl(pageUrl, total));
|
|
} else {
|
|
throw new ExtractionException("Unable to get peertube search info");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
|
|
initPage = getPage(getUrl() + "&" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE);
|
|
}
|
|
|
|
}
|