From 599a91c88cab0af2fea5b17fa61f972a636230f0 Mon Sep 17 00:00:00 2001 From: bopol Date: Wed, 3 Nov 2021 11:51:58 +0100 Subject: [PATCH] [PeerTube] Support /w/ short video links --- .../PeertubeStreamLinkHandlerFactory.java | 8 +++-- .../PeertubeStreamLinkHandlerFactoryTest.java | 34 +++++++++++++------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeStreamLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeStreamLinkHandlerFactory.java index 16b3fed3..cfd09048 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeStreamLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/linkHandler/PeertubeStreamLinkHandlerFactory.java @@ -9,8 +9,12 @@ import org.schabi.newpipe.extractor.utils.Parser; public class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory { private static final PeertubeStreamLinkHandlerFactory instance = new PeertubeStreamLinkHandlerFactory(); - private static final String ID_PATTERN = "/videos/(watch/|embed/)?([^/?&#]*)"; + private static final String ID_PATTERN = "(/w/|(/videos/(watch/|embed/)?))([^/?&#]*)"; public static final String VIDEO_API_ENDPOINT = "/api/v1/videos/"; + + // From PeerTube 3.3.0, the default path is /w/. + // We still use /videos/watch/ for compatibility reasons: + // /videos/watch/ is still accepted by >=3.3.0 but /w/ isn't by <3.3.0 private static final String VIDEO_PATH = "/videos/watch/"; private PeertubeStreamLinkHandlerFactory() { @@ -32,7 +36,7 @@ public class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory { @Override public String getId(String url) throws ParsingException, IllegalArgumentException { - return Parser.matchGroup(ID_PATTERN, url, 2); + return Parser.matchGroup(ID_PATTERN, url, 4); } @Override diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamLinkHandlerFactoryTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamLinkHandlerFactoryTest.java index 853893ce..aef1280d 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamLinkHandlerFactoryTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamLinkHandlerFactoryTest.java @@ -19,7 +19,7 @@ public class PeertubeStreamLinkHandlerFactoryTest { @BeforeClass public static void setUp() { - PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); + PeerTube.setInstance(new PeertubeInstance("https://framatube.org", "Framatube")); linkHandler = PeertubeStreamLinkHandlerFactory.getInstance(); NewPipe.init(DownloaderTestImpl.getInstance()); } @@ -30,22 +30,31 @@ public class PeertubeStreamLinkHandlerFactoryTest { linkHandler.fromUrl("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60").getId()); assertEquals("986aac60-1263-4f73-9ce5-36b18225cb60", linkHandler.fromUrl("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60?fsdafs=fsafa").getId()); - assertEquals("9c9de5e8-0a1e-484a-b099-e80766180a6d", - linkHandler.fromUrl("https://framatube.org/videos/embed/9c9de5e8-0a1e-484a-b099-e80766180a6d").getId()); assertEquals("986aac60-1263-4f73-9ce5-36b18225cb60", linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60").getId()); assertEquals("986aac60-1263-4f73-9ce5-36b18225cb60", linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60?fsdafs=fsafa").getId()); + + assertEquals("9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d").getId()); + assertEquals("9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/videos/embed/9c9de5e8-0a1e-484a-b099-e80766180a6d").getId()); + assertEquals("9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/w/9c9de5e8-0a1e-484a-b099-e80766180a6d").getId()); } @Test public void getUrl() throws Exception { - assertEquals("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60", - linkHandler.fromId("986aac60-1263-4f73-9ce5-36b18225cb60").getUrl()); - assertEquals("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60", - linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60").getUrl()); - assertEquals("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60", - linkHandler.fromUrl("https://peertube.mastodon.host/videos/embed/986aac60-1263-4f73-9ce5-36b18225cb60").getUrl()); + assertEquals("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromId("9c9de5e8-0a1e-484a-b099-e80766180a6d").getUrl()); + assertEquals("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d").getUrl()); + assertEquals("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/videos/embed/9c9de5e8-0a1e-484a-b099-e80766180a6d").getUrl()); + assertEquals("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d").getUrl()); + assertEquals("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d", + linkHandler.fromUrl("https://framatube.org/w/9c9de5e8-0a1e-484a-b099-e80766180a6d").getUrl()); } @@ -53,7 +62,10 @@ public class PeertubeStreamLinkHandlerFactoryTest { public void testAcceptUrl() throws ParsingException { assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60")); assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60?fsdafs=fsafa")); + + assertTrue(linkHandler.acceptUrl("https://framatube.org/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d")); assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/embed/9c9de5e8-0a1e-484a-b099-e80766180a6d")); - assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/api/v1/videos/watch/986aac60-1263-4f73-9ce5-36b18225cb60?fsdafs=fsafa")); + assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d")); + assertTrue(linkHandler.acceptUrl("https://framatube.org/w/9c9de5e8-0a1e-484a-b099-e80766180a6d")); } -} \ No newline at end of file +}