From 57f850bc2da24507fddcce85bca34f15c54c965f Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sat, 28 Jan 2023 13:15:13 +0100 Subject: [PATCH] [YouTube] Support live URLs and do minor improvements to YoutubeStreamLinkHandlerFactory - Move license header at the top; - Use an unmodifiable set for the subpaths instead of a modifiable list; - Add missing Nonnull and Nullable annotations; - Improve exception messages. --- .../YoutubeStreamLinkHandlerFactory.java | 64 ++++++++++--------- 1 file changed, 34 insertions(+), 30 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 5713651e7..0872bbe2d 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 @@ -1,3 +1,23 @@ +/* + * Created by Christian Schabesberger on 02.02.16. + * + * Copyright (C) Christian Schabesberger 2018 + * YoutubeStreamLinkHandlerFactory.java is part of NewPipe Extractor. + * + * NewPipe Extractor is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NewPipe Extractor is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NewPipe Extractor. If not, see . + */ + package org.schabi.newpipe.extractor.services.youtube.linkHandler; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isHooktubeURL; @@ -15,33 +35,13 @@ import java.net.MalformedURLException; 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; +import javax.annotation.Nonnull; import javax.annotation.Nullable; -/* - * Created by Christian Schabesberger on 02.02.16. - * - * Copyright (C) Christian Schabesberger 2018 - * YoutubeStreamLinkHandlerFactory.java is part of NewPipe. - * - * NewPipe is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * NewPipe is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with NewPipe. If not, see . - */ - public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { private static final Pattern YOUTUBE_VIDEO_ID_REGEX_PATTERN @@ -49,7 +49,7 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { private static final YoutubeStreamLinkHandlerFactory INSTANCE = new YoutubeStreamLinkHandlerFactory(); private static final List SUBPATHS - = Arrays.asList("embed/", "shorts/", "watch/", "v/", "w/"); + = List.of("embed/", "live/", "shorts/", "watch/", "v/", "w/"); private YoutubeStreamLinkHandlerFactory() { } @@ -67,21 +67,24 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { return null; } + @Nonnull private static String assertIsId(@Nullable final String id) throws ParsingException { final String extractedId = extractId(id); if (extractedId != null) { return extractedId; } else { - throw new ParsingException("The given string is not a Youtube-Video-ID"); + throw new ParsingException("The given string is not a YouTube video ID"); } } + @Nonnull @Override public String getUrl(final String id) { return "https://www.youtube.com/watch?v=" + id; } @SuppressWarnings("AvoidNestedBlocks") + @Nonnull @Override public String getId(final String theUrlString) throws ParsingException, IllegalArgumentException { @@ -124,14 +127,14 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { if (!Utils.isHTTP(url) || !(isYoutubeURL(url) || isYoutubeServiceURL(url) || isHooktubeURL(url) || isInvidiousURL(url) || isY2ubeURL(url))) { if (host.equalsIgnoreCase("googleads.g.doubleclick.net")) { - throw new FoundAdException("Error found ad: " + urlString); + throw new FoundAdException("Error: found ad: " + urlString); } - throw new ParsingException("The url is not a Youtube-URL"); + throw new ParsingException("The URL is not a YouTube URL"); } if (YoutubePlaylistLinkHandlerFactory.getInstance().acceptUrl(urlString)) { - throw new ParsingException("Error no suitable url: " + urlString); + throw new ParsingException("Error: no suitable URL: " + urlString); } // Using uppercase instead of lowercase, because toLowercase replaces some unicode @@ -154,9 +157,9 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { final URL decodedURL; try { - decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue); + decodedURL = Utils.stringToURL("https://www.youtube.com" + uQueryValue); } catch (final MalformedURLException e) { - throw new ParsingException("Error no suitable url: " + urlString); + throw new ParsingException("Error: no suitable URL: " + urlString); } final String viewQueryValue = Utils.getQueryValue(decodedURL, "v"); @@ -231,7 +234,7 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { } } - throw new ParsingException("Error no suitable url: " + urlString); + throw new ParsingException("Error: no suitable URL: " + urlString); } @Override @@ -246,7 +249,8 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { } } - private String getIdFromSubpathsInPath(final String path) throws ParsingException { + @Nullable + private String getIdFromSubpathsInPath(@Nonnull final String path) throws ParsingException { for (final String subpath : SUBPATHS) { if (path.startsWith(subpath)) { final String id = path.substring(subpath.length());