From 4e04991762a6c3d2aa4fce126e8ef141a6bb0fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Rumi=C5=84ski?= Date: Fri, 9 Oct 2020 00:37:34 +0200 Subject: [PATCH] Support short custom youtube channel urls --- .../YoutubeChannelLinkHandlerFactory.java | 27 +++++++++++++++---- .../YoutubeChannelLinkHandlerFactoryTest.java | 2 ++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java index 0eb03085..89eaaed5 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java @@ -49,6 +49,17 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { return "https://www.youtube.com/" + id; } + /** + * Returns true if path conform to + * custom short channel urls like youtube.com/yourcustomname + * + * @param splitPath path segments array + * @return true - if value conform to short channel url, false - not + */ + public boolean isCustomShortChannelUrl(String[] splitPath) { + return splitPath.length == 1 && !splitPath[0].matches("playlist|watch"); + } + @Override public String getId(String url) throws ParsingException { try { @@ -60,14 +71,20 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { throw new ParsingException("the URL given is not a Youtube-URL"); } - if (!path.startsWith("/user/") && !path.startsWith("/channel/") && !path.startsWith("/c/")) { + // remove leading "/" + path = path.substring(1); + String[] splitPath = path.split("/"); + + // Handle custom short channel urls like youtube.com/yourcustomname + if (isCustomShortChannelUrl(splitPath)) { + path = "c/" + path; + splitPath = path.split("/"); + } + + if (!path.startsWith("user/") && !path.startsWith("channel/") && !path.startsWith("c/")) { throw new ParsingException("the URL given is neither a channel nor an user"); } - // remove leading "/" - path = path.substring(1); - - String[] splitPath = path.split("/"); String id = splitPath[1]; if (id == null || !id.matches("[A-Za-z0-9_-]+")) { diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLinkHandlerFactoryTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLinkHandlerFactoryTest.java index fc409ffa..178d4b8a 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLinkHandlerFactoryTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLinkHandlerFactoryTest.java @@ -30,6 +30,8 @@ public class YoutubeChannelLinkHandlerFactoryTest { assertTrue(linkHandler.acceptUrl("https://www.youtube.com/c/creatoracademy")); + assertTrue(linkHandler.acceptUrl("https://youtube.com/DIMENSI0N")); + assertTrue(linkHandler.acceptUrl("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA")); assertTrue(linkHandler.acceptUrl("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1"));