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

188 lines
6.5 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;
import com.grack.nanojson.JsonParserException;
2018-10-11 21:10:22 +02:00
import org.jsoup.helper.StringUtil;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
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;
2019-03-08 00:24:39 +01:00
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;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Parser.RegexException;
import java.io.IOException;
2018-10-11 21:10:22 +02:00
public class PeertubeChannelExtractor extends ChannelExtractor {
2018-10-11 21:10:22 +02:00
private static final String START_KEY = "start";
private static final String COUNT_KEY = "count";
private static final int ITEMS_PER_PAGE = 12;
private static final String START_PATTERN = "start=(\\d*)";
2018-10-11 21:10:22 +02:00
private InfoItemsPage<StreamInfoItem> initPage;
private long total;
2018-10-11 21:10:22 +02:00
private JsonObject json;
private final String baseUrl;
2018-10-11 21:10:22 +02:00
public PeertubeChannelExtractor(StreamingService service, ListLinkHandler linkHandler) throws ParsingException {
2019-11-19 22:38:17 +01:00
super(service, linkHandler);
2019-11-22 19:35:49 +01:00
this.baseUrl = getBaseUrl();
2018-10-11 21:10:22 +02:00
}
@Override
public String getAvatarUrl() throws ParsingException {
2018-10-11 22:57:32 +02:00
String value;
try {
value = JsonUtils.getString(json, "avatar.path");
} catch (Exception e) {
2018-10-11 22:57:32 +02:00
value = "/client/assets/images/default-avatar.png";
}
return baseUrl + value;
2018-10-11 21:10:22 +02:00
}
@Override
public String getBannerUrl() throws ParsingException {
return null;
}
@Override
public String getFeedUrl() throws ParsingException {
return null;
}
@Override
public long getSubscriberCount() throws ParsingException {
Number number = JsonUtils.getNumber(json, "followersCount");
return number.longValue();
}
@Override
public String getDescription() throws ParsingException {
try {
return JsonUtils.getString(json, "description");
} catch (ParsingException e) {
2018-10-11 21:10:22 +02:00
return "No description";
}
}
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
super.fetchPage();
return initPage;
}
private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonObject json, String pageUrl) throws ParsingException {
JsonArray contents;
try {
contents = (JsonArray) JsonUtils.getValue(json, "data");
} catch (Exception e) {
2018-10-11 21:10:22 +02:00
throw new ParsingException("unable to extract channel streams", e);
}
for (Object c : contents) {
if (c instanceof JsonObject) {
2018-10-11 21:10:22 +02:00
final JsonObject item = (JsonObject) c;
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
2018-10-11 21:10:22 +02:00
collector.commit(extractor);
}
}
2018-10-11 21:10:22 +02:00
}
@Override
public String getNextPageUrl() throws IOException, ExtractionException {
super.fetchPage();
return initPage.getNextPageUrl();
}
@Override
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
2019-11-19 22:38:17 +01:00
Response response = getDownloader().get(pageUrl);
2018-10-11 21:10:22 +02:00
JsonObject json = null;
if (null != response && !StringUtil.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);
}
}
2018-10-11 21:10:22 +02:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
if (json != null) {
2019-03-08 00:24:39 +01:00
PeertubeParsingHelper.validate(json);
2018-10-11 21:10:22 +02:00
Number number = JsonUtils.getNumber(json, "total");
if (number != null) this.total = number.longValue();
2018-10-11 21:10:22 +02:00
collectStreamsFrom(collector, json, pageUrl);
} else {
throw new ExtractionException("Unable to get PeerTube kiosk info");
2018-10-11 21:10:22 +02:00
}
return new InfoItemsPage<>(collector, getNextPageUrl(pageUrl));
}
2018-10-11 21:10:22 +02:00
private String getNextPageUrl(String prevPageUrl) {
String prevStart;
try {
prevStart = Parser.matchGroup1(START_PATTERN, prevPageUrl);
} catch (RegexException e) {
return "";
}
if (StringUtil.isBlank(prevStart)) return "";
2018-10-11 21:10:22 +02:00
long nextStart = 0;
try {
nextStart = Long.valueOf(prevStart) + ITEMS_PER_PAGE;
} catch (NumberFormatException e) {
return "";
}
if (nextStart >= total) {
2018-10-11 21:10:22 +02:00
return "";
} else {
2018-10-11 21:10:22 +02:00
return prevPageUrl.replace(START_KEY + "=" + prevStart, START_KEY + "=" + String.valueOf(nextStart));
}
}
2018-10-11 21:10:22 +02:00
@Override
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
2019-11-19 22:38:17 +01:00
Response response = downloader.get(getUrl());
if (null != response && null != response.responseBody()) {
2019-11-19 22:38:17 +01:00
setInitialData(response.responseBody());
} else {
throw new ExtractionException("Unable to extract PeerTube channel data");
2018-10-11 21:10:22 +02:00
}
2018-10-11 21:10:22 +02:00
String pageUrl = getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE;
this.initPage = getPage(pageUrl);
}
private void setInitialData(String responseBody) throws ExtractionException {
try {
json = JsonParser.object().from(responseBody);
} catch (JsonParserException e) {
throw new ExtractionException("Unable to extract peertube channel data", e);
}
if (json == null) throw new ExtractionException("Unable to extract PeerTube channel data");
2018-10-11 21:10:22 +02:00
}
@Override
public String getName() throws ParsingException {
return JsonUtils.getString(json, "displayName");
}
2019-11-15 22:12:39 +01:00
@Override
public String getOriginalUrl() throws ParsingException {
return baseUrl + "/accounts/" + getId();
2019-11-15 22:12:39 +01:00
}
2018-10-11 21:10:22 +02:00
}