Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PeerTube short links support #743

Merged
merged 5 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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() {
Expand All @@ -19,7 +19,7 @@ public static PeertubeChannelLinkHandlerFactory getInstance() {

@Override
public String getId(String url) throws ParsingException {
return Parser.matchGroup(ID_PATTERN, url, 0);
return fixId(Parser.matchGroup(ID_PATTERN, url, 0));
}

@Override
Expand All @@ -31,7 +31,7 @@ public String getUrl(String id, List<String> contentFilters, String searchFilter
public String getUrl(String id, List<String> 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;
Expand All @@ -40,6 +40,28 @@ public String getUrl(String id, List<String> contentFilter, String sortFilter, S

@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
*
* <p>
* a/:accountName and c/:channelName ids are supported
* by the PeerTube web client (>= v3.3.0)
* but not by the API.
* </p>
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
public class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory {

private static final PeertubeCommentsLinkHandlerFactory instance = new PeertubeCommentsLinkHandlerFactory();
private static final String ID_PATTERN = "/videos/(watch/)?([^/?&#]*)";
private static final String COMMENTS_ENDPOINT = "/api/v1/videos/%s/comment-threads";

public static PeertubeCommentsLinkHandlerFactory getInstance() {
Expand All @@ -20,12 +19,12 @@ public static PeertubeCommentsLinkHandlerFactory getInstance() {

@Override
public String getId(String url) throws ParsingException, IllegalArgumentException {
return Parser.matchGroup(ID_PATTERN, url, 2);
return PeertubeStreamLinkHandlerFactory.getInstance().getId(url); // the same id is needed
}

@Override
public boolean onAcceptUrl(final String url) throws FoundAdException {
return url.contains("/videos/");
return url.contains("/videos/") || url.contains("/w/");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {

private static final PeertubePlaylistLinkHandlerFactory instance = new PeertubePlaylistLinkHandlerFactory();
private static final String ID_PATTERN = "/videos/watch/playlist/([^/?&#]*)";
private static final String ID_PATTERN = "(/videos/watch/playlist/|/w/p/)([^/?&#]*)";

public static PeertubePlaylistLinkHandlerFactory getInstance() {
return instance;
Expand All @@ -30,7 +30,7 @@ public String getUrl(String id, List<String> contentFilters, String sortFilter,

@Override
public String getId(String url) throws ParsingException {
return Parser.matchGroup1(ID_PATTERN, url);
return Parser.matchGroup(ID_PATTERN, url, 2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
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/)?))(?!p/)([^/?&#]*)";
// we exclude p/ because /w/p/ is playlist, not video
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() {
Expand All @@ -32,7 +37,7 @@ public String getUrl(String id, String baseUrl) {

@Override
public String getId(String url) throws ParsingException, IllegalArgumentException {
return Parser.matchGroup(ID_PATTERN, url, 2);
return Parser.matchGroup(ID_PATTERN, url, 4);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]"));
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/[email protected]/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/accounts/[email protected]"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/a/[email protected]"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/accounts/[email protected]/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/video-channels/[email protected]/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/c/[email protected]/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa"));
}

@Test
public void getId() throws ParsingException {
assertEquals("accounts/[email protected]",
linkHandler.fromUrl("https://peertube.mastodon.host/accounts/[email protected]").getId());
linkHandler.fromUrl("https://peertube.stream/accounts/[email protected]").getId());
assertEquals("accounts/[email protected]",
linkHandler.fromUrl("https://peertube.mastodon.host/accounts/[email protected]/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/[email protected]").getId());
assertEquals("accounts/[email protected]",
linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/accounts/[email protected]").getId());
linkHandler.fromUrl("https://peertube.stream/accounts/[email protected]/videos").getId());
assertEquals("accounts/[email protected]",
linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/accounts/[email protected]/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/[email protected]/videos").getId());
assertEquals("accounts/[email protected]",
linkHandler.fromUrl("https://peertube.stream/api/v1/accounts/[email protected]").getId());
assertEquals("accounts/[email protected]",
linkHandler.fromUrl("https://peertube.stream/api/v1/accounts/[email protected]/videos").getId());

assertEquals("video-channels/[email protected]",
linkHandler.fromUrl("https://peertube.stream/video-channels/[email protected]/videos").getId());
assertEquals("video-channels/[email protected]",
linkHandler.fromUrl("https://peertube.stream/c/[email protected]/videos").getId());
assertEquals("video-channels/[email protected]",
linkHandler.fromUrl("https://peertube.stream/c/[email protected]/video-playlists").getId());
assertEquals("video-channels/[email protected]",
linkHandler.fromUrl("https://peertube.stream/api/v1/video-channels/[email protected]").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/[email protected]",
assertEquals("https://peertube.stream/video-channels/[email protected]",
linkHandler.fromId("video-channels/[email protected]").getUrl());
assertEquals("https://peertube.stream/accounts/[email protected]",
linkHandler.fromId("accounts/[email protected]").getUrl());
assertEquals("https://peertube.mastodon.host/accounts/[email protected]",
assertEquals("https://peertube.stream/accounts/[email protected]",
linkHandler.fromId("[email protected]").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/[email protected]",
linkHandler.fromUrl("https://peertube.stream/api/v1/video-channels/[email protected]").getUrl());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,32 @@ public static void setUp() {

@Test
public void acceptUrlTest() throws ParsingException {
assertTrue(linkHandler.acceptUrl("https://peertube.mastodon.host/api/v1/videos/19319/comment-threads?start=0&count=10&sort=-createdAt"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/kkGMgK9ZtnKfYAgnEtQxbv"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/kkGMgK9ZtnKfYAgnEtQxbv"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/api/v1/videos/kkGMgK9ZtnKfYAgnEtQxbv/comment-threads?start=0&count=10&sort=-createdAt"));
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"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d/comment-threads?start=0&count=10&sort=-createdAt"));
}

@Test
public void getIdFromUrl() throws ParsingException {
assertEquals("19319", linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/videos/19319/comment-threads").getId());
assertEquals("19319", linkHandler.fromUrl("https://peertube.mastodon.host/api/v1/videos/19319/comment-threads?start=0&count=10&sort=-createdAt").getId());
assertEquals("kkGMgK9ZtnKfYAgnEtQxbv",
linkHandler.fromUrl("https://framatube.org/w/kkGMgK9ZtnKfYAgnEtQxbv").getId());
assertEquals("kkGMgK9ZtnKfYAgnEtQxbv",
linkHandler.fromUrl("https://framatube.org/videos/watch/kkGMgK9ZtnKfYAgnEtQxbv").getId());
assertEquals("kkGMgK9ZtnKfYAgnEtQxbv",
linkHandler.fromUrl("https://framatube.org/api/v1/videos/kkGMgK9ZtnKfYAgnEtQxbv/comment-threads").getId());
assertEquals("kkGMgK9ZtnKfYAgnEtQxbv",
linkHandler.fromUrl("https://framatube.org/api/v1/videos/kkGMgK9ZtnKfYAgnEtQxbv/comment-threads?start=0&count=10&sort=-createdAt").getId());

assertEquals("9c9de5e8-0a1e-484a-b099-e80766180a6d",
linkHandler.fromUrl("https://framatube.org/w/9c9de5e8-0a1e-484a-b099-e80766180a6d").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/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d/comment-threads").getId());
assertEquals("9c9de5e8-0a1e-484a-b099-e80766180a6d",
linkHandler.fromUrl("https://framatube.org/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d/comment-threads?start=0&count=10&sort=-createdAt").getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,30 @@ public static void setUp() {
@Test
public void acceptUrlTest() throws ParsingException {
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/d8ca79f9-e4c7-4269-8183-d78ed269c909"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/d8ca79f9-e4c7-4269-8183-d78ed269c909"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/d8ca79f9-e4c7-4269-8183-d78ed269c909/videos"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/dacdc4ef-5160-4846-9b70-a655880da667"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/dacdc4ef-5160-4846-9b70-a655880da667"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));
}

@Test
public void getIdFromUrl() throws ParsingException {
assertEquals("d8ca79f9-e4c7-4269-8183-d78ed269c909", linkHandler.getId("https://framatube.org/videos/watch/playlist/d8ca79f9-e4c7-4269-8183-d78ed269c909"));
assertEquals("d8ca79f9-e4c7-4269-8183-d78ed269c909", linkHandler.getId("https://framatube.org/w/p/d8ca79f9-e4c7-4269-8183-d78ed269c909"));
assertEquals("dacdc4ef-5160-4846-9b70-a655880da667", linkHandler.getId("https://framatube.org/videos/watch/playlist/dacdc4ef-5160-4846-9b70-a655880da667"));
assertEquals("dacdc4ef-5160-4846-9b70-a655880da667", linkHandler.getId("https://framatube.org/w/p/dacdc4ef-5160-4846-9b70-a655880da667"));
assertEquals("bfc145f5-1be7-48a6-9b9e-4f1967199dad", linkHandler.getId("https://framatube.org/videos/watch/playlist/bfc145f5-1be7-48a6-9b9e-4f1967199dad"));
assertEquals("bfc145f5-1be7-48a6-9b9e-4f1967199dad", linkHandler.getId("https://framatube.org/w/p/bfc145f5-1be7-48a6-9b9e-4f1967199dad"));
assertEquals("96b0ee2b-a5a7-4794-8769-58d8ccb79ab7", linkHandler.getId("https://framatube.org/videos/watch/playlist/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));
assertEquals("96b0ee2b-a5a7-4794-8769-58d8ccb79ab7", linkHandler.getId("https://framatube.org/w/p/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));
}

@Test
public void getUrl() throws ParsingException {
System.out.println(linkHandler.fromUrl("https://framatube.org/videos/watch/playlist/d8ca79f9-e4c7-4269-8183-d78ed269c909").getUrl());;
System.out.println(linkHandler.fromUrl("https://framatube.org/w/p/d8ca79f9-e4c7-4269-8183-d78ed269c909").getUrl());;
System.out.println(linkHandler.fromUrl("https://framatube.org/w/p/sLFbqXsw7sPR3AfvqQSBZB").getUrl());;
}
}
Loading