2018-10-11 21:10:22 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.peertube.extractors;
|
|
|
|
|
2020-02-08 23:58:46 +01:00
|
|
|
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.InfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.InfoItemExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.InfoItemsCollector;
|
|
|
|
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.search.InfoItemsSearchCollector;
|
|
|
|
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
2020-04-09 17:51:51 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
|
2018-10-11 21:10:22 +02:00
|
|
|
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
|
|
|
|
2020-02-08 23:58:46 +01:00
|
|
|
import java.io.IOException;
|
2018-10-11 21:10:22 +02:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
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-02-08 23:58:46 +01:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
public class PeertubeSearchExtractor extends SearchExtractor {
|
2019-11-19 22:38:17 +01:00
|
|
|
public PeertubeSearchExtractor(StreamingService service, SearchQueryHandler linkHandler) {
|
|
|
|
super(service, linkHandler);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:06:52 +02:00
|
|
|
@Nonnull
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getSearchSuggestion() {
|
2020-04-12 16:06:52 +02:00
|
|
|
return "";
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2020-04-11 19:18:08 +02:00
|
|
|
@Override
|
|
|
|
public boolean isCorrectedSearch() {
|
|
|
|
return false;
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
2020-04-10 10:25:53 +02:00
|
|
|
final String pageUrl = getUrl() + "&" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE;
|
|
|
|
return getPage(pageUrl);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
private InfoItemsCollector<InfoItem, InfoItemExtractor> collectStreamsFrom(final JsonObject json) throws ParsingException {
|
2020-03-23 22:05:02 +01:00
|
|
|
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
final JsonArray contents;
|
2018-10-11 21:10:22 +02:00
|
|
|
try {
|
|
|
|
contents = (JsonArray) JsonUtils.getValue(json, "data");
|
2020-02-08 23:58:46 +01:00
|
|
|
} catch (Exception e) {
|
2018-10-11 21:10:22 +02:00
|
|
|
throw new ParsingException("unable to extract search info", e);
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
final String baseUrl = getBaseUrl();
|
|
|
|
for (final Object c : contents) {
|
2020-02-08 23:58:46 +01:00
|
|
|
if (c instanceof JsonObject) {
|
2018-10-11 21:10:22 +02:00
|
|
|
final JsonObject item = (JsonObject) c;
|
2020-04-10 10:25:53 +02:00
|
|
|
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
|
2018-10-11 21:10:22 +02:00
|
|
|
collector.commit(extractor);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
return collector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public InfoItemsPage<InfoItem> 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-04-10 10:25:53 +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 search info", e);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
|
|
|
if (json != null) {
|
2020-04-10 10:25:53 +02:00
|
|
|
final long total = JsonUtils.getNumber(json, "total").longValue();
|
2020-04-09 17:51:51 +02:00
|
|
|
return new InfoItemsPage<>(collectStreamsFrom(json), PeertubeParsingHelper.getNextPageUrl(pageUrl, total));
|
2018-10-11 21:10:22 +02:00
|
|
|
} else {
|
|
|
|
throw new ExtractionException("Unable to get peertube search info");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException { }
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|