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

127 lines
5.0 KiB
Java
Raw Normal View History

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;
import com.grack.nanojson.JsonObject;
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;
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeChannelInfoItemExtractor;
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;
import org.schabi.newpipe.extractor.utils.Parser;
2020-05-02 08:21:47 +02:00
import org.schabi.newpipe.extractor.utils.Utils;
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 {
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);
}
}
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 {
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;
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;
}
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;
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;
}
if (nextStart >= total) {
2020-04-15 14:09:46 +02:00
return null;
} else {
2022-03-18 10:25:16 +01:00
return new Page(prevPageUrl.replace(
START_KEY + "=" + prevStart, START_KEY + "=" + nextStart));
}
}
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");
}
final boolean isPlaylistInfoItem = item.has("videosLength");
final boolean isChannelInfoItem = item.has("followersCount");
2021-03-25 19:10:11 +01:00
final InfoItemExtractor extractor;
2020-12-23 12:59:37 +01:00
if (sepia) {
extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl);
} 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 {
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
}