2018-10-11 21:10:22 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.peertube;
|
|
|
|
|
2020-06-13 20:25:38 +02:00
|
|
|
import com.grack.nanojson.JsonArray;
|
2020-02-08 23:58:46 +01:00
|
|
|
import com.grack.nanojson.JsonObject;
|
2022-12-04 19:15:44 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.InfoItemExtractor;
|
2020-06-13 20:25:38 +02:00
|
|
|
import org.schabi.newpipe.extractor.InfoItemsCollector;
|
2020-04-15 14:09:46 +02:00
|
|
|
import org.schabi.newpipe.extractor.Page;
|
2020-02-08 23:58:46 +01:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2022-12-04 21:44:00 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeChannelInfoItemExtractor;
|
2022-12-04 19:15:44 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubePlaylistInfoItemExtractor;
|
2020-12-23 12:59:37 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSepiaStreamInfoItemExtractor;
|
2020-06-13 20:25:38 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamInfoItemExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
2020-04-09 17:51:51 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Parser;
|
2020-05-02 08:21:47 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2020-02-08 23:58:46 +01:00
|
|
|
|
2020-10-18 05:48:14 +02:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.time.OffsetDateTime;
|
|
|
|
import java.time.ZoneOffset;
|
|
|
|
import java.time.format.DateTimeParseException;
|
2018-10-11 21:10:22 +02:00
|
|
|
|
2022-03-18 10:25:16 +01:00
|
|
|
public final class PeertubeParsingHelper {
|
2020-04-09 17:51:51 +02:00
|
|
|
public static final String START_KEY = "start";
|
|
|
|
public static final String COUNT_KEY = "count";
|
|
|
|
public static final int ITEMS_PER_PAGE = 12;
|
|
|
|
public static final String START_PATTERN = "start=(\\d*)";
|
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
private PeertubeParsingHelper() {
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
public static void validate(final JsonObject json) throws ContentNotAvailableException {
|
|
|
|
final String error = json.getString("error");
|
2020-05-02 08:21:47 +02:00
|
|
|
if (!Utils.isBlank(error)) {
|
2019-03-08 00:24:39 +01:00
|
|
|
throw new ContentNotAvailableException(error);
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 22:24:28 +01:00
|
|
|
|
2022-03-18 10:25:16 +01:00
|
|
|
public static OffsetDateTime parseDateFrom(final String textualUploadDate)
|
|
|
|
throws ParsingException {
|
2019-11-19 22:38:17 +01:00
|
|
|
try {
|
2020-10-18 05:48:14 +02:00
|
|
|
return OffsetDateTime.ofInstant(Instant.parse(textualUploadDate), ZoneOffset.UTC);
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final DateTimeParseException e) {
|
2019-11-19 22:38:17 +01:00
|
|
|
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
public static Page getNextPage(final String prevPageUrl, final long total) {
|
|
|
|
final String prevStart;
|
2020-04-09 17:51:51 +02:00
|
|
|
try {
|
|
|
|
prevStart = Parser.matchGroup1(START_PATTERN, prevPageUrl);
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final Parser.RegexException e) {
|
2020-04-15 14:09:46 +02:00
|
|
|
return null;
|
2020-04-09 17:51:51 +02:00
|
|
|
}
|
2022-03-18 10:25:16 +01:00
|
|
|
if (Utils.isBlank(prevStart)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
final long nextStart;
|
2020-04-09 17:51:51 +02:00
|
|
|
try {
|
|
|
|
nextStart = Long.parseLong(prevStart) + ITEMS_PER_PAGE;
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final NumberFormatException e) {
|
2020-04-15 14:09:46 +02:00
|
|
|
return null;
|
2020-04-09 17:51:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nextStart >= total) {
|
2020-04-15 14:09:46 +02:00
|
|
|
return null;
|
2020-04-09 17:51:51 +02:00
|
|
|
} else {
|
2022-03-18 10:25:16 +01:00
|
|
|
return new Page(prevPageUrl.replace(
|
|
|
|
START_KEY + "=" + prevStart, START_KEY + "=" + nextStart));
|
2020-04-09 17:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-13 20:25:38 +02:00
|
|
|
|
2022-03-18 10:25:16 +01:00
|
|
|
public static void collectStreamsFrom(final InfoItemsCollector collector,
|
|
|
|
final JsonObject json,
|
|
|
|
final String baseUrl) throws ParsingException {
|
2020-12-23 12:59:37 +01:00
|
|
|
collectStreamsFrom(collector, json, baseUrl, false);
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:37:32 +01:00
|
|
|
/**
|
|
|
|
* Collect stream from json with collector
|
|
|
|
*
|
|
|
|
* @param collector the collector used to collect information
|
|
|
|
* @param json the file to retrieve data from
|
|
|
|
* @param baseUrl the base Url of the instance
|
|
|
|
* @param sepia if we should use PeertubeSepiaStreamInfoItemExtractor
|
|
|
|
*/
|
2022-03-18 10:25:16 +01:00
|
|
|
public static void collectStreamsFrom(final InfoItemsCollector collector,
|
|
|
|
final JsonObject json,
|
|
|
|
final String baseUrl,
|
|
|
|
final boolean sepia) throws ParsingException {
|
2020-06-13 20:25:38 +02:00
|
|
|
final JsonArray contents;
|
|
|
|
try {
|
|
|
|
contents = (JsonArray) JsonUtils.getValue(json, "data");
|
2022-03-18 10:25:16 +01:00
|
|
|
} catch (final Exception e) {
|
2020-06-13 20:25:38 +02:00
|
|
|
throw new ParsingException("Unable to extract list info", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final Object c : contents) {
|
|
|
|
if (c instanceof JsonObject) {
|
2021-03-25 19:10:11 +01:00
|
|
|
JsonObject item = (JsonObject) c;
|
|
|
|
|
|
|
|
// PeerTube playlists have the stream info encapsulated in an "video" object
|
|
|
|
if (item.has("video")) {
|
|
|
|
item = item.getObject("video");
|
|
|
|
}
|
2022-12-04 19:15:44 +01:00
|
|
|
final boolean isPlaylistInfoItem = item.has("videosLength");
|
2022-12-04 21:44:00 +01:00
|
|
|
final boolean isChannelInfoItem = item.has("followersCount");
|
2021-03-25 19:10:11 +01:00
|
|
|
|
2022-12-04 19:15:44 +01:00
|
|
|
final InfoItemExtractor extractor;
|
2020-12-23 12:59:37 +01:00
|
|
|
if (sepia) {
|
|
|
|
extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl);
|
2022-12-31 18:23:28 +01:00
|
|
|
} else if (isPlaylistInfoItem) {
|
|
|
|
extractor = new PeertubePlaylistInfoItemExtractor(item, baseUrl);
|
|
|
|
} else if (isChannelInfoItem) {
|
|
|
|
extractor = new PeertubeChannelInfoItemExtractor(item, baseUrl);
|
2020-12-23 12:59:37 +01:00
|
|
|
} else {
|
2022-12-31 18:23:28 +01:00
|
|
|
extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
|
2020-12-23 12:59:37 +01:00
|
|
|
}
|
2020-06-13 20:25:38 +02:00
|
|
|
collector.commit(extractor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 12:59:37 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|