From 055cc418348606eb26ed0b9f20681d5f001b96e3 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Mon, 12 Feb 2018 17:29:36 +0100 Subject: [PATCH] add support for hooktube links fix some links --- .../youtube/YoutubeChannelUrlIdHandler.java | 2 +- .../youtube/YoutubeStreamUrlIdHandler.java | 18 +++++- .../YoutubeChannelUrlIdHandlerTest.java | 55 +++++++++++++++++++ .../YoutubeStreamUrlIdHandlerTest.java | 36 ++++++++++-- 4 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandlerTest.java diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandler.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandler.java index fcc455a64..bc9ce6811 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandler.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandler.java @@ -50,7 +50,7 @@ public class YoutubeChannelUrlIdHandler implements UrlIdHandler { @Override public boolean acceptUrl(String url) { - return (url.contains("youtube") || url.contains("youtu.be")) + return (url.contains("youtube") || url.contains("youtu.be") || url.contains("hooktube.com")) && (url.contains("/user/") || url.contains("/channel/")); } } diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandler.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandler.java index 65c737687..a08407aef 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandler.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandler.java @@ -88,6 +88,19 @@ public class YoutubeStreamUrlIdHandler implements UrlIdHandler { } else { id = Parser.matchGroup1("[Yy][Oo][Uu][Tt][Uu]\\.[Bb][Ee]/" + ID_PATTERN, url); } + } else if(lowercaseUrl.contains("hooktube")) { + if(lowercaseUrl.contains("&v=") + || lowercaseUrl.contains("?v=")) { + id = Parser.matchGroup1("[?&]v=" + ID_PATTERN, url); + } else if (url.contains("/embed/")) { + id = Parser.matchGroup1("embed/" + ID_PATTERN, url); + } else if (url.contains("/v/")) { + id = Parser.matchGroup1("v/" + ID_PATTERN, url); + } else if (url.contains("/watch/")) { + id = Parser.matchGroup1("watch/" + ID_PATTERN, url); + } else { + throw new ParsingException("Error no suitable url: " + url); + } } else { throw new ParsingException("Error no suitable url: " + url); } @@ -161,8 +174,9 @@ public class YoutubeStreamUrlIdHandler implements UrlIdHandler { @Override public boolean acceptUrl(String url) { String lowercaseUrl = url.toLowerCase(); - if (lowercaseUrl.contains("youtube") || - lowercaseUrl.contains("youtu.be")) { + if (lowercaseUrl.contains("youtube") + || lowercaseUrl.contains("youtu.be") + || lowercaseUrl.contains("hooktube")) { // bad programming I know try { getId(url); diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandlerTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandlerTest.java new file mode 100644 index 000000000..57bccdf90 --- /dev/null +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelUrlIdHandlerTest.java @@ -0,0 +1,55 @@ +package org.schabi.newpipe.extractor.services.youtube; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.schabi.newpipe.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test for {@link YoutubeChannelUrlIdHandler} + */ +public class YoutubeChannelUrlIdHandlerTest { + + private static YoutubeChannelUrlIdHandler urlIdHandler; + + @BeforeClass + public static void setUp() { + urlIdHandler = YoutubeChannelUrlIdHandler.getInstance(); + NewPipe.init(Downloader.getInstance()); + } + + @Test + public void acceptrUrlTest() { + assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/user/Gronkh")); + assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/user/Netzkino/videos")); + + assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA")); + assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1")); + + assertTrue(urlIdHandler.acceptUrl("https://hooktube.com/user/Gronkh")); + assertTrue(urlIdHandler.acceptUrl("https://hooktube.com/user/Netzkino/videos")); + + assertTrue(urlIdHandler.acceptUrl("https://hooktube.com/channel/UClq42foiSgl7sSpLupnugGA")); + assertTrue(urlIdHandler.acceptUrl("https://hooktube.com/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1")); + } + + @Test + public void getIdFromUrl() throws ParsingException { + assertEquals("user/Gronkh", urlIdHandler.getId("https://www.youtube.com/user/Gronkh")); + assertEquals("user/Netzkino", urlIdHandler.getId("https://www.youtube.com/user/Netzkino/videos")); + + assertEquals("channel/UClq42foiSgl7sSpLupnugGA", urlIdHandler.getId("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA")); + assertEquals("channel/UClq42foiSgl7sSpLupnugGA", urlIdHandler.getId("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1")); + + + assertEquals("user/Gronkh", urlIdHandler.getId("https://hooktube.com/user/Gronkh")); + assertEquals("user/Netzkino", urlIdHandler.getId("https://hooktube.com/user/Netzkino/videos")); + + assertEquals("channel/UClq42foiSgl7sSpLupnugGA", urlIdHandler.getId("https://hooktube.com/channel/UClq42foiSgl7sSpLupnugGA")); + assertEquals("channel/UClq42foiSgl7sSpLupnugGA", urlIdHandler.getId("https://hooktube.com/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1")); + } +} diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandlerTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandlerTest.java index 286762e3c..2711c7aed 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandlerTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamUrlIdHandlerTest.java @@ -20,7 +20,7 @@ public class YoutubeStreamUrlIdHandlerTest { private static YoutubeStreamUrlIdHandler urlIdHandler; @BeforeClass - public static void setUp() throws Exception { + public static void setUp() { urlIdHandler = YoutubeStreamUrlIdHandler.getInstance(); NewPipe.init(Downloader.getInstance()); } @@ -36,7 +36,7 @@ public class YoutubeStreamUrlIdHandlerTest { } @Test - public void getIdForInvalidUrls() throws ParsingException { + public void getIdForInvalidUrls() { List invalidUrls = new ArrayList<>(50); invalidUrls.add("https://www.youtube.com/watch?v=jZViOEv90d"); invalidUrls.add("https://www.youtube.com/watchjZViOEv90d"); @@ -55,7 +55,7 @@ public class YoutubeStreamUrlIdHandlerTest { } @Test - public void getId() throws Exception { + public void getIdfromYt() throws Exception { assertEquals("jZViOEv90dI", urlIdHandler.getId("https://www.youtube.com/watch?v=jZViOEv90dI")); assertEquals("W-fFHeTX70Q", urlIdHandler.getId("https://www.youtube.com/watch?v=W-fFHeTX70Q")); assertEquals("jZViOEv90dI", urlIdHandler.getId("https://www.youtube.com/watch?v=jZViOEv90dI?t=100")); @@ -78,8 +78,10 @@ public class YoutubeStreamUrlIdHandlerTest { assertEquals("EhxJLojIE_o", urlIdHandler.getId("http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare")); assertEquals("jZViOEv90dI", urlIdHandler.getId("vnd.youtube://www.youtube.com/watch?v=jZViOEv90dI")); assertEquals("jZViOEv90dI", urlIdHandler.getId("vnd.youtube:jZViOEv90dI")); + } - // Shared links + @Test + public void getIdfromSharedLinksYt() throws Exception { String sharedId = "7JIArTByb3E"; String realId = "Q7JsK50NGaA"; assertEquals(realId, urlIdHandler.getId("vnd.youtube://www.YouTube.com/shared?ci=" + sharedId + "&feature=twitter-deep-link")); @@ -89,13 +91,12 @@ public class YoutubeStreamUrlIdHandlerTest { @Test - public void testAcceptUrl() { + public void testAcceptYtUrl() { assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/watch?v=jZViOEv90dI")); assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/watch?v=jZViOEv90dI?t=100")); assertTrue(urlIdHandler.acceptUrl("https://WWW.YouTube.com/watch?v=jZViOEv90dI?t=100")); assertTrue(urlIdHandler.acceptUrl("HTTPS://www.youtube.com/watch?v=jZViOEv90dI?t=100")); assertTrue(urlIdHandler.acceptUrl("https://youtu.be/jZViOEv90dI?t=9s")); - //assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/watch/jZViOEv90dI")); assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/embed/jZViOEv90dI")); assertTrue(urlIdHandler.acceptUrl("https://www.youtube-nocookie.com/embed/jZViOEv90dI")); assertTrue(urlIdHandler.acceptUrl("http://www.youtube.com/watch?v=jZViOEv90dI")); @@ -107,10 +108,33 @@ public class YoutubeStreamUrlIdHandlerTest { assertTrue(urlIdHandler.acceptUrl("vnd.youtube:jZViOEv90dI")); assertTrue(urlIdHandler.acceptUrl("vnd.youtube:jZViOEv90dI")); + } + @Test + public void testAcceptSharedYtUrl() { String sharedId = "8A940MXKFmQ"; assertTrue(urlIdHandler.acceptUrl("vnd.youtube://www.youtube.com/shared?ci=" + sharedId + "&feature=twitter-deep-link")); assertTrue(urlIdHandler.acceptUrl("vnd.youtube://www.youtube.com/shared?ci=" + sharedId)); assertTrue(urlIdHandler.acceptUrl("https://www.youtube.com/shared?ci=" + sharedId)); } + + @Test + public void testAcceptHookUrl() { + assertTrue(urlIdHandler.acceptUrl("https://hooktube.com/watch?v=TglNG-yjabU")); + assertTrue(urlIdHandler.acceptUrl("hooktube.com/watch?v=3msbfr6pBNE")); + assertTrue(urlIdHandler.acceptUrl("https://hooktube.com/watch?v=ocH3oSnZG3c&list=PLS2VU1j4vzuZwooPjV26XM9UEBY2CPNn2")); + assertTrue(urlIdHandler.acceptUrl("hooktube.com/watch/3msbfr6pBNE")); + assertTrue(urlIdHandler.acceptUrl("hooktube.com/v/3msbfr6pBNE")); + assertTrue(urlIdHandler.acceptUrl("hooktube.com/embed/3msbfr6pBNE")); + } + + @Test + public void testGetHookIdfromUrl() throws ParsingException { + assertEquals("TglNG-yjabU", urlIdHandler.getId("https://hooktube.com/watch?v=TglNG-yjabU")); + assertEquals("3msbfr6pBNE", urlIdHandler.getId("hooktube.com/watch?v=3msbfr6pBNE")); + assertEquals("ocH3oSnZG3c", urlIdHandler.getId("https://hooktube.com/watch?v=ocH3oSnZG3c&list=PLS2VU1j4vzuZwooPjV26XM9UEBY2CPNn2")); + assertEquals("3msbfr6pBNE", urlIdHandler.getId("hooktube.com/watch/3msbfr6pBNE")); + assertEquals("3msbfr6pBNE", urlIdHandler.getId("hooktube.com/v/3msbfr6pBNE")); + assertEquals("3msbfr6pBNE", urlIdHandler.getId("hooktube.com/embed/3msbfr6pBNE")); + } } \ No newline at end of file