[Peertube] Fix checkstyle issues

This commit is contained in:
Stypox 2022-03-18 10:25:16 +01:00 committed by litetex
parent 9f7e06c817
commit 9ab32cb2e7
18 changed files with 377 additions and 242 deletions

View File

@ -5,7 +5,6 @@ import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException; import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
@ -18,14 +17,15 @@ public class PeertubeInstance {
private final String url; private final String url;
private String name; private String name;
public static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube"); public static final PeertubeInstance DEFAULT_INSTANCE
= new PeertubeInstance("https://framatube.org", "FramaTube");
public PeertubeInstance(String url) { public PeertubeInstance(final String url) {
this.url = url; this.url = url;
this.name = "PeerTube"; this.name = "PeerTube";
} }
public PeertubeInstance(String url, String name) { public PeertubeInstance(final String url, final String name) {
this.url = url; this.url = url;
this.name = name; this.name = name;
} }
@ -35,11 +35,9 @@ public class PeertubeInstance {
} }
public void fetchInstanceMetaData() throws Exception { public void fetchInstanceMetaData() throws Exception {
Downloader downloader = NewPipe.getDownloader(); final Response response;
Response response = null;
try { try {
response = downloader.get(url + "/api/v1/config"); response = NewPipe.getDownloader().get(url + "/api/v1/config");
} catch (ReCaptchaException | IOException e) { } catch (ReCaptchaException | IOException e) {
throw new Exception("unable to configure instance " + url, e); throw new Exception("unable to configure instance " + url, e);
} }
@ -49,7 +47,7 @@ public class PeertubeInstance {
} }
try { try {
JsonObject json = JsonParser.object().from(response.responseBody()); final JsonObject json = JsonParser.object().from(response.responseBody());
this.name = JsonUtils.getString(json, "instance.name"); this.name = JsonUtils.getString(json, "instance.name");
} catch (JsonParserException | ParsingException e) { } catch (JsonParserException | ParsingException e) {
throw new Exception("unable to parse instance config", e); throw new Exception("unable to parse instance config", e);

View File

@ -17,7 +17,7 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
public class PeertubeParsingHelper { public final class PeertubeParsingHelper {
public static final String START_KEY = "start"; public static final String START_KEY = "start";
public static final String COUNT_KEY = "count"; public static final String COUNT_KEY = "count";
public static final int ITEMS_PER_PAGE = 12; public static final int ITEMS_PER_PAGE = 12;
@ -33,10 +33,11 @@ public class PeertubeParsingHelper {
} }
} }
public static OffsetDateTime parseDateFrom(final String textualUploadDate) throws ParsingException { public static OffsetDateTime parseDateFrom(final String textualUploadDate)
throws ParsingException {
try { try {
return OffsetDateTime.ofInstant(Instant.parse(textualUploadDate), ZoneOffset.UTC); return OffsetDateTime.ofInstant(Instant.parse(textualUploadDate), ZoneOffset.UTC);
} catch (DateTimeParseException e) { } catch (final DateTimeParseException e) {
throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"", e); throw new ParsingException("Could not parse date: \"" + textualUploadDate + "\"", e);
} }
} }
@ -45,25 +46,31 @@ public class PeertubeParsingHelper {
final String prevStart; final String prevStart;
try { try {
prevStart = Parser.matchGroup1(START_PATTERN, prevPageUrl); prevStart = Parser.matchGroup1(START_PATTERN, prevPageUrl);
} catch (Parser.RegexException e) { } catch (final Parser.RegexException e) {
return null; return null;
} }
if (Utils.isBlank(prevStart)) return null; if (Utils.isBlank(prevStart)) {
return null;
}
final long nextStart; final long nextStart;
try { try {
nextStart = Long.parseLong(prevStart) + ITEMS_PER_PAGE; nextStart = Long.parseLong(prevStart) + ITEMS_PER_PAGE;
} catch (NumberFormatException e) { } catch (final NumberFormatException e) {
return null; return null;
} }
if (nextStart >= total) { if (nextStart >= total) {
return null; return null;
} else { } else {
return new Page(prevPageUrl.replace(START_KEY + "=" + prevStart, START_KEY + "=" + nextStart)); return new Page(prevPageUrl.replace(
START_KEY + "=" + prevStart, START_KEY + "=" + nextStart));
} }
} }
public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl) throws ParsingException { public static void collectStreamsFrom(final InfoItemsCollector collector,
final JsonObject json,
final String baseUrl) throws ParsingException {
collectStreamsFrom(collector, json, baseUrl, false); collectStreamsFrom(collector, json, baseUrl, false);
} }
@ -74,13 +81,15 @@ public class PeertubeParsingHelper {
* @param json the file to retrieve data from * @param json the file to retrieve data from
* @param baseUrl the base Url of the instance * @param baseUrl the base Url of the instance
* @param sepia if we should use PeertubeSepiaStreamInfoItemExtractor * @param sepia if we should use PeertubeSepiaStreamInfoItemExtractor
* @throws ParsingException
*/ */
public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl, boolean sepia) throws ParsingException { public static void collectStreamsFrom(final InfoItemsCollector collector,
final JsonObject json,
final String baseUrl,
final boolean sepia) throws ParsingException {
final JsonArray contents; final JsonArray contents;
try { try {
contents = (JsonArray) JsonUtils.getValue(json, "data"); contents = (JsonArray) JsonUtils.getValue(json, "data");
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Unable to extract list info", e); throw new ParsingException("Unable to extract list info", e);
} }
@ -93,7 +102,7 @@ public class PeertubeParsingHelper {
item = item.getObject("video"); item = item.getObject("video");
} }
PeertubeStreamInfoItemExtractor extractor; final PeertubeStreamInfoItemExtractor extractor;
if (sepia) { if (sepia) {
extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl); extractor = new PeertubeSepiaStreamInfoItemExtractor(item, baseUrl);
} else { } else {

View File

@ -1,35 +1,51 @@
package org.schabi.newpipe.extractor.services.peertube; package org.schabi.newpipe.extractor.services.peertube;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.COMMENTS;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.VIDEO;
import static java.util.Arrays.asList;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.comments.CommentsExtractor; import org.schabi.newpipe.extractor.comments.CommentsExtractor;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
import org.schabi.newpipe.extractor.kiosk.KioskList; import org.schabi.newpipe.extractor.kiosk.KioskList;
import org.schabi.newpipe.extractor.linkhandler.*; import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.search.SearchExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.*; import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeAccountExtractor;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.*; import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeChannelExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeCommentsExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubePlaylistExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSearchExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSuggestionExtractor;
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeTrendingExtractor;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeCommentsLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubePlaylistLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeStreamLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeTrendingLinkHandlerFactory;
import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor; import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor; import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
import java.util.List; import java.util.List;
import static java.util.Arrays.asList;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.COMMENTS;
import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.VIDEO;
public class PeertubeService extends StreamingService { public class PeertubeService extends StreamingService {
private PeertubeInstance instance; private PeertubeInstance instance;
public PeertubeService(int id) { public PeertubeService(final int id) {
this(id, PeertubeInstance.defaultInstance); this(id, PeertubeInstance.DEFAULT_INSTANCE);
} }
public PeertubeService(int id, PeertubeInstance instance) { public PeertubeService(final int id, final PeertubeInstance instance) {
super(id, "PeerTube", asList(VIDEO, COMMENTS)); super(id, "PeerTube", asList(VIDEO, COMMENTS));
this.instance = instance; this.instance = instance;
} }
@ -60,13 +76,10 @@ public class PeertubeService extends StreamingService {
} }
@Override @Override
public SearchExtractor getSearchExtractor(SearchQueryHandler queryHandler) { public SearchExtractor getSearchExtractor(final SearchQueryHandler queryHandler) {
final List<String> contentFilters = queryHandler.getContentFilters(); final List<String> contentFilters = queryHandler.getContentFilters();
boolean external = false; return new PeertubeSearchExtractor(this, queryHandler,
if (!contentFilters.isEmpty() && contentFilters.get(0).startsWith("sepia_")) { !contentFilters.isEmpty() && contentFilters.get(0).startsWith("sepia_"));
external = true;
}
return new PeertubeSearchExtractor(this, queryHandler, external);
} }
@Override @Override
@ -80,7 +93,7 @@ public class PeertubeService extends StreamingService {
} }
@Override @Override
public ChannelExtractor getChannelExtractor(ListLinkHandler linkHandler) public ChannelExtractor getChannelExtractor(final ListLinkHandler linkHandler)
throws ExtractionException { throws ExtractionException {
if (linkHandler.getUrl().contains("/video-channels/")) { if (linkHandler.getUrl().contains("/video-channels/")) {
@ -91,19 +104,19 @@ public class PeertubeService extends StreamingService {
} }
@Override @Override
public PlaylistExtractor getPlaylistExtractor(ListLinkHandler linkHandler) public PlaylistExtractor getPlaylistExtractor(final ListLinkHandler linkHandler)
throws ExtractionException { throws ExtractionException {
return new PeertubePlaylistExtractor(this, linkHandler); return new PeertubePlaylistExtractor(this, linkHandler);
} }
@Override @Override
public StreamExtractor getStreamExtractor(LinkHandler linkHandler) public StreamExtractor getStreamExtractor(final LinkHandler linkHandler)
throws ExtractionException { throws ExtractionException {
return new PeertubeStreamExtractor(this, linkHandler); return new PeertubeStreamExtractor(this, linkHandler);
} }
@Override @Override
public CommentsExtractor getCommentsExtractor(ListLinkHandler linkHandler) public CommentsExtractor getCommentsExtractor(final ListLinkHandler linkHandler)
throws ExtractionException { throws ExtractionException {
return new PeertubeCommentsExtractor(this, linkHandler); return new PeertubeCommentsExtractor(this, linkHandler);
} }
@ -117,34 +130,31 @@ public class PeertubeService extends StreamingService {
return this.instance; return this.instance;
} }
public void setInstance(PeertubeInstance instance) { public void setInstance(final PeertubeInstance instance) {
this.instance = instance; this.instance = instance;
} }
@Override @Override
public KioskList getKioskList() throws ExtractionException { public KioskList getKioskList() throws ExtractionException {
KioskList.KioskExtractorFactory kioskFactory = new KioskList.KioskExtractorFactory() { final KioskList.KioskExtractorFactory kioskFactory = (streamingService, url, id) ->
@Override new PeertubeTrendingExtractor(
public KioskExtractor createNewKiosk(StreamingService streamingService, PeertubeService.this,
String url, new PeertubeTrendingLinkHandlerFactory().fromId(id),
String id) id
throws ExtractionException { );
return new PeertubeTrendingExtractor(PeertubeService.this,
new PeertubeTrendingLinkHandlerFactory().fromId(id), id);
}
};
KioskList list = new KioskList(this); final KioskList list = new KioskList(this);
// add kiosks here e.g.: // add kiosks here e.g.:
final PeertubeTrendingLinkHandlerFactory h = new PeertubeTrendingLinkHandlerFactory(); final PeertubeTrendingLinkHandlerFactory h = new PeertubeTrendingLinkHandlerFactory();
try { try {
list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_TRENDING); list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_TRENDING);
list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_MOST_LIKED); list.addKioskEntry(kioskFactory, h,
PeertubeTrendingLinkHandlerFactory.KIOSK_MOST_LIKED);
list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_RECENT); list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_RECENT);
list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_LOCAL); list.addKioskEntry(kioskFactory, h, PeertubeTrendingLinkHandlerFactory.KIOSK_LOCAL);
list.setDefaultKiosk(PeertubeTrendingLinkHandlerFactory.KIOSK_TRENDING); list.setDefaultKiosk(PeertubeTrendingLinkHandlerFactory.KIOSK_TRENDING);
} catch (Exception e) { } catch (final Exception e) {
throw new ExtractionException(e); throw new ExtractionException(e);
} }

View File

@ -23,7 +23,10 @@ import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*; 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;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeAccountExtractor extends ChannelExtractor { public class PeertubeAccountExtractor extends ChannelExtractor {
@ -31,7 +34,8 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
private final String baseUrl; private final String baseUrl;
private static final String ACCOUNTS = "accounts/"; private static final String ACCOUNTS = "accounts/";
public PeertubeAccountExtractor(final StreamingService service, final ListLinkHandler linkHandler) throws ParsingException { public PeertubeAccountExtractor(final StreamingService service,
final ListLinkHandler linkHandler) throws ParsingException {
super(service, linkHandler); super(service, linkHandler);
this.baseUrl = getBaseUrl(); this.baseUrl = getBaseUrl();
} }
@ -41,7 +45,7 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
String value; String value;
try { try {
value = JsonUtils.getString(json, "avatar.path"); value = JsonUtils.getString(json, "avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -80,7 +84,8 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
subscribersCount += videoChannelJsonObject.getInt("followersCount"); subscribersCount += videoChannelJsonObject.getInt("followersCount");
} }
} catch (final IOException | JsonParserException | ReCaptchaException ignored) { } catch (final IOException | JsonParserException | ReCaptchaException ignored) {
// something went wrong during video channels extraction, only return subscribers of ownerAccount // something went wrong during video channels extraction,
// only return subscribers of ownerAccount
} }
return subscribersCount; return subscribersCount;
} }
@ -89,7 +94,7 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
public String getDescription() { public String getDescription() {
try { try {
return JsonUtils.getString(json, "description"); return JsonUtils.getString(json, "description");
} catch (ParsingException e) { } catch (final ParsingException e) {
return "No description"; return "No description";
} }
} }
@ -117,8 +122,8 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
return getPage(new Page( return getPage(new Page(baseUrl + "/api/v1/" + getId() + "/videos?" + START_KEY + "=0&"
baseUrl + "/api/v1/" + getId() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE)); + COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
@ -130,23 +135,24 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
final Response response = getDownloader().get(page.getUrl()); final Response response = getDownloader().get(page.getUrl());
JsonObject json = null; JsonObject pageJson = null;
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
json = JsonParser.object().from(response.responseBody()); pageJson = JsonParser.object().from(response.responseBody());
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not parse json data for account info", e); throw new ParsingException("Could not parse json data for account info", e);
} }
} }
if (json != null) { if (pageJson != null) {
PeertubeParsingHelper.validate(json); PeertubeParsingHelper.validate(pageJson);
final long total = json.getLong("total"); final long total = pageJson.getLong("total");
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
collectStreamsFrom(collector, json, getBaseUrl()); collectStreamsFrom(collector, pageJson, getBaseUrl());
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else { } else {
throw new ExtractionException("Unable to get PeerTube account info"); throw new ExtractionException("Unable to get PeerTube account info");
} }
@ -173,10 +179,12 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
private void setInitialData(final String responseBody) throws ExtractionException { private void setInitialData(final String responseBody) throws ExtractionException {
try { try {
json = JsonParser.object().from(responseBody); json = JsonParser.object().from(responseBody);
} catch (JsonParserException e) { } catch (final JsonParserException e) {
throw new ExtractionException("Unable to extract PeerTube account data", e); throw new ExtractionException("Unable to extract PeerTube account data", e);
} }
if (json == null) throw new ExtractionException("Unable to extract PeerTube account data"); if (json == null) {
throw new ExtractionException("Unable to extract PeerTube account data");
}
} }
@Nonnull @Nonnull

View File

@ -21,15 +21,18 @@ import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*; 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;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeChannelExtractor extends ChannelExtractor { public class PeertubeChannelExtractor extends ChannelExtractor {
private JsonObject json; private JsonObject json;
private final String baseUrl; private final String baseUrl;
public PeertubeChannelExtractor(final StreamingService service, final ListLinkHandler linkHandler) throws ParsingException { public PeertubeChannelExtractor(final StreamingService service,
final ListLinkHandler linkHandler) throws ParsingException {
super(service, linkHandler); super(service, linkHandler);
this.baseUrl = getBaseUrl(); this.baseUrl = getBaseUrl();
} }
@ -39,7 +42,7 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
String value; String value;
try { try {
value = JsonUtils.getString(json, "avatar.path"); value = JsonUtils.getString(json, "avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -64,7 +67,7 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
public String getDescription() { public String getDescription() {
try { try {
return JsonUtils.getString(json, "description"); return JsonUtils.getString(json, "description");
} catch (ParsingException e) { } catch (final ParsingException e) {
return "No description"; return "No description";
} }
} }
@ -84,7 +87,7 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
String value; String value;
try { try {
value = JsonUtils.getString(json, "ownerAccount.avatar.path"); value = JsonUtils.getString(json, "ownerAccount.avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -98,45 +101,48 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
return getPage(new Page( return getPage(new Page(baseUrl + "/api/v1/" + getId() + "/videos?" + START_KEY + "=0&"
baseUrl + "/api/v1/" + getId() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE)); + COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) { if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL"); throw new IllegalArgumentException("Page doesn't contain an URL");
} }
final Response response = getDownloader().get(page.getUrl()); final Response response = getDownloader().get(page.getUrl());
JsonObject json = null; JsonObject pageJson = null;
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
json = JsonParser.object().from(response.responseBody()); pageJson = JsonParser.object().from(response.responseBody());
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not parse json data for channel info", e); throw new ParsingException("Could not parse json data for channel info", e);
} }
} }
if (json != null) { if (pageJson != null) {
PeertubeParsingHelper.validate(json); PeertubeParsingHelper.validate(pageJson);
final long total = json.getLong("total"); final long total = pageJson.getLong("total");
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
collectStreamsFrom(collector, json, getBaseUrl()); collectStreamsFrom(collector, pageJson, getBaseUrl());
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else { } else {
throw new ExtractionException("Unable to get PeerTube channel info"); throw new ExtractionException("Unable to get PeerTube channel info");
} }
} }
@Override @Override
public void onFetchPage(final Downloader downloader) throws IOException, ExtractionException { public void onFetchPage(final Downloader downloader)
throws IOException, ExtractionException {
final Response response = downloader.get( final Response response = downloader.get(
baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId()); baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId());
if (response != null ) { if (response != null) {
setInitialData(response.responseBody()); setInitialData(response.responseBody());
} else { } else {
throw new ExtractionException("Unable to extract PeerTube channel data"); throw new ExtractionException("Unable to extract PeerTube channel data");
@ -146,10 +152,12 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
private void setInitialData(final String responseBody) throws ExtractionException { private void setInitialData(final String responseBody) throws ExtractionException {
try { try {
json = JsonParser.object().from(responseBody); json = JsonParser.object().from(responseBody);
} catch (JsonParserException e) { } catch (final JsonParserException e) {
throw new ExtractionException("Unable to extract PeerTube channel data", e); throw new ExtractionException("Unable to extract PeerTube channel data", e);
} }
if (json == null) throw new ExtractionException("Unable to extract PeerTube channel data"); if (json == null) {
throw new ExtractionException("Unable to extract PeerTube channel data");
}
} }
@Nonnull @Nonnull

View File

@ -18,36 +18,41 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*; 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;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeCommentsExtractor extends CommentsExtractor { public class PeertubeCommentsExtractor extends CommentsExtractor {
public PeertubeCommentsExtractor(final StreamingService service, final ListLinkHandler uiHandler) { public PeertubeCommentsExtractor(final StreamingService service,
final ListLinkHandler uiHandler) {
super(service, uiHandler); super(service, uiHandler);
} }
@Override @Override
public InfoItemsPage<CommentsInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<CommentsInfoItem> getInitialPage()
final String pageUrl = getUrl() + "?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE; throws IOException, ExtractionException {
return getPage(new Page(pageUrl)); return getPage(new Page(getUrl() + "?" + START_KEY + "=0&"
+ COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
private void collectCommentsFrom(final CommentsInfoItemsCollector collector, final JsonObject json) throws ParsingException { private void collectCommentsFrom(final CommentsInfoItemsCollector collector,
final JsonObject json) throws ParsingException {
final JsonArray contents = json.getArray("data"); final JsonArray contents = json.getArray("data");
for (final Object c : contents) { for (final Object c : contents) {
if (c instanceof JsonObject) { if (c instanceof JsonObject) {
final JsonObject item = (JsonObject) c; final JsonObject item = (JsonObject) c;
if (!item.getBoolean("isDeleted")) { if (!item.getBoolean("isDeleted")) {
final PeertubeCommentsInfoItemExtractor extractor = new PeertubeCommentsInfoItemExtractor(item, this); collector.commit(new PeertubeCommentsInfoItemExtractor(item, this));
collector.commit(extractor);
} }
} }
} }
} }
@Override @Override
public InfoItemsPage<CommentsInfoItem> getPage(final Page page) throws IOException, ExtractionException { public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) { if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL"); throw new IllegalArgumentException("Page doesn't contain an URL");
} }
@ -58,7 +63,7 @@ public class PeertubeCommentsExtractor extends CommentsExtractor {
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
json = JsonParser.object().from(response.responseBody()); json = JsonParser.object().from(response.responseBody());
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not parse json data for comments info", e); throw new ParsingException("Could not parse json data for comments info", e);
} }
} }
@ -67,15 +72,18 @@ public class PeertubeCommentsExtractor extends CommentsExtractor {
PeertubeParsingHelper.validate(json); PeertubeParsingHelper.validate(json);
final long total = json.getLong("total"); final long total = json.getLong("total");
final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId()); final CommentsInfoItemsCollector collector
= new CommentsInfoItemsCollector(getServiceId());
collectCommentsFrom(collector, json); collectCommentsFrom(collector, json);
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else { } else {
throw new ExtractionException("Unable to get PeerTube kiosk info"); throw new ExtractionException("Unable to get PeerTube kiosk info");
} }
} }
@Override @Override
public void onFetchPage(Downloader downloader) { } public void onFetchPage(final Downloader downloader) {
}
} }

View File

@ -19,7 +19,9 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
private final String url; private final String url;
private final String baseUrl; private final String baseUrl;
public PeertubeCommentsInfoItemExtractor(final JsonObject item, final PeertubeCommentsExtractor extractor) throws ParsingException { public PeertubeCommentsInfoItemExtractor(final JsonObject item,
final PeertubeCommentsExtractor extractor)
throws ParsingException {
this.item = item; this.item = item;
this.url = extractor.getUrl(); this.url = extractor.getUrl();
this.baseUrl = extractor.getBaseUrl(); this.baseUrl = extractor.getBaseUrl();
@ -35,7 +37,7 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
String value; String value;
try { try {
value = JsonUtils.getString(item, "account.avatar.path"); value = JsonUtils.getString(item, "account.avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -63,7 +65,7 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
try { try {
final Document doc = Jsoup.parse(htmlText); final Document doc = Jsoup.parse(htmlText);
return doc.body().text(); return doc.body().text();
} catch (Exception e) { } catch (final Exception e) {
return htmlText.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", EMPTY_STRING); return htmlText.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", EMPTY_STRING);
} }
} }
@ -78,7 +80,7 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
String value; String value;
try { try {
value = JsonUtils.getString(item, "account.avatar.path"); value = JsonUtils.getString(item, "account.avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -86,13 +88,15 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac
@Override @Override
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
return JsonUtils.getString(item, "account.name") + "@" + JsonUtils.getString(item, "account.host"); return JsonUtils.getString(item, "account.name") + "@"
+ JsonUtils.getString(item, "account.host");
} }
@Override @Override
public String getUploaderUrl() throws ParsingException { public String getUploaderUrl() throws ParsingException {
final String name = JsonUtils.getString(item, "account.name"); final String name = JsonUtils.getString(item, "account.name");
final String host = JsonUtils.getString(item, "account.host"); final String host = JsonUtils.getString(item, "account.host");
return ServiceList.PeerTube.getChannelLHFactory().fromId("accounts/" + name + "@" + host, baseUrl).getUrl(); return ServiceList.PeerTube.getChannelLHFactory()
.fromId("accounts/" + name + "@" + host, baseUrl).getUrl();
} }
} }

View File

@ -19,13 +19,17 @@ import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.*; 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;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubePlaylistExtractor extends PlaylistExtractor { public class PeertubePlaylistExtractor extends PlaylistExtractor {
private JsonObject playlistInfo; private JsonObject playlistInfo;
public PeertubePlaylistExtractor(final StreamingService service, final ListLinkHandler linkHandler) { public PeertubePlaylistExtractor(final StreamingService service,
final ListLinkHandler linkHandler) {
super(service, linkHandler); super(service, linkHandler);
} }
@ -47,7 +51,8 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getUploaderAvatarUrl() throws ParsingException { public String getUploaderAvatarUrl() throws ParsingException {
return getBaseUrl() + playlistInfo.getObject("ownerAccount").getObject("avatar").getString("path"); return getBaseUrl()
+ playlistInfo.getObject("ownerAccount").getObject("avatar").getString("path");
} }
@Override @Override
@ -75,17 +80,20 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor {
@Nonnull @Nonnull
@Override @Override
public String getSubChannelAvatarUrl() throws ParsingException { public String getSubChannelAvatarUrl() throws ParsingException {
return getBaseUrl() + playlistInfo.getObject("videoChannel").getObject("avatar").getString("path"); return getBaseUrl()
+ playlistInfo.getObject("videoChannel").getObject("avatar").getString("path");
} }
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
return getPage(new Page(getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE)); return getPage(new Page(getUrl() + "/videos?" + START_KEY + "=0&"
+ COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) { if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL"); throw new IllegalArgumentException("Page doesn't contain an URL");
} }
@ -96,7 +104,7 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor {
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
json = JsonParser.object().from(response.responseBody()); json = JsonParser.object().from(response.responseBody());
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not parse json data for playlist info", e); throw new ParsingException("Could not parse json data for playlist info", e);
} }
} }
@ -108,18 +116,20 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
collectStreamsFrom(collector, json, getBaseUrl()); collectStreamsFrom(collector, json, getBaseUrl());
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else { } else {
throw new ExtractionException("Unable to get PeerTube playlist info"); throw new ExtractionException("Unable to get PeerTube playlist info");
} }
} }
@Override @Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException { public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException {
final Response response = downloader.get(getUrl()); final Response response = downloader.get(getUrl());
try { try {
playlistInfo = JsonParser.object().from(response.responseBody()); playlistInfo = JsonParser.object().from(response.responseBody());
} catch (JsonParserException jpe) { } catch (final JsonParserException jpe) {
throw new ExtractionException("Could not parse json", jpe); throw new ExtractionException("Could not parse json", jpe);
} }
PeertubeParsingHelper.validate(playlistInfo); PeertubeParsingHelper.validate(playlistInfo);

View File

@ -32,13 +32,16 @@ import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeSearchExtractor extends SearchExtractor { public class PeertubeSearchExtractor extends SearchExtractor {
// if we should use PeertubeSepiaStreamInfoItemExtractor // if we should use PeertubeSepiaStreamInfoItemExtractor
private boolean sepia; private final boolean sepia;
public PeertubeSearchExtractor(StreamingService service, SearchQueryHandler linkHandler) { public PeertubeSearchExtractor(final StreamingService service,
final SearchQueryHandler linkHandler) {
this(service, linkHandler, false); this(service, linkHandler, false);
} }
public PeertubeSearchExtractor(StreamingService service, SearchQueryHandler linkHandler, boolean sepia) { public PeertubeSearchExtractor(final StreamingService service,
final SearchQueryHandler linkHandler,
final boolean sepia) {
super(service, linkHandler); super(service, linkHandler);
this.sepia = sepia; this.sepia = sepia;
} }
@ -62,12 +65,13 @@ public class PeertubeSearchExtractor extends SearchExtractor {
@Override @Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
final String pageUrl = getUrl() + "&" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE; return getPage(new Page(getUrl() + "&" + START_KEY + "=0&"
return getPage(new Page(pageUrl)); + COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, ExtractionException { public InfoItemsPage<InfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) { if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL"); throw new IllegalArgumentException("Page doesn't contain an URL");
} }
@ -78,7 +82,7 @@ public class PeertubeSearchExtractor extends SearchExtractor {
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
json = JsonParser.object().from(response.responseBody()); json = JsonParser.object().from(response.responseBody());
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not parse json data for search info", e); throw new ParsingException("Could not parse json data for search info", e);
} }
} }
@ -90,12 +94,15 @@ public class PeertubeSearchExtractor extends SearchExtractor {
final MultiInfoItemsCollector collector = new MultiInfoItemsCollector(getServiceId()); final MultiInfoItemsCollector collector = new MultiInfoItemsCollector(getServiceId());
collectStreamsFrom(collector, json, getBaseUrl(), sepia); collectStreamsFrom(collector, json, getBaseUrl(), sepia);
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else { } else {
throw new ExtractionException("Unable to get PeerTube search info"); throw new ExtractionException("Unable to get PeerTube search info");
} }
} }
@Override @Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException { } public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException {
}
} }

View File

@ -7,7 +7,7 @@ import com.grack.nanojson.JsonObject;
*/ */
public class PeertubeSepiaStreamInfoItemExtractor extends PeertubeStreamInfoItemExtractor { public class PeertubeSepiaStreamInfoItemExtractor extends PeertubeStreamInfoItemExtractor {
public PeertubeSepiaStreamInfoItemExtractor(JsonObject item, String baseUrl) { public PeertubeSepiaStreamInfoItemExtractor(final JsonObject item, final String baseUrl) {
super(item, baseUrl); super(item, baseUrl);
final String embedUrl = super.item.getString("embedUrl"); final String embedUrl = super.item.getString("embedUrl");
final String embedPath = super.item.getString("embedPath"); final String embedPath = super.item.getString("embedPath");
@ -15,8 +15,8 @@ public class PeertubeSepiaStreamInfoItemExtractor extends PeertubeStreamInfoItem
setBaseUrl(itemBaseUrl); setBaseUrl(itemBaseUrl);
// Usually, all videos, pictures and other content are hosted on the instance, // Usually, all videos, pictures and other content are hosted on the instance,
// or can be accessed by the same URL path if the instance with baseUrl federates the one where the video is actually uploaded // or can be accessed by the same URL path if the instance with baseUrl federates the one
// But it can't be accessed with Sepiasearch, so we use the item's instance as base URL // where the video is actually uploaded. But it can't be accessed with Sepiasearch, so we
// use the item's instance as base URL.
} }
} }

View File

@ -1,11 +1,14 @@
package org.schabi.newpipe.extractor.services.peertube.extractors; package org.schabi.newpipe.extractor.services.peertube.extractors;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException; import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.MetaInfo;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Downloader;
@ -18,12 +21,17 @@ import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory; import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeSearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeStreamLinkHandlerFactory; import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeStreamLinkHandlerFactory;
import org.schabi.newpipe.extractor.stream.*; 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.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.stream.SubtitlesStream;
import org.schabi.newpipe.extractor.stream.VideoStream;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -32,15 +40,16 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import javax.annotation.Nullable;
public class PeertubeStreamExtractor extends StreamExtractor { public class PeertubeStreamExtractor extends StreamExtractor {
private final String baseUrl; private final String baseUrl;
private JsonObject json; private JsonObject json;
private final List<SubtitlesStream> subtitles = new ArrayList<>(); private final List<SubtitlesStream> subtitles = new ArrayList<>();
public PeertubeStreamExtractor(final StreamingService service, final LinkHandler linkHandler) throws ParsingException { public PeertubeStreamExtractor(final StreamingService service, final LinkHandler linkHandler)
throws ParsingException {
super(service, linkHandler); super(service, linkHandler);
this.baseUrl = getBaseUrl(); this.baseUrl = getBaseUrl();
} }
@ -73,9 +82,10 @@ public class PeertubeStreamExtractor extends StreamExtractor {
String text; String text;
try { try {
text = JsonUtils.getString(json, "description"); text = JsonUtils.getString(json, "description");
} catch (ParsingException e) { } catch (final ParsingException e) {
return Description.EMPTY_DESCRIPTION; return Description.EMPTY_DESCRIPTION;
} }
if (text.length() == 250 && text.substring(247).equals("...")) { if (text.length() == 250 && text.substring(247).equals("...")) {
//if description is shortened, get full description //if description is shortened, get full description
final Downloader dl = NewPipe.getDownloader(); final Downloader dl = NewPipe.getDownloader();
@ -140,7 +150,8 @@ public class PeertubeStreamExtractor extends StreamExtractor {
public String getUploaderUrl() throws ParsingException { public String getUploaderUrl() throws ParsingException {
final String name = JsonUtils.getString(json, "account.name"); final String name = JsonUtils.getString(json, "account.name");
final String host = JsonUtils.getString(json, "account.host"); final String host = JsonUtils.getString(json, "account.host");
return getService().getChannelLHFactory().fromId("accounts/" + name + "@" + host, baseUrl).getUrl(); return getService().getChannelLHFactory()
.fromId("accounts/" + name + "@" + host, baseUrl).getUrl();
} }
@Nonnull @Nonnull
@ -155,7 +166,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
String value; String value;
try { try {
value = JsonUtils.getString(json, "account.avatar.path"); value = JsonUtils.getString(json, "account.avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -179,7 +190,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
String value; String value;
try { try {
value = JsonUtils.getString(json, "channel.avatar.path"); value = JsonUtils.getString(json, "channel.avatar.path");
} catch (Exception e) { } catch (final Exception e) {
value = "/client/assets/images/default-avatar.png"; value = "/client/assets/images/default-avatar.png";
} }
return baseUrl + value; return baseUrl + value;
@ -200,35 +211,41 @@ public class PeertubeStreamExtractor extends StreamExtractor {
public List<VideoStream> getVideoStreams() throws ExtractionException { public List<VideoStream> getVideoStreams() throws ExtractionException {
assertPageFetched(); assertPageFetched();
final List<VideoStream> videoStreams = new ArrayList<>(); final List<VideoStream> videoStreams = new ArrayList<>();
// mp4 // mp4
try { try {
videoStreams.addAll(getVideoStreamsFromArray(json.getArray("files"))); videoStreams.addAll(getVideoStreamsFromArray(json.getArray("files")));
} catch (Exception ignored) { } } catch (final Exception ignored) { }
// HLS // HLS
try { try {
final JsonArray streamingPlaylists = json.getArray("streamingPlaylists"); final JsonArray streamingPlaylists = json.getArray("streamingPlaylists");
for (final Object p : streamingPlaylists) { for (final Object p : streamingPlaylists) {
if (!(p instanceof JsonObject)) continue; if (!(p instanceof JsonObject)) {
continue;
}
final JsonObject playlist = (JsonObject) p; final JsonObject playlist = (JsonObject) p;
videoStreams.addAll(getVideoStreamsFromArray(playlist.getArray("files"))); videoStreams.addAll(getVideoStreamsFromArray(playlist.getArray("files")));
} }
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not get video streams", e); throw new ParsingException("Could not get video streams", e);
} }
if (getStreamType() == StreamType.LIVE_STREAM) { if (getStreamType() == StreamType.LIVE_STREAM) {
final String url = getHlsUrl(); videoStreams.add(new VideoStream(getHlsUrl(), MediaFormat.MPEG_4, "720p"));
videoStreams.add(new VideoStream(url, MediaFormat.MPEG_4, "720p"));
} }
return videoStreams; return videoStreams;
} }
private List<VideoStream> getVideoStreamsFromArray(final JsonArray streams) throws ParsingException { private List<VideoStream> getVideoStreamsFromArray(final JsonArray streams)
throws ParsingException {
try { try {
final List<VideoStream> videoStreams = new ArrayList<>(); final List<VideoStream> videoStreams = new ArrayList<>();
for (final Object s : streams) { for (final Object s : streams) {
if (!(s instanceof JsonObject)) continue; if (!(s instanceof JsonObject)) {
continue;
}
final JsonObject stream = (JsonObject) s; final JsonObject stream = (JsonObject) s;
final String url; final String url;
if (stream.has("fileDownloadUrl")) { if (stream.has("fileDownloadUrl")) {
@ -240,13 +257,14 @@ public class PeertubeStreamExtractor extends StreamExtractor {
final String resolution = JsonUtils.getString(stream, "resolution.label"); final String resolution = JsonUtils.getString(stream, "resolution.label");
final String extension = url.substring(url.lastIndexOf(".") + 1); final String extension = url.substring(url.lastIndexOf(".") + 1);
final MediaFormat format = MediaFormat.getFromSuffix(extension); final MediaFormat format = MediaFormat.getFromSuffix(extension);
final VideoStream videoStream = new VideoStream(url, torrentUrl, format, resolution); final VideoStream videoStream
= new VideoStream(url, torrentUrl, format, resolution);
if (!Stream.containSimilarStream(videoStream, videoStreams)) { if (!Stream.containSimilarStream(videoStream, videoStreams)) {
videoStreams.add(videoStream); videoStreams.add(videoStream);
} }
} }
return videoStreams; return videoStreams;
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not get video streams from array"); throw new ParsingException("Could not get video streams from array");
} }
@ -287,8 +305,8 @@ public class PeertubeStreamExtractor extends StreamExtractor {
final String apiUrl; final String apiUrl;
if (tags.isEmpty()) { if (tags.isEmpty()) {
apiUrl = baseUrl + "/api/v1/accounts/" + JsonUtils.getString(json, "account.name") apiUrl = baseUrl + "/api/v1/accounts/" + JsonUtils.getString(json, "account.name")
+ "@" + JsonUtils.getString(json, "account.host") + + "@" + JsonUtils.getString(json, "account.host")
"/videos?start=0&count=8"; + "/videos?start=0&count=8";
} else { } else {
apiUrl = getRelatedItemsUrl(tags); apiUrl = getRelatedItemsUrl(tags);
} }
@ -313,7 +331,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
public String getSupportInfo() { public String getSupportInfo() {
try { try {
return JsonUtils.getString(json, "support"); return JsonUtils.getString(json, "support");
} catch (ParsingException e) { } catch (final ParsingException e) {
return ""; return "";
} }
} }
@ -326,16 +344,17 @@ public class PeertubeStreamExtractor extends StreamExtractor {
params.append("&tagsOneOf="); params.append("&tagsOneOf=");
params.append(URLEncoder.encode(tag, UTF_8)); params.append(URLEncoder.encode(tag, UTF_8));
} }
return url + "?" + params.toString(); return url + "?" + params;
} }
private void getStreamsFromApi(final StreamInfoItemsCollector collector, final String apiUrl) throws ReCaptchaException, IOException, ParsingException { private void getStreamsFromApi(final StreamInfoItemsCollector collector, final String apiUrl)
throws ReCaptchaException, IOException, ParsingException {
final Response response = getDownloader().get(apiUrl); final Response response = getDownloader().get(apiUrl);
JsonObject relatedVideosJson = null; JsonObject relatedVideosJson = null;
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
relatedVideosJson = JsonParser.object().from(response.responseBody()); relatedVideosJson = JsonParser.object().from(response.responseBody());
} catch (JsonParserException e) { } catch (final JsonParserException e) {
throw new ParsingException("Could not parse json data for related videos", e); throw new ParsingException("Could not parse json data for related videos", e);
} }
} }
@ -345,27 +364,33 @@ public class PeertubeStreamExtractor extends StreamExtractor {
} }
} }
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException { private void collectStreamsFrom(final StreamInfoItemsCollector collector,
final JsonObject jsonObject)
throws ParsingException {
final JsonArray contents; final JsonArray contents;
try { try {
contents = (JsonArray) JsonUtils.getValue(json, "data"); contents = (JsonArray) JsonUtils.getValue(jsonObject, "data");
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("unable to extract related videos", e); throw new ParsingException("unable to extract related videos", e);
} }
for (final Object c : contents) { for (final Object c : contents) {
if (c instanceof JsonObject) { if (c instanceof JsonObject) {
final JsonObject item = (JsonObject) c; final JsonObject item = (JsonObject) c;
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); final PeertubeStreamInfoItemExtractor extractor
= new PeertubeStreamInfoItemExtractor(item, baseUrl);
//do not add the same stream in related streams //do not add the same stream in related streams
if (!extractor.getUrl().equals(getUrl())) collector.commit(extractor); if (!extractor.getUrl().equals(getUrl())) {
collector.commit(extractor);
}
} }
} }
} }
@Override @Override
public void onFetchPage(final Downloader downloader) throws IOException, ExtractionException { public void onFetchPage(final Downloader downloader) throws IOException, ExtractionException {
final Response response = downloader.get(baseUrl + PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT + getId()); final Response response = downloader.get(
baseUrl + PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT + getId());
if (response != null) { if (response != null) {
setInitialData(response.responseBody()); setInitialData(response.responseBody());
} else { } else {
@ -378,7 +403,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
private void setInitialData(final String responseBody) throws ExtractionException { private void setInitialData(final String responseBody) throws ExtractionException {
try { try {
json = JsonParser.object().from(responseBody); json = JsonParser.object().from(responseBody);
} catch (JsonParserException e) { } catch (final JsonParserException e) {
throw new ExtractionException("Unable to extract PeerTube stream data", e); throw new ExtractionException("Unable to extract PeerTube stream data", e);
} }
if (json == null) { if (json == null) {
@ -402,11 +427,12 @@ public class PeertubeStreamExtractor extends StreamExtractor {
final String languageCode = JsonUtils.getString(caption, "language.id"); final String languageCode = JsonUtils.getString(caption, "language.id");
final String ext = url.substring(url.lastIndexOf(".") + 1); final String ext = url.substring(url.lastIndexOf(".") + 1);
final MediaFormat fmt = MediaFormat.getFromSuffix(ext); final MediaFormat fmt = MediaFormat.getFromSuffix(ext);
if (fmt != null && !isNullOrEmpty(languageCode)) if (fmt != null && !isNullOrEmpty(languageCode)) {
subtitles.add(new SubtitlesStream(fmt, languageCode, url, false)); subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
}
} }
} }
} catch (Exception e) { } catch (final Exception e) {
// ignore all exceptions // ignore all exceptions
} }
} }
@ -457,7 +483,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
public Locale getLanguageInfo() { public Locale getLanguageInfo() {
try { try {
return new Locale(JsonUtils.getString(json, "language.id")); return new Locale(JsonUtils.getString(json, "language.id"));
} catch (ParsingException e) { } catch (final ParsingException e) {
return null; return null;
} }
} }

View File

@ -27,7 +27,9 @@ import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelp
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class PeertubeTrendingExtractor extends KioskExtractor<StreamInfoItem> { public class PeertubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
public PeertubeTrendingExtractor(final StreamingService streamingService, final ListLinkHandler linkHandler, final String kioskId) { public PeertubeTrendingExtractor(final StreamingService streamingService,
final ListLinkHandler linkHandler,
final String kioskId) {
super(streamingService, linkHandler, kioskId); super(streamingService, linkHandler, kioskId);
} }
@ -38,12 +40,13 @@ public class PeertubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
final String pageUrl = getUrl() + "&" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE; return getPage(new Page(getUrl() + "&" + START_KEY + "=0&"
return getPage(new Page(pageUrl)); + COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (page == null || isNullOrEmpty(page.getUrl())) { if (page == null || isNullOrEmpty(page.getUrl())) {
throw new IllegalArgumentException("Page doesn't contain an URL"); throw new IllegalArgumentException("Page doesn't contain an URL");
} }
@ -54,7 +57,7 @@ public class PeertubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
if (response != null && !Utils.isBlank(response.responseBody())) { if (response != null && !Utils.isBlank(response.responseBody())) {
try { try {
json = JsonParser.object().from(response.responseBody()); json = JsonParser.object().from(response.responseBody());
} catch (Exception e) { } catch (final Exception e) {
throw new ParsingException("Could not parse json data for kiosk info", e); throw new ParsingException("Could not parse json data for kiosk info", e);
} }
} }
@ -66,12 +69,15 @@ public class PeertubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
collectStreamsFrom(collector, json, getBaseUrl()); collectStreamsFrom(collector, json, getBaseUrl());
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); return new InfoItemsPage<>(collector,
PeertubeParsingHelper.getNextPage(page.getUrl(), total));
} else { } else {
throw new ExtractionException("Unable to get PeerTube kiosk info"); throw new ExtractionException("Unable to get PeerTube kiosk info");
} }
} }
@Override @Override
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException { } public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException {
}
} }

View File

@ -7,39 +7,49 @@ import org.schabi.newpipe.extractor.utils.Parser;
import java.util.List; import java.util.List;
public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
private static final PeertubeChannelLinkHandlerFactory instance = new PeertubeChannelLinkHandlerFactory(); private static final PeertubeChannelLinkHandlerFactory INSTANCE
= new PeertubeChannelLinkHandlerFactory();
private static final String ID_PATTERN = "((accounts|a)|(video-channels|c))/([^/?&#]*)"; private static final String ID_PATTERN = "((accounts|a)|(video-channels|c))/([^/?&#]*)";
public static final String API_ENDPOINT = "/api/v1/"; public static final String API_ENDPOINT = "/api/v1/";
private PeertubeChannelLinkHandlerFactory() {
}
public static PeertubeChannelLinkHandlerFactory getInstance() { public static PeertubeChannelLinkHandlerFactory getInstance() {
return instance; return INSTANCE;
} }
@Override @Override
public String getId(String url) throws ParsingException { public String getId(final String url) throws ParsingException {
return fixId(Parser.matchGroup(ID_PATTERN, url, 0)); return fixId(Parser.matchGroup(ID_PATTERN, url, 0));
} }
@Override @Override
public String getUrl(String id, List<String> contentFilters, String searchFilter) throws ParsingException { public String getUrl(final String id,
final List<String> contentFilters,
final String searchFilter) throws ParsingException {
return getUrl(id, contentFilters, searchFilter, ServiceList.PeerTube.getBaseUrl()); return getUrl(id, contentFilters, searchFilter, ServiceList.PeerTube.getBaseUrl());
} }
@Override @Override
public String getUrl(String id, List<String> contentFilter, String sortFilter, String baseUrl) public String getUrl(final String id,
final List<String> contentFilter,
final String sortFilter,
final String baseUrl)
throws ParsingException { throws ParsingException {
if (id.matches(ID_PATTERN)) { if (id.matches(ID_PATTERN)) {
return baseUrl + "/" + fixId(id); return baseUrl + "/" + fixId(id);
} else { } else {
// This is needed for compatibility with older versions were we didn't support video channels yet // This is needed for compatibility with older versions were we didn't support
// video channels yet
return baseUrl + "/accounts/" + id; return baseUrl + "/accounts/" + id;
} }
} }
@Override @Override
public boolean onAcceptUrl(String url) { public boolean onAcceptUrl(final String url) {
return url.contains("/accounts/") || url.contains("/a/") return url.contains("/accounts/") || url.contains("/a/")
|| url.contains("/video-channels/") || url.contains("/c/"); || url.contains("/video-channels/") || url.contains("/c/");
} }
@ -56,11 +66,11 @@ public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
* @param id the id to fix * @param id the id to fix
* @return the fixed id * @return the fixed id
*/ */
private String fixId(String id) { private String fixId(final String id) {
if (id.startsWith("a/")) { if (id.startsWith("a/")) {
id = "accounts" + id.substring(1); return "accounts" + id.substring(1);
} else if (id.startsWith("c/")) { } else if (id.startsWith("c/")) {
id = "video-channels" + id.substring(1); return "video-channels" + id.substring(1);
} }
return id; return id;
} }

View File

@ -4,21 +4,24 @@ import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.FoundAdException; import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser;
import java.util.List; import java.util.List;
public class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory {
private static final PeertubeCommentsLinkHandlerFactory instance = new PeertubeCommentsLinkHandlerFactory(); private static final PeertubeCommentsLinkHandlerFactory INSTANCE
= new PeertubeCommentsLinkHandlerFactory();
private static final String COMMENTS_ENDPOINT = "/api/v1/videos/%s/comment-threads"; private static final String COMMENTS_ENDPOINT = "/api/v1/videos/%s/comment-threads";
private PeertubeCommentsLinkHandlerFactory() {
}
public static PeertubeCommentsLinkHandlerFactory getInstance() { public static PeertubeCommentsLinkHandlerFactory getInstance() {
return instance; return INSTANCE;
} }
@Override @Override
public String getId(String url) throws ParsingException, IllegalArgumentException { public String getId(final String url) throws ParsingException, IllegalArgumentException {
return PeertubeStreamLinkHandlerFactory.getInstance().getId(url); // the same id is needed return PeertubeStreamLinkHandlerFactory.getInstance().getId(url); // the same id is needed
} }
@ -28,13 +31,17 @@ public class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory {
} }
@Override @Override
public String getUrl(String id, List<String> contentFilter, String sortFilter) throws ParsingException { public String getUrl(final String id,
String baseUrl = ServiceList.PeerTube.getBaseUrl(); final List<String> contentFilter,
return getUrl(id, contentFilter, sortFilter, baseUrl); final String sortFilter) throws ParsingException {
return getUrl(id, contentFilter, sortFilter, ServiceList.PeerTube.getBaseUrl());
} }
@Override @Override
public String getUrl(String id, List<String> contentFilter, String sortFilter, String baseUrl) throws ParsingException { public String getUrl(final String id,
final List<String> contentFilter,
final String sortFilter,
final String baseUrl) throws ParsingException {
return baseUrl + String.format(COMMENTS_ENDPOINT, id); return baseUrl + String.format(COMMENTS_ENDPOINT, id);
} }

View File

@ -8,28 +8,36 @@ import org.schabi.newpipe.extractor.utils.Parser;
import java.util.List; import java.util.List;
public class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
private static final PeertubePlaylistLinkHandlerFactory instance = new PeertubePlaylistLinkHandlerFactory(); private static final PeertubePlaylistLinkHandlerFactory INSTANCE
= new PeertubePlaylistLinkHandlerFactory();
private static final String ID_PATTERN = "(/videos/watch/playlist/|/w/p/)([^/?&#]*)"; private static final String ID_PATTERN = "(/videos/watch/playlist/|/w/p/)([^/?&#]*)";
private PeertubePlaylistLinkHandlerFactory() {
}
public static PeertubePlaylistLinkHandlerFactory getInstance() { public static PeertubePlaylistLinkHandlerFactory getInstance() {
return instance; return INSTANCE;
} }
@Override @Override
public String getUrl(String id, List<String> contentFilters, String sortFilter) { public String getUrl(final String id,
String baseUrl = ServiceList.PeerTube.getBaseUrl(); final List<String> contentFilters,
return getUrl(id, contentFilters, sortFilter, baseUrl); final String sortFilter) {
return getUrl(id, contentFilters, sortFilter, ServiceList.PeerTube.getBaseUrl());
} }
@Override @Override
public String getUrl(String id, List<String> contentFilters, String sortFilter, String baseUrl) { public String getUrl(final String id,
final List<String> contentFilters,
final String sortFilter,
final String baseUrl) {
return baseUrl + "/api/v1/video-playlists/" + id; return baseUrl + "/api/v1/video-playlists/" + id;
} }
@Override @Override
public String getId(String url) throws ParsingException { public String getId(final String url) throws ParsingException {
return Parser.matchGroup(ID_PATTERN, url, 2); return Parser.matchGroup(ID_PATTERN, url, 2);
} }
@ -38,7 +46,7 @@ public class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
try { try {
getId(url); getId(url);
return true; return true;
} catch (ParsingException e) { } catch (final ParsingException e) {
return false; return false;
} }
} }

View File

@ -10,20 +10,25 @@ import java.util.List;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
public class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory { public final class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory {
public static final String VIDEOS = "videos"; public static final String VIDEOS = "videos";
public static final String SEPIA_VIDEOS = "sepia_videos"; // sepia is the global index public static final String SEPIA_VIDEOS = "sepia_videos"; // sepia is the global index
public static final String SEPIA_BASE_URL = "https://sepiasearch.org"; public static final String SEPIA_BASE_URL = "https://sepiasearch.org";
public static final String SEARCH_ENDPOINT = "/api/v1/search/videos"; public static final String SEARCH_ENDPOINT = "/api/v1/search/videos";
private PeertubeSearchQueryHandlerFactory() {
}
public static PeertubeSearchQueryHandlerFactory getInstance() { public static PeertubeSearchQueryHandlerFactory getInstance() {
return new PeertubeSearchQueryHandlerFactory(); return new PeertubeSearchQueryHandlerFactory();
} }
@Override @Override
public String getUrl(String searchString, List<String> contentFilters, String sortFilter) throws ParsingException { public String getUrl(final String searchString,
String baseUrl; final List<String> contentFilters,
final String sortFilter) throws ParsingException {
final String baseUrl;
if (!contentFilters.isEmpty() && contentFilters.get(0).startsWith("sepia_")) { if (!contentFilters.isEmpty() && contentFilters.get(0).startsWith("sepia_")) {
baseUrl = SEPIA_BASE_URL; baseUrl = SEPIA_BASE_URL;
} else { } else {
@ -33,10 +38,13 @@ public class PeertubeSearchQueryHandlerFactory extends SearchQueryHandlerFactory
} }
@Override @Override
public String getUrl(String searchString, List<String> contentFilters, String sortFilter, String baseUrl) throws ParsingException { public String getUrl(final String searchString,
final List<String> contentFilters,
final String sortFilter,
final String baseUrl) throws ParsingException {
try { try {
return baseUrl + SEARCH_ENDPOINT + "?search=" + URLEncoder.encode(searchString, UTF_8); return baseUrl + SEARCH_ENDPOINT + "?search=" + URLEncoder.encode(searchString, UTF_8);
} catch (UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
throw new ParsingException("Could not encode query", e); throw new ParsingException("Could not encode query", e);
} }
} }

View File

@ -6,9 +6,10 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser;
public class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory { public final class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {
private static final PeertubeStreamLinkHandlerFactory instance = new PeertubeStreamLinkHandlerFactory(); private static final PeertubeStreamLinkHandlerFactory INSTANCE
= new PeertubeStreamLinkHandlerFactory();
private static final String ID_PATTERN = "(/w/|(/videos/(watch/|embed/)?))(?!p/)([^/?&#]*)"; private static final String ID_PATTERN = "(/w/|(/videos/(watch/|embed/)?))(?!p/)([^/?&#]*)";
// we exclude p/ because /w/p/ is playlist, not video // we exclude p/ because /w/p/ is playlist, not video
public static final String VIDEO_API_ENDPOINT = "/api/v1/videos/"; public static final String VIDEO_API_ENDPOINT = "/api/v1/videos/";
@ -22,31 +23,33 @@ public class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {
} }
public static PeertubeStreamLinkHandlerFactory getInstance() { public static PeertubeStreamLinkHandlerFactory getInstance() {
return instance; return INSTANCE;
} }
@Override @Override
public String getUrl(String id) { public String getUrl(final String id) {
return getUrl(id, ServiceList.PeerTube.getBaseUrl()); return getUrl(id, ServiceList.PeerTube.getBaseUrl());
} }
@Override @Override
public String getUrl(String id, String baseUrl) { public String getUrl(final String id, final String baseUrl) {
return baseUrl + VIDEO_PATH + id; return baseUrl + VIDEO_PATH + id;
} }
@Override @Override
public String getId(String url) throws ParsingException, IllegalArgumentException { public String getId(final String url) throws ParsingException, IllegalArgumentException {
return Parser.matchGroup(ID_PATTERN, url, 4); return Parser.matchGroup(ID_PATTERN, url, 4);
} }
@Override @Override
public boolean onAcceptUrl(final String url) throws FoundAdException { public boolean onAcceptUrl(final String url) throws FoundAdException {
if (url.contains("/playlist/")) return false; if (url.contains("/playlist/")) {
return false;
}
try { try {
getId(url); getId(url);
return true; return true;
} catch (ParsingException e) { } catch (final ParsingException e) {
return false; return false;
} }
} }

View File

@ -9,10 +9,10 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
private static final PeertubeTrendingLinkHandlerFactory INSTANCE
private static final PeertubeTrendingLinkHandlerFactory instance = new PeertubeTrendingLinkHandlerFactory(); = new PeertubeTrendingLinkHandlerFactory();
public static final Map<String, String> KIOSK_MAP; public static final Map<String, String> KIOSK_MAP;
public static final Map<String, String> REVERSE_KIOSK_MAP; public static final Map<String, String> REVERSE_KIOSK_MAP;
@ -22,49 +22,52 @@ public class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
public static final String KIOSK_LOCAL = "Local"; public static final String KIOSK_LOCAL = "Local";
static { static {
Map<String, String> map = new HashMap<>(); final Map<String, String> map = new HashMap<>();
map.put(KIOSK_TRENDING, "%s/api/v1/videos?sort=-trending"); map.put(KIOSK_TRENDING, "%s/api/v1/videos?sort=-trending");
map.put(KIOSK_MOST_LIKED, "%s/api/v1/videos?sort=-likes"); map.put(KIOSK_MOST_LIKED, "%s/api/v1/videos?sort=-likes");
map.put(KIOSK_RECENT, "%s/api/v1/videos?sort=-publishedAt"); map.put(KIOSK_RECENT, "%s/api/v1/videos?sort=-publishedAt");
map.put(KIOSK_LOCAL, "%s/api/v1/videos?sort=-publishedAt&filter=local"); map.put(KIOSK_LOCAL, "%s/api/v1/videos?sort=-publishedAt&filter=local");
KIOSK_MAP = Collections.unmodifiableMap(map); KIOSK_MAP = Collections.unmodifiableMap(map);
Map<String, String> reverseMap = new HashMap<>(); final Map<String, String> reverseMap = new HashMap<>();
for (Map.Entry<String, String> entry : KIOSK_MAP.entrySet()) { for (final Map.Entry<String, String> entry : KIOSK_MAP.entrySet()) {
reverseMap.put(entry.getValue(), entry.getKey()); reverseMap.put(entry.getValue(), entry.getKey());
} }
REVERSE_KIOSK_MAP = Collections.unmodifiableMap(reverseMap); REVERSE_KIOSK_MAP = Collections.unmodifiableMap(reverseMap);
} }
public static PeertubeTrendingLinkHandlerFactory getInstance() { public static PeertubeTrendingLinkHandlerFactory getInstance() {
return instance; return INSTANCE;
} }
@Override @Override
public String getUrl(String id, List<String> contentFilters, String sortFilter) { public String getUrl(final String id,
String baseUrl = ServiceList.PeerTube.getBaseUrl(); final List<String> contentFilters,
return getUrl(id, contentFilters, sortFilter, baseUrl); final String sortFilter) {
return getUrl(id, contentFilters, sortFilter, ServiceList.PeerTube.getBaseUrl());
} }
@Override @Override
public String getUrl(String id, List<String> contentFilters, String sortFilter, String baseUrl) { public String getUrl(final String id,
final List<String> contentFilters,
final String sortFilter,
final String baseUrl) {
return String.format(KIOSK_MAP.get(id), baseUrl); return String.format(KIOSK_MAP.get(id), baseUrl);
} }
@Override @Override
public String getId(String url) throws ParsingException { public String getId(final String url) throws ParsingException {
String baseUrl = ServiceList.PeerTube.getBaseUrl(); final String cleanUrl = url.replace(ServiceList.PeerTube.getBaseUrl(), "%s");
url = url.replace(baseUrl, "%s"); if (cleanUrl.contains("/videos/trending")) {
if (url.contains("/videos/trending")) {
return KIOSK_TRENDING; return KIOSK_TRENDING;
} else if (url.contains("/videos/most-liked")) { } else if (cleanUrl.contains("/videos/most-liked")) {
return KIOSK_MOST_LIKED; return KIOSK_MOST_LIKED;
} else if (url.contains("/videos/recently-added")) { } else if (cleanUrl.contains("/videos/recently-added")) {
return KIOSK_RECENT; return KIOSK_RECENT;
} else if (url.contains("/videos/local")) { } else if (cleanUrl.contains("/videos/local")) {
return KIOSK_LOCAL; return KIOSK_LOCAL;
} else if (REVERSE_KIOSK_MAP.containsKey(url)) { } else if (REVERSE_KIOSK_MAP.containsKey(cleanUrl)) {
return REVERSE_KIOSK_MAP.get(url); return REVERSE_KIOSK_MAP.get(cleanUrl);
} else { } else {
throw new ParsingException("no id found for this url"); throw new ParsingException("no id found for this url");
} }
@ -72,6 +75,8 @@ public class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
@Override @Override
public boolean onAcceptUrl(final String url) { public boolean onAcceptUrl(final String url) {
return url.contains("/videos?") || url.contains("/videos/trending") || url.contains("/videos/most-liked") || url.contains("/videos/recently-added") || url.contains("/videos/local"); return url.contains("/videos?") || url.contains("/videos/trending")
|| url.contains("/videos/most-liked") || url.contains("/videos/recently-added")
|| url.contains("/videos/local");
} }
} }