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

161 lines
5.6 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;
import com.grack.nanojson.JsonParserException;
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.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;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
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;
2020-05-02 08:21:47 +02:00
import org.schabi.newpipe.extractor.utils.Utils;
2018-10-11 21:10:22 +02:00
import javax.annotation.Nonnull;
import java.io.IOException;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeChannelExtractor extends ChannelExtractor {
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(final StreamingService service, final 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() {
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() {
2018-10-11 21:10:22 +02:00
return null;
}
@Override
public String getFeedUrl() throws ParsingException {
return getBaseUrl() + "/feeds/videos.xml?videoChannelId=" + json.get("id");
2018-10-11 21:10:22 +02:00
}
@Override
2020-06-13 20:25:38 +02:00
public long getSubscriberCount() {
return json.getLong("followersCount");
2018-10-11 21:10:22 +02:00
}
@Override
public String getDescription() {
2018-10-11 21:10:22 +02:00
try {
return JsonUtils.getString(json, "description");
} catch (ParsingException e) {
2018-10-11 21:10:22 +02:00
return "No description";
}
}
@Override
public String getParentChannelName() throws ParsingException {
return JsonUtils.getString(json, "ownerAccount.name");
}
@Override
public String getParentChannelUrl() throws ParsingException {
return JsonUtils.getString(json, "ownerAccount.url");
}
@Override
public String getParentChannelAvatarUrl() {
String value;
try {
value = JsonUtils.getString(json, "ownerAccount.avatar.path");
} catch (Exception e) {
value = "/client/assets/images/default-avatar.png";
}
return baseUrl + value;
}
@Override
public boolean isVerified() throws ParsingException {
return false;
}
@Nonnull
2018-10-11 21:10:22 +02:00
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
return getPage(new Page(
baseUrl + "/api/v1/" + getId() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE));
2018-10-11 21:10:22 +02:00
}
@Override
2020-04-15 14:09:46 +02:00
public InfoItemsPage<StreamInfoItem> 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;
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) {
2020-04-15 14:09:46 +02:00
throw new ParsingException("Could not parse json data for channel info", e);
2018-10-11 21:10:22 +02:00
}
}
if (json != null) {
2019-03-08 00:24:39 +01: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 StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2020-06-13 20:25:38 +02:00
collectStreamsFrom(collector, json, getBaseUrl());
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 channel info");
2018-10-11 21:10:22 +02:00
}
}
2018-10-11 21:10:22 +02:00
@Override
public void onFetchPage(final Downloader downloader) throws IOException, ExtractionException {
final Response response = downloader.get(
baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId());
2021-03-14 00:45:44 +01:00
if (response != null ) {
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
}
}
private void setInitialData(final String responseBody) throws ExtractionException {
2018-10-11 21:10:22 +02:00
try {
json = JsonParser.object().from(responseBody);
} catch (JsonParserException e) {
throw new ExtractionException("Unable to extract PeerTube channel data", e);
2018-10-11 21:10:22 +02:00
}
if (json == null) throw new ExtractionException("Unable to extract PeerTube channel data");
2018-10-11 21:10:22 +02:00
}
@Nonnull
2018-10-11 21:10:22 +02:00
@Override
public String getName() throws ParsingException {
return JsonUtils.getString(json, "displayName");
}
}