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.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;
|
2020-04-09 19:46:20 +02:00
|
|
|
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
|
|
|
|
2020-04-09 19:46:20 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2023-04-30 17:39:46 +02:00
|
|
|
import javax.annotation.Nullable;
|
2020-10-29 19:52:29 +01:00
|
|
|
import java.io.IOException;
|
2020-04-09 19:46:20 +02:00
|
|
|
|
2022-03-18 10:25:16 +01: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;
|
|
|
|
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
|
2020-05-11 15:25:18 +02:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
|
|
|
|
2020-04-09 17:51:51 +02:00
|
|
|
public class PeertubeChannelExtractor extends ChannelExtractor {
|
2018-10-11 21:10:22 +02:00
|
|
|
private JsonObject json;
|
2019-11-21 01:05:22 +01:00
|
|
|
private final String baseUrl;
|
2018-10-11 21:10:22 +02:00
|
|
|
|
2022-03-18 10:25:16 +01: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
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getAvatarUrl() {
|
2018-10-11 22:57:32 +02:00
|
|
|
String value;
|
|
|
|
try {
|
|
|
|
value = JsonUtils.getString(json, "avatar.path");
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final Exception e) {
|
2018-10-11 22:57:32 +02:00
|
|
|
value = "/client/assets/images/default-avatar.png";
|
|
|
|
}
|
2019-11-21 01:05:22 +01:00
|
|
|
return baseUrl + value;
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getBannerUrl() {
|
2018-10-11 21:10:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFeedUrl() throws ParsingException {
|
2020-04-09 19:37:49 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-30 17:39:46 +02:00
|
|
|
@Nullable
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getDescription() {
|
2023-04-30 17:39:46 +02:00
|
|
|
return json.getString("description");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:33:44 +02:00
|
|
|
@Override
|
2020-05-08 16:02:59 +02:00
|
|
|
public String getParentChannelName() throws ParsingException {
|
2020-04-13 22:33:44 +02:00
|
|
|
return JsonUtils.getString(json, "ownerAccount.name");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-05-08 16:02:59 +02:00
|
|
|
public String getParentChannelUrl() throws ParsingException {
|
2020-04-13 22:33:44 +02:00
|
|
|
return JsonUtils.getString(json, "ownerAccount.url");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getParentChannelAvatarUrl() {
|
2020-04-13 22:33:44 +02:00
|
|
|
String value;
|
|
|
|
try {
|
|
|
|
value = JsonUtils.getString(json, "ownerAccount.avatar.path");
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final Exception e) {
|
2020-04-13 22:33:44 +02:00
|
|
|
value = "/client/assets/images/default-avatar.png";
|
|
|
|
}
|
|
|
|
return baseUrl + value;
|
|
|
|
}
|
|
|
|
|
2021-01-22 01:44:58 +01:00
|
|
|
@Override
|
|
|
|
public boolean isVerified() throws ParsingException {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-09 19:46:20 +02:00
|
|
|
@Nonnull
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
|
2022-03-18 10:25:16 +01:00
|
|
|
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
|
2022-03-18 10:25:16 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> 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
|
|
|
|
2022-03-18 10:25:16 +01:00
|
|
|
JsonObject pageJson = 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 {
|
2022-03-18 10:25:16 +01:00
|
|
|
pageJson = JsonParser.object().from(response.responseBody());
|
|
|
|
} catch (final 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
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2022-03-18 10:25:16 +01:00
|
|
|
if (pageJson != null) {
|
|
|
|
PeertubeParsingHelper.validate(pageJson);
|
|
|
|
final long total = pageJson.getLong("total");
|
2020-04-15 14:09:46 +02:00
|
|
|
|
|
|
|
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2022-03-18 10:25:16 +01:00
|
|
|
collectStreamsFrom(collector, pageJson, getBaseUrl());
|
2020-04-15 14:09:46 +02:00
|
|
|
|
2022-03-18 10:25:16 +01: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
|
|
|
}
|
|
|
|
}
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
2022-03-17 16:25:49 +01:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader)
|
2022-03-18 10:25:16 +01:00
|
|
|
throws IOException, ExtractionException {
|
2020-04-09 19:46:20 +02:00
|
|
|
final Response response = downloader.get(
|
|
|
|
baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId());
|
2022-03-18 10:25:16 +01:00
|
|
|
if (response != null) {
|
2019-11-19 22:38:17 +01:00
|
|
|
setInitialData(response.responseBody());
|
2020-02-08 23:58:46 +01:00
|
|
|
} else {
|
2020-02-10 09:50:19 +01:00
|
|
|
throw new ExtractionException("Unable to extract PeerTube channel data");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
private void setInitialData(final String responseBody) throws ExtractionException {
|
2018-10-11 21:10:22 +02:00
|
|
|
try {
|
|
|
|
json = JsonParser.object().from(responseBody);
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final JsonParserException e) {
|
2020-04-10 10:25:53 +02:00
|
|
|
throw new ExtractionException("Unable to extract PeerTube channel data", e);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
2022-03-18 10:25:16 +01:00
|
|
|
if (json == null) {
|
|
|
|
throw new ExtractionException("Unable to extract PeerTube channel data");
|
|
|
|
}
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:46:20 +02:00
|
|
|
@Nonnull
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "displayName");
|
|
|
|
}
|
|
|
|
}
|