2018-10-11 21:10:22 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.peertube.extractors;
|
|
|
|
|
2020-04-09 17:51:51 +02:00
|
|
|
import com.grack.nanojson.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2020-04-10 10:25:53 +02:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2019-11-19 22:38:17 +01:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2020-04-09 17:51:51 +02:00
|
|
|
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;
|
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
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.stream.StreamInfoItem;
|
2020-04-09 17:51:51 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
|
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
2018-10-11 21:10:22 +02:00
|
|
|
|
2020-02-08 23:58:46 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2020-04-09 17:51:51 +02: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;
|
2018-10-11 21:10:22 +02:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
public class PeertubePlaylistExtractor extends PlaylistExtractor {
|
2020-04-09 17:51:51 +02:00
|
|
|
private JsonObject playlistInfo;
|
|
|
|
private String initialPageUrl;
|
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
public PeertubePlaylistExtractor(final StreamingService service, final ListLinkHandler linkHandler) {
|
2019-11-19 22:38:17 +01:00
|
|
|
super(service, linkHandler);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
2020-04-09 17:51:51 +02:00
|
|
|
return getBaseUrl() + playlistInfo.getString("thumbnailPath");
|
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
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getUploaderUrl() {
|
2020-04-09 17:51:51 +02:00
|
|
|
return playlistInfo.getObject("ownerAccount").getString("url");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getUploaderName() {
|
2020-04-09 17:51:51 +02:00
|
|
|
return playlistInfo.getObject("ownerAccount").getString("displayName");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() throws ParsingException {
|
2020-04-09 17:51:51 +02:00
|
|
|
return getBaseUrl() + playlistInfo.getObject("ownerAccount").getObject("avatar").getString("path");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public long getStreamCount() {
|
2020-04-09 17:51:51 +02:00
|
|
|
return playlistInfo.getNumber("videosLength").longValue();
|
2020-05-09 09:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getSubChannelName() {
|
2020-05-09 09:52:24 +02:00
|
|
|
return playlistInfo.getObject("videoChannel").getString("displayName");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public String getSubChannelUrl() {
|
2020-05-09 09:52:24 +02:00
|
|
|
return playlistInfo.getObject("videoChannel").getString("url");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getSubChannelAvatarUrl() throws ParsingException {
|
|
|
|
return getBaseUrl() + playlistInfo.getObject("videoChannel").getObject("avatar").getString("path");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:51:51 +02:00
|
|
|
@Nonnull
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
|
2020-04-09 17:51:51 +02:00
|
|
|
return getPage(initialPageUrl);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
|
|
|
|
final Response response = getDownloader().get(pageUrl);
|
|
|
|
final JsonObject playlistVideos;
|
2020-04-09 17:51:51 +02:00
|
|
|
try {
|
|
|
|
playlistVideos = JsonParser.object().from(response.responseBody());
|
|
|
|
} catch (JsonParserException jpe) {
|
|
|
|
throw new ExtractionException("Could not parse json", jpe);
|
|
|
|
}
|
|
|
|
PeertubeParsingHelper.validate(playlistVideos);
|
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
final long total = JsonUtils.getNumber(playlistVideos, "total").longValue();
|
2020-04-09 17:51:51 +02:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2020-04-09 17:51:51 +02:00
|
|
|
|
2020-04-10 10:25:53 +02:00
|
|
|
final JsonArray videos = playlistVideos.getArray("data");
|
|
|
|
for (final Object o : videos) {
|
|
|
|
final JsonObject video = ((JsonObject) o).getObject("video");
|
2020-04-09 17:51:51 +02:00
|
|
|
collector.commit(new PeertubeStreamInfoItemExtractor(video, getBaseUrl()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPageUrl(pageUrl, total));
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-10 10:25:53 +02:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException {
|
|
|
|
final Response response = downloader.get(getUrl());
|
2020-04-09 17:51:51 +02:00
|
|
|
try {
|
|
|
|
playlistInfo = JsonParser.object().from(response.responseBody());
|
|
|
|
} catch (JsonParserException jpe) {
|
|
|
|
throw new ExtractionException("Could not parse json", jpe);
|
|
|
|
}
|
|
|
|
PeertubeParsingHelper.validate(playlistInfo);
|
|
|
|
initialPageUrl = getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE;
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:51:51 +02:00
|
|
|
@Nonnull
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
2020-04-09 17:51:51 +02:00
|
|
|
return playlistInfo.getString("displayName");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
}
|