From e0b8e142fcc83bbb1f1d71dc6b4565f4524b5e33 Mon Sep 17 00:00:00 2001 From: bopol Date: Wed, 3 Nov 2021 13:26:00 +0100 Subject: [PATCH] [PeerTube] Support /a/ and /c/ short links --- .../PeertubeChannelLinkHandlerFactory.java | 30 ++++++++++-- ...PeertubeChannelLinkHandlerFactoryTest.java | 49 ++++++++++++------- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeChannelLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeChannelLinkHandlerFactory.java index 9134c14e..950514b0 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeChannelLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeChannelLinkHandlerFactory.java @@ -10,7 +10,7 @@ import java.util.List; public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { private static final PeertubeChannelLinkHandlerFactory instance = new PeertubeChannelLinkHandlerFactory(); - private static final String ID_PATTERN = "(accounts|video-channels)/([^/?&#]*)"; + private static final String ID_PATTERN = "((accounts|a)|(video-channels|c))/([^/?&#]*)"; public static final String API_ENDPOINT = "/api/v1/"; public static PeertubeChannelLinkHandlerFactory getInstance() { @@ -19,7 +19,7 @@ public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { @Override public String getId(String url) throws ParsingException { - return Parser.matchGroup(ID_PATTERN, url, 0); + return fixId(Parser.matchGroup(ID_PATTERN, url, 0)); } @Override @@ -31,7 +31,7 @@ public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { public String getUrl(String id, List contentFilter, String sortFilter, String baseUrl) throws ParsingException { if (id.matches(ID_PATTERN)) { - return baseUrl + "/" + id; + return baseUrl + "/" + fixId(id); } else { // This is needed for compatibility with older versions were we didn't support video channels yet return baseUrl + "/accounts/" + id; @@ -40,6 +40,28 @@ public class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { @Override public boolean onAcceptUrl(String url) { - return url.contains("/accounts/") || url.contains("/video-channels/"); + return url.contains("/accounts/") || url.contains("/a/") + || url.contains("/video-channels/") || url.contains("/c/"); + } + + /** + * Fix id + * + *

+ * a/:accountName and c/:channelName ids are supported + * by the PeerTube web client (>= v3.3.0) + * but not by the API. + *

+ * + * @param id the id to fix + * @return the fixed id + */ + private String fixId(String id) { + if (id.startsWith("a/")) { + id = "accounts" + id.substring(1); + } else if (id.startsWith("c/")) { + id = "video-channels" + id.substring(1); + } + return id; } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelLinkHandlerFactoryTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelLinkHandlerFactoryTest.java index 4668dfdb..8d72429a 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelLinkHandlerFactoryTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelLinkHandlerFactoryTest.java @@ -20,44 +20,55 @@ public class PeertubeChannelLinkHandlerFactoryTest { @BeforeClass public static void setUp() { - PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); + PeerTube.setInstance(new PeertubeInstance("https://peertube.stream", "PeerTube on peertube.stream")); linkHandler = PeertubeChannelLinkHandlerFactory.getInstance(); NewPipe.init(DownloaderTestImpl.getInstance()); } @Test public void acceptUrlTest() throws ParsingException { - assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/accounts/kranti@videos.squat.net")); - assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa/videos")); - assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/api/v1/accounts/kranti@videos.squat.net/videos")); - assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa")); + assertTrue(linkHandler.acceptUrl("https://peertube.stream/accounts/kranti@videos.squat.net")); + assertTrue(linkHandler.acceptUrl("https://peertube.stream/a/kranti@videos.squat.net")); + assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/accounts/kranti@videos.squat.net/videos")); + assertTrue(linkHandler.acceptUrl("https://peertube.stream/video-channels/kranti_channel@videos.squat.net/videos")); + assertTrue(linkHandler.acceptUrl("https://peertube.stream/c/kranti_channel@videos.squat.net/videos")); + assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa")); } @Test public void getId() throws ParsingException { assertEquals("accounts/kranti@videos.squat.net", - linkHandler.fromUrl("https://peertube.mastodon.host/accounts/kranti@videos.squat.net").getId()); + linkHandler.fromUrl("https://peertube.stream/accounts/kranti@videos.squat.net").getId()); assertEquals("accounts/kranti@videos.squat.net", - linkHandler.fromUrl("https://peertube.mastodon.host/accounts/kranti@videos.squat.net/videos").getId()); - assertEquals("video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa", - linkHandler.fromUrl("https://peertube.mastodon.host/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa/videos").getId()); + linkHandler.fromUrl("https://peertube.stream/a/kranti@videos.squat.net").getId()); assertEquals("accounts/kranti@videos.squat.net", - linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/accounts/kranti@videos.squat.net").getId()); + linkHandler.fromUrl("https://peertube.stream/accounts/kranti@videos.squat.net/videos").getId()); assertEquals("accounts/kranti@videos.squat.net", - linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/accounts/kranti@videos.squat.net/videos").getId()); - assertEquals("video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa", - linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa").getId()); + linkHandler.fromUrl("https://peertube.stream/a/kranti@videos.squat.net/videos").getId()); + assertEquals("accounts/kranti@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/api/v1/accounts/kranti@videos.squat.net").getId()); + assertEquals("accounts/kranti@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/api/v1/accounts/kranti@videos.squat.net/videos").getId()); + + assertEquals("video-channels/kranti_channel@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/video-channels/kranti_channel@videos.squat.net/videos").getId()); + assertEquals("video-channels/kranti_channel@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/c/kranti_channel@videos.squat.net/videos").getId()); + assertEquals("video-channels/kranti_channel@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/c/kranti_channel@videos.squat.net/video-playlists").getId()); + assertEquals("video-channels/kranti_channel@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/api/v1/video-channels/kranti_channel@videos.squat.net").getId()); } @Test public void getUrl() throws ParsingException { - assertEquals("https://peertube.mastodon.host/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa", - linkHandler.fromId("video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa").getUrl()); - assertEquals("https://peertube.mastodon.host/accounts/kranti@videos.squat.net", + assertEquals("https://peertube.stream/video-channels/kranti_channel@videos.squat.net", + linkHandler.fromId("video-channels/kranti_channel@videos.squat.net").getUrl()); + assertEquals("https://peertube.stream/accounts/kranti@videos.squat.net", linkHandler.fromId("accounts/kranti@videos.squat.net").getUrl()); - assertEquals("https://peertube.mastodon.host/accounts/kranti@videos.squat.net", + assertEquals("https://peertube.stream/accounts/kranti@videos.squat.net", linkHandler.fromId("kranti@videos.squat.net").getUrl()); - assertEquals("https://peertube.mastodon.host/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa", - linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa").getUrl()); + assertEquals("https://peertube.stream/video-channels/kranti_channel@videos.squat.net", + linkHandler.fromUrl("https://peertube.stream/api/v1/video-channels/kranti_channel@videos.squat.net").getUrl()); } }