Skip to content

Commit

Permalink
support /watch/ and /v/ folders
Browse files Browse the repository at this point in the history
  • Loading branch information
B0pol committed Feb 10, 2021
1 parent fcdb50b commit 086ef29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -38,6 +40,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {

private static final Pattern YOUTUBE_VIDEO_ID_REGEX_PATTERN = Pattern.compile("^([a-zA-Z0-9_-]{11})");
private static final YoutubeStreamLinkHandlerFactory instance = new YoutubeStreamLinkHandlerFactory();
private static final List<String> folders = Arrays.asList("embed/", "shorts/", "watch/", "v/");

private YoutubeStreamLinkHandlerFactory() {
}
Expand Down Expand Up @@ -150,11 +153,8 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
return assertIsId(viewQueryValue);
}

if (path.startsWith("embed/") || path.startsWith("shorts/")) {
String id = path.split("/")[1];

return assertIsId(id);
}
String maybeId = getIdFromFoldersInPath(path);
if (maybeId != null) return maybeId;

String viewQueryValue = Utils.getQueryValue(url, "v");
return assertIsId(viewQueryValue);
Expand All @@ -169,20 +169,7 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
return assertIsId(path);
}

case "HOOKTUBE.COM": {
if (path.startsWith("v/")) {
String id = path.substring("v/".length());

return assertIsId(id);
}
if (path.startsWith("watch/")) {
String id = path.substring("watch/".length());

return assertIsId(id);
}
// there is no break-statement here on purpose so the next code-block gets also run for hooktube
}

case "HOOKTUBE.COM":
case "INVIDIO.US":
case "DEV.INVIDIO.US":
case "WWW.INVIDIO.US":
Expand All @@ -208,11 +195,8 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
return assertIsId(viewQueryValue);
}
}
if (path.startsWith("embed/")) {
String id = path.substring("embed/".length());

return assertIsId(id);
}
String maybeId = getIdFromFoldersInPath(path);
if (maybeId != null) return maybeId;

String viewQueryValue = Utils.getQueryValue(url, "v");
if (viewQueryValue != null) {
Expand All @@ -237,4 +221,14 @@ public boolean onAcceptUrl(final String url) throws FoundAdException {
return false;
}
}

private String getIdFromFoldersInPath(String path) throws ParsingException {
for (final String folder : folders) {
if (path.startsWith(folder)) {
String id = path.split("/")[1];
return assertIsId(id);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void getIdfromYt() throws Exception {
assertEquals("-cdveCh1kQk", linkHandler.fromUrl("HTTPS://youtu.be/-cdveCh1kQk)").getId());
assertEquals("IOS2fqxwYbA", linkHandler.fromUrl("https://www.youtube.com/shorts/IOS2fqxwYbAhi").getId());
assertEquals("IOS2fqxwYbA", linkHandler.fromUrl("http://www.youtube.com/shorts/IOS2fqxwYbA").getId());
assertEquals("IOS2fqxwYbA", linkHandler.fromUrl("http://www.youtube.com/v/IOS2fqxwYbA").getId());
assertEquals("IOS2fqxwYbA", linkHandler.fromUrl("http://www.youtube.com/watch/IOS2fqxwYbA").getId());
}

@Test
Expand All @@ -113,6 +115,8 @@ public void testAcceptYtUrl() throws ParsingException {
assertTrue(linkHandler.acceptUrl("vnd.youtube.launch:jZViOEv90dI"));
assertTrue(linkHandler.acceptUrl("https://music.youtube.com/watch?v=O0EDx9WAelc"));
assertTrue(linkHandler.acceptUrl("https://www.youtube.com/shorts/IOS2fqxwYbA"));
assertTrue(linkHandler.acceptUrl("https://www.youtube.com/v/IOS2fqxwYbA"));
assertTrue(linkHandler.acceptUrl("https://www.youtube.com/watch/IOS2fqxwYbA"));
}

@Test
Expand All @@ -138,22 +142,26 @@ public void testGetHookIdfromUrl() throws ParsingException {
}

@Test
public void testAcceptInvidioUrl() throws ParsingException {
public void testAcceptInvidiousUrl() throws ParsingException {
assertTrue(linkHandler.acceptUrl("https://invidio.us/watch?v=TglNG-yjabU"));
assertTrue(linkHandler.acceptUrl("http://www.invidio.us/watch?v=TglNG-yjabU"));
assertTrue(linkHandler.acceptUrl("http://invidio.us/watch?v=TglNG-yjabU"));
assertTrue(linkHandler.acceptUrl("invidio.us/watch?v=3msbfr6pBNE"));
assertTrue(linkHandler.acceptUrl("https://invidio.us/watch?v=ocH3oSnZG3c&test=PLS2VU1j4vzuZwooPjV26XM9UEBY2CPNn2"));
assertTrue(linkHandler.acceptUrl("invidio.us/embed/3msbfr6pBNE"));
assertTrue(linkHandler.acceptUrl("invidio.us/watch/3msbfr6pBNE"));
assertTrue(linkHandler.acceptUrl("invidio.us/v/3msbfr6pBNE"));
}

@Test
public void testGetInvidioIdfromUrl() throws ParsingException {
public void testGetInvidiousIdfromUrl() throws ParsingException {
assertEquals("TglNG-yjabU", linkHandler.fromUrl("https://invidio.us/watch?v=TglNG-yjabU").getId());
assertEquals("TglNG-yjabU", linkHandler.fromUrl("http://www.invidio.us/watch?v=TglNG-yjabU").getId());
assertEquals("TglNG-yjabU", linkHandler.fromUrl("http://invidio.us/watch?v=TglNG-yjabU").getId());
assertEquals("3msbfr6pBNE", linkHandler.fromUrl("invidio.us/watch?v=3msbfr6pBNE").getId());
assertEquals("ocH3oSnZG3c", linkHandler.fromUrl("https://invidio.us/watch?v=ocH3oSnZG3c&test=PLS2VU1j4vzuZwooPjV26XM9UEBY2CPNn2").getId());
assertEquals("3msbfr6pBNE", linkHandler.fromUrl("invidio.us/embed/3msbfr6pBNE").getId());
assertEquals("3msbfr6pBNE", linkHandler.fromUrl("invidio.us/v/3msbfr6pBNE").getId());
assertEquals("3msbfr6pBNE", linkHandler.fromUrl("invidio.us/watch/3msbfr6pBNE").getId());
}
}

0 comments on commit 086ef29

Please sign in to comment.