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.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2020-05-02 08:21:47 +02:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
import org.schabi.newpipe.extractor.MediaFormat;
|
added metadata, fix descriptions, fix thumbnail, update tests
thumbnail: quality before: https://peertube.cpy.re/static/thumbnails/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
quality after: https://peertube.cpy.re/static/previews/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
description: we were getting about the first 260 characters, we now get full description (with fallback to first 260 chars if the get request for full description fails)
test: updated tests to match description, also changed some test: it was assertEquals(extracted, expected), but the proper way to do it is assertEquals(expected, extracted)
metadata: got host, privacy (public, private, unlisted), licence, language, tags
2020-01-19 12:45:52 +01:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
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;
|
|
|
|
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.exceptions.ReCaptchaException;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
|
2019-11-19 22:38:17 +01:00
|
|
|
import org.schabi.newpipe.extractor.localization.DateWrapper;
|
2018-10-11 21:10:22 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
|
2018-12-26 09:59:23 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory;
|
2020-05-02 08:21:47 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.Description;
|
|
|
|
import org.schabi.newpipe.extractor.stream.Stream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamType;
|
|
|
|
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
2018-10-11 21:10:22 +02:00
|
|
|
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-02-08 23:58:46 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2020-01-23 14:19:22 +01:00
|
|
|
|
2020-05-02 08:21:47 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
public class PeertubeStreamExtractor extends StreamExtractor {
|
|
|
|
|
2020-01-24 20:16:24 +01:00
|
|
|
|
|
|
|
private final String baseUrl;
|
2018-10-11 21:10:22 +02:00
|
|
|
private JsonObject json;
|
2019-11-16 00:00:13 +01:00
|
|
|
private List<SubtitlesStream> subtitles = new ArrayList<>();
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2019-11-21 01:05:22 +01:00
|
|
|
public PeertubeStreamExtractor(StreamingService service, LinkHandler 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
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
2019-11-19 22:38:17 +01:00
|
|
|
public String getTextualUploadDate() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "publishedAt");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 22:38:17 +01:00
|
|
|
@Override
|
|
|
|
public DateWrapper getUploadDate() throws ParsingException {
|
|
|
|
final String textualUploadDate = getTextualUploadDate();
|
|
|
|
|
|
|
|
if (textualUploadDate == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DateWrapper(PeertubeParsingHelper.parseDateFrom(textualUploadDate));
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
added metadata, fix descriptions, fix thumbnail, update tests
thumbnail: quality before: https://peertube.cpy.re/static/thumbnails/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
quality after: https://peertube.cpy.re/static/previews/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
description: we were getting about the first 260 characters, we now get full description (with fallback to first 260 chars if the get request for full description fails)
test: updated tests to match description, also changed some test: it was assertEquals(extracted, expected), but the proper way to do it is assertEquals(expected, extracted)
metadata: got host, privacy (public, private, unlisted), licence, language, tags
2020-01-19 12:45:52 +01:00
|
|
|
return baseUrl + JsonUtils.getString(json, "previewPath");
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-02-06 23:35:46 +01:00
|
|
|
public Description getDescription() throws ParsingException {
|
|
|
|
String text;
|
2018-10-11 22:29:13 +02:00
|
|
|
try {
|
2020-02-06 23:35:46 +01:00
|
|
|
text = JsonUtils.getString(json, "description");
|
2020-01-20 14:36:12 +01:00
|
|
|
} catch (ParsingException e) {
|
2020-02-06 23:35:46 +01:00
|
|
|
return Description.emptyDescription;
|
2018-10-11 22:29:13 +02:00
|
|
|
}
|
2020-02-06 23:35:46 +01:00
|
|
|
if (text.length() == 250 && text.substring(247).equals("...")) {
|
2020-01-20 15:14:23 +01:00
|
|
|
//if description is shortened, get full description
|
2020-01-20 14:36:12 +01:00
|
|
|
Downloader dl = NewPipe.getDownloader();
|
|
|
|
try {
|
|
|
|
Response response = dl.get(getUrl() + "/description");
|
|
|
|
JsonObject jsonObject = JsonParser.object().from(response.responseBody());
|
2020-02-06 23:35:46 +01:00
|
|
|
text = JsonUtils.getString(jsonObject, "description");
|
2020-01-20 14:36:12 +01:00
|
|
|
} catch (ReCaptchaException | IOException | JsonParserException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
added metadata, fix descriptions, fix thumbnail, update tests
thumbnail: quality before: https://peertube.cpy.re/static/thumbnails/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
quality after: https://peertube.cpy.re/static/previews/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
description: we were getting about the first 260 characters, we now get full description (with fallback to first 260 chars if the get request for full description fails)
test: updated tests to match description, also changed some test: it was assertEquals(extracted, expected), but the proper way to do it is assertEquals(expected, extracted)
metadata: got host, privacy (public, private, unlisted), licence, language, tags
2020-01-19 12:45:52 +01:00
|
|
|
}
|
2020-02-07 13:28:27 +01:00
|
|
|
return new Description(text, Description.MARKDOWN);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getAgeLimit() throws ParsingException {
|
2020-01-23 04:42:54 +01:00
|
|
|
boolean isNSFW = JsonUtils.getBoolean(json, "nsfw");
|
|
|
|
if (isNSFW) {
|
|
|
|
return 18;
|
|
|
|
} else {
|
|
|
|
return NO_AGE_LIMIT;
|
|
|
|
}
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLength() throws ParsingException {
|
|
|
|
Number value = JsonUtils.getNumber(json, "duration");
|
|
|
|
return value.longValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getTimeStamp() throws ParsingException {
|
|
|
|
//TODO fetch timestamp from url if present;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getViewCount() throws ParsingException {
|
|
|
|
Number value = JsonUtils.getNumber(json, "views");
|
|
|
|
return value.longValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLikeCount() throws ParsingException {
|
|
|
|
Number value = JsonUtils.getNumber(json, "likes");
|
|
|
|
return value.longValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getDislikeCount() throws ParsingException {
|
|
|
|
Number value = JsonUtils.getNumber(json, "dislikes");
|
|
|
|
return value.longValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() throws ParsingException {
|
|
|
|
String name = JsonUtils.getString(json, "account.name");
|
|
|
|
String host = JsonUtils.getString(json, "account.host");
|
2020-04-09 19:37:49 +02:00
|
|
|
return getService().getChannelLHFactory().fromId("accounts/" + name + "@" + host, baseUrl).getUrl();
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderName() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "account.displayName");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() throws ParsingException {
|
2018-10-11 22:29:13 +02:00
|
|
|
String value;
|
|
|
|
try {
|
|
|
|
value = JsonUtils.getString(json, "account.avatar.path");
|
2020-01-24 20:16:24 +01:00
|
|
|
} catch (Exception e) {
|
2018-10-11 22:29:13 +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
|
|
|
}
|
|
|
|
|
2020-04-13 22:34:20 +02:00
|
|
|
@Override
|
2020-04-16 14:19:36 +02:00
|
|
|
public String getSubChannelUrl() throws ParsingException {
|
2020-04-13 22:34:20 +02:00
|
|
|
return JsonUtils.getString(json, "channel.url");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-04-16 14:19:36 +02:00
|
|
|
public String getSubChannelName() throws ParsingException {
|
2020-04-13 22:34:20 +02:00
|
|
|
return JsonUtils.getString(json, "channel.displayName");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-04-16 14:19:36 +02:00
|
|
|
public String getSubChannelAvatarUrl() throws ParsingException {
|
2020-04-13 22:34:20 +02:00
|
|
|
String value;
|
|
|
|
try {
|
|
|
|
value = JsonUtils.getString(json, "channel.avatar.path");
|
|
|
|
} catch (Exception e) {
|
|
|
|
value = "/client/assets/images/default-avatar.png";
|
|
|
|
}
|
|
|
|
return baseUrl + value;
|
|
|
|
}
|
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
@Override
|
|
|
|
public String getDashMpdUrl() throws ParsingException {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHlsUrl() throws ParsingException {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<AudioStream> getAudioStreams() throws IOException, ExtractionException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<VideoStream> getVideoStreams() throws IOException, ExtractionException {
|
|
|
|
assertPageFetched();
|
|
|
|
List<VideoStream> videoStreams = new ArrayList<>();
|
|
|
|
try {
|
2020-04-16 16:08:14 +02:00
|
|
|
JsonArray streams = json.getArray("files");
|
2020-01-24 20:16:24 +01:00
|
|
|
for (Object s : streams) {
|
|
|
|
if (!(s instanceof JsonObject)) continue;
|
2018-10-11 21:10:22 +02:00
|
|
|
JsonObject stream = (JsonObject) s;
|
|
|
|
String url = JsonUtils.getString(stream, "fileUrl");
|
2018-12-30 10:30:57 +01:00
|
|
|
String torrentUrl = JsonUtils.getString(stream, "torrentUrl");
|
2018-10-11 21:10:22 +02:00
|
|
|
String resolution = JsonUtils.getString(stream, "resolution.label");
|
|
|
|
String extension = url.substring(url.lastIndexOf(".") + 1);
|
|
|
|
MediaFormat format = MediaFormat.getFromSuffix(extension);
|
2018-12-30 10:30:57 +01:00
|
|
|
VideoStream videoStream = new VideoStream(url, torrentUrl, format, resolution);
|
2018-10-11 21:10:22 +02:00
|
|
|
if (!Stream.containSimilarStream(videoStream, videoStreams)) {
|
|
|
|
videoStreams.add(videoStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get video streams", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return videoStreams;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-25 14:42:22 +01:00
|
|
|
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
|
2019-11-16 00:00:13 +01:00
|
|
|
return subtitles;
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-25 14:42:22 +01:00
|
|
|
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
|
2019-11-16 00:00:13 +01:00
|
|
|
List<SubtitlesStream> filteredSubs = new ArrayList<>();
|
2020-01-24 20:16:24 +01:00
|
|
|
for (SubtitlesStream sub : subtitles) {
|
|
|
|
if (sub.getFormat() == format) {
|
2019-11-16 00:00:13 +01:00
|
|
|
filteredSubs.add(sub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredSubs;
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StreamType getStreamType() throws ParsingException {
|
|
|
|
return StreamType.VIDEO_STREAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-25 14:42:22 +01:00
|
|
|
public StreamInfoItem getNextStream() throws IOException, ExtractionException {
|
2018-10-11 21:10:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-25 14:42:22 +01:00
|
|
|
public StreamInfoItemsCollector getRelatedStreams() throws IOException, ExtractionException {
|
2018-10-11 21:10:22 +02:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2018-12-26 09:59:23 +01:00
|
|
|
List<String> tags = getTags();
|
|
|
|
String apiUrl = null;
|
2020-01-24 20:16:24 +01:00
|
|
|
if (!tags.isEmpty()) {
|
2018-12-26 09:59:23 +01:00
|
|
|
apiUrl = getRelatedStreamsUrl(tags);
|
2020-01-24 20:16:24 +01:00
|
|
|
|
|
|
|
} else {
|
2018-12-26 09:59:23 +01:00
|
|
|
apiUrl = getUploaderUrl() + "/videos?start=0&count=8";
|
|
|
|
}
|
2020-05-02 08:21:47 +02:00
|
|
|
if (!Utils.isBlank(apiUrl)) getStreamsFromApi(collector, apiUrl);
|
2018-10-11 21:10:22 +02:00
|
|
|
return collector;
|
|
|
|
}
|
added metadata, fix descriptions, fix thumbnail, update tests
thumbnail: quality before: https://peertube.cpy.re/static/thumbnails/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
quality after: https://peertube.cpy.re/static/previews/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
description: we were getting about the first 260 characters, we now get full description (with fallback to first 260 chars if the get request for full description fails)
test: updated tests to match description, also changed some test: it was assertEquals(extracted, expected), but the proper way to do it is assertEquals(expected, extracted)
metadata: got host, privacy (public, private, unlisted), licence, language, tags
2020-01-19 12:45:52 +01:00
|
|
|
|
|
|
|
@Override
|
2020-01-24 20:16:24 +01:00
|
|
|
public List<String> getTags() {
|
2018-12-26 09:59:23 +01:00
|
|
|
try {
|
|
|
|
return (List) JsonUtils.getArray(json, "tags");
|
|
|
|
} catch (Exception e) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 14:19:22 +01:00
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getSupportInfo() throws ParsingException {
|
2020-01-23 14:37:14 +01:00
|
|
|
try {
|
|
|
|
return JsonUtils.getString(json, "support");
|
|
|
|
} catch (ParsingException e) {
|
|
|
|
return "";
|
|
|
|
}
|
2020-01-23 14:19:22 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 09:59:23 +01:00
|
|
|
private String getRelatedStreamsUrl(List<String> tags) throws UnsupportedEncodingException {
|
2019-11-21 01:05:22 +01:00
|
|
|
String url = baseUrl + PeertubeSearchQueryHandlerFactory.SEARCH_ENDPOINT;
|
2018-12-26 09:59:23 +01:00
|
|
|
StringBuilder params = new StringBuilder();
|
|
|
|
params.append("start=0&count=8&sort=-createdAt");
|
2020-01-24 20:16:24 +01:00
|
|
|
for (String tag : tags) {
|
2018-12-26 09:59:23 +01:00
|
|
|
params.append("&tagsOneOf=");
|
|
|
|
params.append(URLEncoder.encode(tag, "UTF-8"));
|
|
|
|
}
|
|
|
|
return url + "?" + params.toString();
|
|
|
|
}
|
2018-10-11 21:10:22 +02:00
|
|
|
|
|
|
|
private void getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl) throws ReCaptchaException, IOException, ParsingException {
|
2019-11-19 22:38:17 +01:00
|
|
|
Response response = getDownloader().get(apiUrl);
|
2018-10-11 21:10:22 +02:00
|
|
|
JsonObject relatedVideosJson = null;
|
2020-05-02 08:21:47 +02:00
|
|
|
if (null != response && !Utils.isBlank(response.responseBody())) {
|
2018-10-11 21:10:22 +02:00
|
|
|
try {
|
2019-11-19 22:38:17 +01:00
|
|
|
relatedVideosJson = JsonParser.object().from(response.responseBody());
|
2018-10-11 21:10:22 +02:00
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not parse json data for related videos", e);
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
|
|
|
if (relatedVideosJson != null) {
|
2018-10-11 21:10:22 +02:00
|
|
|
collectStreamsFrom(collector, relatedVideosJson);
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonObject json) throws ParsingException {
|
|
|
|
JsonArray contents;
|
|
|
|
try {
|
|
|
|
contents = (JsonArray) JsonUtils.getValue(json, "data");
|
2020-01-24 20:16:24 +01:00
|
|
|
} catch (Exception e) {
|
2018-10-11 21:10:22 +02:00
|
|
|
throw new ParsingException("unable to extract related videos", e);
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
|
|
|
for (Object c : contents) {
|
|
|
|
if (c instanceof JsonObject) {
|
2018-10-11 21:10:22 +02:00
|
|
|
final JsonObject item = (JsonObject) c;
|
2019-11-21 00:30:09 +01:00
|
|
|
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
|
2018-12-26 09:59:23 +01:00
|
|
|
//do not add the same stream in related streams
|
2020-01-24 20:16:24 +01:00
|
|
|
if (!extractor.getUrl().equals(getUrl())) collector.commit(extractor);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2018-10-11 21:10:22 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getErrorMessage() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
|
2019-11-19 22:38:17 +01:00
|
|
|
Response response = downloader.get(getUrl());
|
2020-01-24 20:16:24 +01:00
|
|
|
if (null != response && null != response.responseBody()) {
|
2019-11-19 22:38:17 +01:00
|
|
|
setInitialData(response.responseBody());
|
2020-01-24 20:16:24 +01:00
|
|
|
} else {
|
2018-10-11 21:10:22 +02:00
|
|
|
throw new ExtractionException("Unable to extract peertube channel data");
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2019-11-16 00:00:13 +01:00
|
|
|
loadSubtitles();
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setInitialData(String responseBody) throws ExtractionException {
|
|
|
|
try {
|
|
|
|
json = JsonParser.object().from(responseBody);
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ExtractionException("Unable to extract peertube stream data", e);
|
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
if (null == json) throw new ExtractionException("Unable to extract peertube stream data");
|
2019-03-09 19:03:51 +01:00
|
|
|
PeertubeParsingHelper.validate(json);
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|
2020-01-24 20:16:24 +01:00
|
|
|
|
2019-11-16 00:00:13 +01:00
|
|
|
private void loadSubtitles() {
|
|
|
|
if (subtitles.isEmpty()) {
|
|
|
|
try {
|
2020-01-24 20:16:24 +01:00
|
|
|
Response response = getDownloader().get(getUrl() + "/captions");
|
2019-11-19 22:38:17 +01:00
|
|
|
JsonObject captionsJson = JsonParser.object().from(response.responseBody());
|
2019-11-16 00:00:13 +01:00
|
|
|
JsonArray captions = JsonUtils.getArray(captionsJson, "data");
|
2020-01-24 20:16:24 +01:00
|
|
|
for (Object c : captions) {
|
|
|
|
if (c instanceof JsonObject) {
|
|
|
|
JsonObject caption = (JsonObject) c;
|
2019-11-21 01:05:22 +01:00
|
|
|
String url = baseUrl + JsonUtils.getString(caption, "captionPath");
|
2019-11-16 00:00:13 +01:00
|
|
|
String languageCode = JsonUtils.getString(caption, "language.id");
|
|
|
|
String ext = url.substring(url.lastIndexOf(".") + 1);
|
|
|
|
MediaFormat fmt = MediaFormat.getFromSuffix(ext);
|
2020-01-24 20:16:24 +01:00
|
|
|
if (fmt != null && languageCode != null)
|
|
|
|
subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
|
2019-11-16 00:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
// ignore all exceptions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 21:10:22 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "name");
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:12:39 +01:00
|
|
|
@Override
|
|
|
|
public String getOriginalUrl() throws ParsingException {
|
2019-11-21 01:05:22 +01:00
|
|
|
return baseUrl + "/videos/watch/" + getId();
|
2019-11-15 22:12:39 +01:00
|
|
|
}
|
|
|
|
|
added metadata, fix descriptions, fix thumbnail, update tests
thumbnail: quality before: https://peertube.cpy.re/static/thumbnails/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
quality after: https://peertube.cpy.re/static/previews/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
description: we were getting about the first 260 characters, we now get full description (with fallback to first 260 chars if the get request for full description fails)
test: updated tests to match description, also changed some test: it was assertEquals(extracted, expected), but the proper way to do it is assertEquals(expected, extracted)
metadata: got host, privacy (public, private, unlisted), licence, language, tags
2020-01-19 12:45:52 +01:00
|
|
|
@Override
|
|
|
|
public String getHost() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "account.host");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getPrivacy() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "privacy.label");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getCategory() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "category.label");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getLicence() throws ParsingException {
|
|
|
|
return JsonUtils.getString(json, "licence.label");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-25 13:16:42 +01:00
|
|
|
public Locale getLanguageInfo() throws ParsingException {
|
|
|
|
try {
|
|
|
|
return new Locale(JsonUtils.getString(json, "language.id"));
|
|
|
|
} catch (ParsingException e) {
|
|
|
|
return null;
|
|
|
|
}
|
added metadata, fix descriptions, fix thumbnail, update tests
thumbnail: quality before: https://peertube.cpy.re/static/thumbnails/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
quality after: https://peertube.cpy.re/static/previews/d2a5ec78-5f85-4090-8ec5-dc1102e022ea.jpg
description: we were getting about the first 260 characters, we now get full description (with fallback to first 260 chars if the get request for full description fails)
test: updated tests to match description, also changed some test: it was assertEquals(extracted, expected), but the proper way to do it is assertEquals(expected, extracted)
metadata: got host, privacy (public, private, unlisted), licence, language, tags
2020-01-19 12:45:52 +01:00
|
|
|
}
|
2018-10-11 21:10:22 +02:00
|
|
|
}
|