[PeerTube] Fix link handler inconsistency providing API links

This commit is contained in:
Stypox 2020-04-09 19:46:20 +02:00
parent 4349be13af
commit 7ae3cb6d07
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
8 changed files with 46 additions and 43 deletions

View File

@ -13,6 +13,7 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
@ -20,6 +21,8 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; 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.ITEMS_PER_PAGE;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
@ -85,10 +88,11 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
return ""; return "";
} }
@Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
final String pageUrl = getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE; return getPage(new Page(
return getPage(new Page(pageUrl)); getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
@ -123,7 +127,8 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
@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(getUrl()); final Response response = downloader.get(
baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId());
if (response != null && response.responseBody() != null) { if (response != null && response.responseBody() != null) {
setInitialData(response.responseBody()); setInitialData(response.responseBody());
} else { } else {
@ -140,13 +145,9 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
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
@Override @Override
public String getName() throws ParsingException { public String getName() throws ParsingException {
return JsonUtils.getString(json, "displayName"); return JsonUtils.getString(json, "displayName");
} }
@Override
public String getOriginalUrl() throws ParsingException {
return baseUrl + "/" + getId();
}
} }

View File

@ -13,6 +13,7 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
@ -20,6 +21,8 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; 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.ITEMS_PER_PAGE;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
@ -92,10 +95,11 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
return baseUrl + value; return baseUrl + value;
} }
@Nonnull
@Override @Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
final String pageUrl = getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE; return getPage(new Page(
return getPage(new Page(pageUrl)); getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE));
} }
@Override @Override
@ -130,7 +134,8 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
@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(getUrl()); final Response response = downloader.get(
baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT + getId());
if (response != null && response.responseBody() != null) { if (response != null && response.responseBody() != null) {
setInitialData(response.responseBody()); setInitialData(response.responseBody());
} else { } else {
@ -147,13 +152,9 @@ public class PeertubeChannelExtractor extends ChannelExtractor {
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
@Override @Override
public String getName() throws ParsingException { public String getName() throws ParsingException {
return JsonUtils.getString(json, "displayName"); return JsonUtils.getString(json, "displayName");
} }
@Override
public String getOriginalUrl() throws ParsingException {
return baseUrl + "/" + getId();
}
} }

View File

@ -17,6 +17,7 @@ import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.localization.DateWrapper; 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.stream.AudioStream; import org.schabi.newpipe.extractor.stream.AudioStream;
import org.schabi.newpipe.extractor.stream.Description; import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.Stream; import org.schabi.newpipe.extractor.stream.Stream;
@ -83,7 +84,9 @@ public class PeertubeStreamExtractor extends StreamExtractor {
//if description is shortened, get full description //if description is shortened, get full description
final Downloader dl = NewPipe.getDownloader(); final Downloader dl = NewPipe.getDownloader();
try { try {
final Response response = dl.get(getUrl() + "/description"); final Response response = dl.get(baseUrl
+ PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT
+ getId() + "/description");
final JsonObject jsonObject = JsonParser.object().from(response.responseBody()); final JsonObject jsonObject = JsonParser.object().from(response.responseBody());
text = JsonUtils.getString(jsonObject, "description"); text = JsonUtils.getString(jsonObject, "description");
} catch (ReCaptchaException | IOException | JsonParserException e) { } catch (ReCaptchaException | IOException | JsonParserException e) {
@ -338,7 +341,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
@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(getUrl()); final Response response = downloader.get(baseUrl + PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT + getId());
if (response != null && response.responseBody() != null) { if (response != null && response.responseBody() != null) {
setInitialData(response.responseBody()); setInitialData(response.responseBody());
} else { } else {
@ -354,14 +357,18 @@ public class PeertubeStreamExtractor extends StreamExtractor {
} catch (JsonParserException e) { } catch (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) throw new ExtractionException("Unable to extract PeerTube stream data"); if (json == null) {
throw new ExtractionException("Unable to extract PeerTube stream data");
}
PeertubeParsingHelper.validate(json); PeertubeParsingHelper.validate(json);
} }
private void loadSubtitles() { private void loadSubtitles() {
if (subtitles.isEmpty()) { if (subtitles.isEmpty()) {
try { try {
final Response response = getDownloader().get(getUrl() + "/captions"); final Response response = getDownloader().get(baseUrl
+ PeertubeStreamLinkHandlerFactory.VIDEO_API_ENDPOINT
+ getId() + "/captions");
final JsonObject captionsJson = JsonParser.object().from(response.responseBody()); final JsonObject captionsJson = JsonParser.object().from(response.responseBody());
final JsonArray captions = JsonUtils.getArray(captionsJson, "data"); final JsonArray captions = JsonUtils.getArray(captionsJson, "data");
for (final Object c : captions) { for (final Object c : captions) {
@ -387,12 +394,6 @@ public class PeertubeStreamExtractor extends StreamExtractor {
return JsonUtils.getString(json, "name"); return JsonUtils.getString(json, "name");
} }
@Nonnull
@Override
public String getOriginalUrl() throws ParsingException {
return baseUrl + "/videos/watch/" + getId();
}
@Nonnull @Nonnull
@Override @Override
public String getHost() throws ParsingException { public String getHost() throws ParsingException {

View File

@ -11,7 +11,7 @@ public 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|video-channels)/([^/?&#]*)"; private static final String ID_PATTERN = "(accounts|video-channels)/([^/?&#]*)";
private static final String API_ENDPOINT = "/api/v1/"; public static final String API_ENDPOINT = "/api/v1/";
public static PeertubeChannelLinkHandlerFactory getInstance() { public static PeertubeChannelLinkHandlerFactory getInstance() {
return instance; return instance;
@ -31,12 +31,11 @@ public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
@Override @Override
public String getUrl(String id, List<String> contentFilter, String sortFilter, String baseUrl) public String getUrl(String id, List<String> contentFilter, String sortFilter, String baseUrl)
throws ParsingException { throws ParsingException {
if (id.matches(ID_PATTERN)) { if (id.matches(ID_PATTERN)) {
return baseUrl + API_ENDPOINT + id; return baseUrl + "/" + 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 + API_ENDPOINT + "accounts/" + id; return baseUrl + "/accounts/" + id;
} }
} }

View File

@ -10,7 +10,8 @@ public class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {
private static final PeertubeStreamLinkHandlerFactory instance = new PeertubeStreamLinkHandlerFactory(); private static final PeertubeStreamLinkHandlerFactory instance = new PeertubeStreamLinkHandlerFactory();
private static final String ID_PATTERN = "/videos/(watch/|embed/)?([^/?&#]*)"; private static final String ID_PATTERN = "/videos/(watch/|embed/)?([^/?&#]*)";
private static final String VIDEO_ENDPOINT = "/api/v1/videos/"; public static final String VIDEO_API_ENDPOINT = "/api/v1/videos/";
private static final String VIDEO_PATH = "/videos/watch/";
private PeertubeStreamLinkHandlerFactory() { private PeertubeStreamLinkHandlerFactory() {
} }
@ -27,7 +28,7 @@ public class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {
@Override @Override
public String getUrl(String id, String baseUrl) { public String getUrl(String id, String baseUrl) {
return baseUrl + VIDEO_ENDPOINT + id; return baseUrl + VIDEO_PATH + id;
} }
@Override @Override

View File

@ -28,7 +28,7 @@ public class PeertubeAccountExtractorTest {
// setting instance might break test when running in parallel // setting instance might break test when running in parallel
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
extractor = (PeertubeAccountExtractor) PeerTube extractor = (PeertubeAccountExtractor) PeerTube
.getChannelExtractor("https://peertube.mastodon.host/api/v1/accounts/kde"); .getChannelExtractor("https://peertube.mastodon.host/accounts/kde");
extractor.fetchPage(); extractor.fetchPage();
} }
@ -53,7 +53,7 @@ public class PeertubeAccountExtractorTest {
@Test @Test
public void testUrl() throws ParsingException { public void testUrl() throws ParsingException {
assertEquals("https://peertube.mastodon.host/api/v1/accounts/kde", extractor.getUrl()); assertEquals("https://peertube.mastodon.host/accounts/kde", extractor.getUrl());
} }
@Test @Test
@ -115,7 +115,7 @@ public class PeertubeAccountExtractorTest {
// setting instance might break test when running in parallel // setting instance might break test when running in parallel
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
extractor = (PeertubeAccountExtractor) PeerTube extractor = (PeertubeAccountExtractor) PeerTube
.getChannelExtractor("https://peertube.mastodon.host/accounts/booteille"); .getChannelExtractor("https://peertube.mastodon.host/api/v1/accounts/booteille");
extractor.fetchPage(); extractor.fetchPage();
} }
@ -150,12 +150,12 @@ public class PeertubeAccountExtractorTest {
@Test @Test
public void testUrl() throws ParsingException { public void testUrl() throws ParsingException {
assertEquals("https://peertube.mastodon.host/api/v1/accounts/booteille", extractor.getUrl()); assertEquals("https://peertube.mastodon.host/accounts/booteille", extractor.getUrl());
} }
@Test @Test
public void testOriginalUrl() throws ParsingException { public void testOriginalUrl() throws ParsingException {
assertEquals("https://peertube.mastodon.host/accounts/booteille", extractor.getOriginalUrl()); assertEquals("https://peertube.mastodon.host/api/v1/accounts/booteille", extractor.getOriginalUrl());
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////

View File

@ -53,7 +53,7 @@ public class PeertubeChannelExtractorTest {
@Test @Test
public void testUrl() throws ParsingException { public void testUrl() throws ParsingException {
assertEquals("https://peertube.mastodon.host/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa", extractor.getUrl()); assertEquals("https://peertube.mastodon.host/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa", extractor.getUrl());
} }
@Test @Test
@ -130,7 +130,7 @@ public class PeertubeChannelExtractorTest {
// setting instance might break test when running in parallel // setting instance might break test when running in parallel
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
extractor = (PeertubeChannelExtractor) PeerTube extractor = (PeertubeChannelExtractor) PeerTube
.getChannelExtractor("https://peertube.mastodon.host/video-channels/35080089-79b6-45fc-96ac-37e4d46a4457"); .getChannelExtractor("https://peertube.mastodon.host/api/v1/video-channels/35080089-79b6-45fc-96ac-37e4d46a4457");
extractor.fetchPage(); extractor.fetchPage();
} }
@ -165,12 +165,12 @@ public class PeertubeChannelExtractorTest {
@Test @Test
public void testUrl() throws ParsingException { public void testUrl() throws ParsingException {
assertEquals("https://peertube.mastodon.host/api/v1/video-channels/35080089-79b6-45fc-96ac-37e4d46a4457", extractor.getUrl()); assertEquals("https://peertube.mastodon.host/video-channels/35080089-79b6-45fc-96ac-37e4d46a4457", extractor.getUrl());
} }
@Test @Test
public void testOriginalUrl() throws ParsingException { public void testOriginalUrl() throws ParsingException {
assertEquals("https://peertube.mastodon.host/video-channels/35080089-79b6-45fc-96ac-37e4d46a4457", extractor.getOriginalUrl()); assertEquals("https://peertube.mastodon.host/api/v1/video-channels/35080089-79b6-45fc-96ac-37e4d46a4457", extractor.getOriginalUrl());
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////

View File

@ -53,7 +53,7 @@ public class PeertubeStreamExtractorTest {
@Override public StreamType expectedStreamType() { return StreamType.VIDEO_STREAM; } @Override public StreamType expectedStreamType() { return StreamType.VIDEO_STREAM; }
@Override public String expectedUploaderName() { return "Framasoft"; } @Override public String expectedUploaderName() { return "Framasoft"; }
@Override public String expectedUploaderUrl() { return "https://framatube.org/accounts/framasoft"; } @Override public String expectedUploaderUrl() { return "https://framatube.org/accounts/framasoft@framatube.org"; }
@Override public List<String> expectedDescriptionContains() { // CRLF line ending @Override public List<String> expectedDescriptionContains() { // CRLF line ending
return Arrays.asList("**[Want to help to translate this video?](https://weblate.framasoft.org/projects/what-is-peertube-video/)**\r\n" return Arrays.asList("**[Want to help to translate this video?](https://weblate.framasoft.org/projects/what-is-peertube-video/)**\r\n"
+ "\r\n" + "\r\n"
@ -108,7 +108,7 @@ public class PeertubeStreamExtractorTest {
@Override public StreamType expectedStreamType() { return StreamType.VIDEO_STREAM; } @Override public StreamType expectedStreamType() { return StreamType.VIDEO_STREAM; }
@Override public String expectedUploaderName() { return "Tomas Berezovskiy"; } @Override public String expectedUploaderName() { return "Tomas Berezovskiy"; }
@Override public String expectedUploaderUrl() { return "https://peertube.iriseden.eu/accounts/tomas_berezovskiy"; } @Override public String expectedUploaderUrl() { return "https://peertube.co.uk/accounts/tomas_berezovskiy@peertube.iriseden.eu"; }
@Override public List<String> expectedDescriptionContains() { // LF line ending @Override public List<String> expectedDescriptionContains() { // LF line ending
return Arrays.asList("https://en.informnapalm.org/dpr-combatant-describes-orders-given-russian-officers/ " return Arrays.asList("https://en.informnapalm.org/dpr-combatant-describes-orders-given-russian-officers/ "
+ " The InformNapalm team received another video of a separatist prisoner of war telling about his " + " The InformNapalm team received another video of a separatist prisoner of war telling about his "