[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.
This commit is contained in:
AudricV 2023-01-28 13:15:13 +01:00
parent c589a2c1a2
commit 57f850bc2d
No known key found for this signature in database
GPG Key ID: DA92EC7905614198
1 changed files with 34 additions and 30 deletions

View File

@ -1,3 +1,23 @@
/*
* Created by Christian Schabesberger on 02.02.16.
*
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
* 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 <https://www.gnu.org/licenses/>.
*/
package org.schabi.newpipe.extractor.services.youtube.linkHandler; package org.schabi.newpipe.extractor.services.youtube.linkHandler;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isHooktubeURL; 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.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/*
* Created by Christian Schabesberger on 02.02.16.
*
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
* 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 <http://www.gnu.org/licenses/>.
*/
public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
private static final Pattern YOUTUBE_VIDEO_ID_REGEX_PATTERN private static final Pattern YOUTUBE_VIDEO_ID_REGEX_PATTERN
@ -49,7 +49,7 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
private static final YoutubeStreamLinkHandlerFactory INSTANCE private static final YoutubeStreamLinkHandlerFactory INSTANCE
= new YoutubeStreamLinkHandlerFactory(); = new YoutubeStreamLinkHandlerFactory();
private static final List<String> SUBPATHS private static final List<String> SUBPATHS
= Arrays.asList("embed/", "shorts/", "watch/", "v/", "w/"); = List.of("embed/", "live/", "shorts/", "watch/", "v/", "w/");
private YoutubeStreamLinkHandlerFactory() { private YoutubeStreamLinkHandlerFactory() {
} }
@ -67,21 +67,24 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
return null; return null;
} }
@Nonnull
private static String assertIsId(@Nullable final String id) throws ParsingException { private static String assertIsId(@Nullable final String id) throws ParsingException {
final String extractedId = extractId(id); final String extractedId = extractId(id);
if (extractedId != null) { if (extractedId != null) {
return extractedId; return extractedId;
} else { } 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 @Override
public String getUrl(final String id) { public String getUrl(final String id) {
return "https://www.youtube.com/watch?v=" + id; return "https://www.youtube.com/watch?v=" + id;
} }
@SuppressWarnings("AvoidNestedBlocks") @SuppressWarnings("AvoidNestedBlocks")
@Nonnull
@Override @Override
public String getId(final String theUrlString) public String getId(final String theUrlString)
throws ParsingException, IllegalArgumentException { throws ParsingException, IllegalArgumentException {
@ -124,14 +127,14 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
if (!Utils.isHTTP(url) || !(isYoutubeURL(url) || isYoutubeServiceURL(url) if (!Utils.isHTTP(url) || !(isYoutubeURL(url) || isYoutubeServiceURL(url)
|| isHooktubeURL(url) || isInvidiousURL(url) || isY2ubeURL(url))) { || isHooktubeURL(url) || isInvidiousURL(url) || isY2ubeURL(url))) {
if (host.equalsIgnoreCase("googleads.g.doubleclick.net")) { 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)) { 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 // Using uppercase instead of lowercase, because toLowercase replaces some unicode
@ -154,9 +157,9 @@ public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
final URL decodedURL; final URL decodedURL;
try { try {
decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue); decodedURL = Utils.stringToURL("https://www.youtube.com" + uQueryValue);
} catch (final MalformedURLException e) { } 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"); 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 @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) { for (final String subpath : SUBPATHS) {
if (path.startsWith(subpath)) { if (path.startsWith(subpath)) {
final String id = path.substring(subpath.length()); final String id = path.substring(subpath.length());