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
|
|
|
|
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;
|
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.comments.CommentsInfoItemsCollector;
|
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.ListLinkHandler;
|
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-05-31 13:33:14 +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-05-11 15:25:18 +02:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
2018-10-11 21:10:22 +02:00
|
|
|
|
2020-04-09 17:51:51 +02:00
|
|
|
public class PeertubeCommentsExtractor extends CommentsExtractor {
|
2020-05-31 13:33:14 +02:00
|
|
|
public PeertubeCommentsExtractor(final StreamingService service, final ListLinkHandler uiHandler) {
|
2019-11-19 22:38:17 +01:00
|
|
|
super(service, uiHandler);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InfoItemsPage<CommentsInfoItem> getInitialPage() throws IOException, ExtractionException {
|
2020-04-10 10:25:53 +02:00
|
|
|
final String pageUrl = getUrl() + "?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE;
|
2020-04-15 14:09:46 +02:00
|
|
|
return getPage(new Page(pageUrl));
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 13:33:14 +02:00
|
|
|
private void collectCommentsFrom(final CommentsInfoItemsCollector collector, final JsonObject json) throws ParsingException {
|
|
|
|
final JsonArray contents = json.getArray("data");
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2020-05-31 13:33:14 +02:00
|
|
|
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-05-31 13:33:14 +02:00
|
|
|
if (!item.getBoolean("isDeleted")) {
|
|
|
|
final PeertubeCommentsInfoItemExtractor extractor = new PeertubeCommentsInfoItemExtractor(item, this);
|
|
|
|
collector.commit(extractor);
|
|
|
|
}
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-15 14:09:46 +02:00
|
|
|
public InfoItemsPage<CommentsInfoItem> getPage(final Page page) throws IOException, ExtractionException {
|
2020-05-11 15:25:18 +02:00
|
|
|
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());
|
2020-05-11 15:25:18 +02:00
|
|
|
|
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 comments info", e);
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
|
|
|
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 CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId());
|
2020-05-31 13:33:14 +02:00
|
|
|
collectCommentsFrom(collector, json);
|
2020-04-15 14:09:46 +02: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 kiosk info");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
@Override
|
|
|
|
public void onFetchPage(Downloader downloader) { }
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|