From a02bfbeae5b7113cff2069e3823d42db21640fc0 Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 6 May 2020 15:41:01 +0200 Subject: [PATCH] [YouTube] Support stream urls in `vnd.youtube://ID` form --- .../YoutubeStreamLinkHandlerFactory.java | 45 ++++++++++++------- .../YoutubeStreamLinkHandlerFactoryTest.java | 1 + 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java index fd5b5b46d..45edfdda2 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java @@ -13,6 +13,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import javax.annotation.Nullable; + /* * Created by Christian Schabesberger on 02.02.16. * @@ -44,12 +46,16 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { return instance; } - private static String assertIsID(String id) throws ParsingException { - if (id == null || !id.matches("[a-zA-Z0-9_-]{11}")) { + private static boolean isId(@Nullable String id) { + return id != null && id.matches("[a-zA-Z0-9_-]{11}"); + } + + private static String assertIsId(@Nullable String id) throws ParsingException { + if (isId(id)) { + return id; + } else { throw new ParsingException("The given string is not a Youtube-Video-ID"); } - - return id; } @Override @@ -75,9 +81,14 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { if (scheme != null && (scheme.equals("vnd.youtube") || scheme.equals("vnd.youtube.launch"))) { String schemeSpecificPart = uri.getSchemeSpecificPart(); if (schemeSpecificPart.startsWith("//")) { + final String possiblyId = schemeSpecificPart.substring(2); + if (isId(possiblyId)) { + return possiblyId; + } + urlString = "https:" + schemeSpecificPart; } else { - return assertIsID(schemeSpecificPart); + return assertIsId(schemeSpecificPart); } } } catch (URISyntaxException ignored) { @@ -118,7 +129,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { if (path.startsWith("embed/")) { String id = path.split("/")[1]; - return assertIsID(id); + return assertIsId(id); } break; @@ -139,38 +150,38 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { } String viewQueryValue = Utils.getQueryValue(decodedURL, "v"); - return assertIsID(viewQueryValue); + return assertIsId(viewQueryValue); } if (path.startsWith("embed/")) { String id = path.split("/")[1]; - return assertIsID(id); + return assertIsId(id); } String viewQueryValue = Utils.getQueryValue(url, "v"); - return assertIsID(viewQueryValue); + return assertIsId(viewQueryValue); } case "YOUTU.BE": { String viewQueryValue = Utils.getQueryValue(url, "v"); if (viewQueryValue != null) { - return assertIsID(viewQueryValue); + return assertIsId(viewQueryValue); } - return assertIsID(path); + return assertIsId(path); } case "HOOKTUBE.COM": { if (path.startsWith("v/")) { String id = path.substring("v/".length()); - return assertIsID(id); + return assertIsId(id); } if (path.startsWith("watch/")) { String id = path.substring("watch/".length()); - return assertIsID(id); + return assertIsId(id); } // there is no break-statement here on purpose so the next code-block gets also run for hooktube } @@ -193,21 +204,21 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { if (path.equals("watch")) { String viewQueryValue = Utils.getQueryValue(url, "v"); if (viewQueryValue != null) { - return assertIsID(viewQueryValue); + return assertIsId(viewQueryValue); } } if (path.startsWith("embed/")) { String id = path.substring("embed/".length()); - return assertIsID(id); + return assertIsId(id); } String viewQueryValue = Utils.getQueryValue(url, "v"); if (viewQueryValue != null) { - return assertIsID(viewQueryValue); + return assertIsId(viewQueryValue); } - return assertIsID(path); + return assertIsId(path); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamLinkHandlerFactoryTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamLinkHandlerFactoryTest.java index 908642fdb..62398d597 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamLinkHandlerFactoryTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamLinkHandlerFactoryTest.java @@ -79,6 +79,7 @@ public class YoutubeStreamLinkHandlerFactoryTest { assertEquals("EhxJLojIE_o", linkHandler.fromUrl("http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare").getId()); assertEquals("jZViOEv90dI", linkHandler.fromUrl("vnd.youtube://www.youtube.com/watch?v=jZViOEv90dI").getId()); assertEquals("jZViOEv90dI", linkHandler.fromUrl("vnd.youtube:jZViOEv90dI").getId()); + assertEquals("n8X9_MgEdCg", linkHandler.fromUrl("vnd.youtube://n8X9_MgEdCg").getId()); assertEquals("O0EDx9WAelc", linkHandler.fromUrl("https://music.youtube.com/watch?v=O0EDx9WAelc").getId()); }