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

108 lines
4.1 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;
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;
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
public 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);
}
}
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);
} catch (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);
} catch (Parser.RegexException e) {
2020-04-15 14:09:46 +02:00
return null;
}
2020-04-15 14:09:46 +02:00
if (Utils.isBlank(prevStart)) return null;
final long nextStart;
try {
nextStart = Long.parseLong(prevStart) + ITEMS_PER_PAGE;
} catch (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 {
2020-04-15 14:09:46 +02:00
return new Page(prevPageUrl.replace(START_KEY + "=" + prevStart, START_KEY + "=" + nextStart));
}
}
2020-06-13 20:25:38 +02: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
* @throws ParsingException
*/
2020-12-23 12:59:37 +01:00
public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl, boolean sepia) throws ParsingException {
2020-06-13 20:25:38 +02:00
final JsonArray contents;
try {
contents = (JsonArray) JsonUtils.getValue(json, "data");
} catch (Exception e) {
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");
}
2020-12-23 12:59:37 +01:00
PeertubeStreamInfoItemExtractor extractor;
if (sepia) {
extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl);
} else {
extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
}
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
}