NewPipeExtractor/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFa...

140 lines
4.9 KiB
Java
Raw Normal View History

/*
2017-03-01 18:47:52 +01:00
* Created by Christian Schabesberger on 25.07.16.
*
2018-07-01 16:21:40 +02:00
* Copyright (C) Christian Schabesberger 2018 <chrźis.schabesberger@mailbox.org>
* YoutubeChannelLinkHandlerFactory.java is part of NewPipe Extractor.
2017-03-01 18:47:52 +01:00
*
* NewPipe Extractor is free software: you can redistribute it and/or modify
2017-03-01 18:47:52 +01:00
* 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,
2017-03-01 18:47:52 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2017-03-01 18:47:52 +01:00
* 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/>.
2017-03-01 18:47:52 +01:00
*/
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nonnull;
import java.net.URL;
import java.util.List;
import java.util.regex.Pattern;
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
2022-03-18 15:09:06 +01:00
public final class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
2017-03-01 18:47:52 +01:00
2022-03-18 15:09:06 +01:00
private static final YoutubeChannelLinkHandlerFactory INSTANCE
= new YoutubeChannelLinkHandlerFactory();
private static final Pattern EXCLUDED_SEGMENTS = Pattern.compile(
// CHECKSTYLE:OFF
"playlist|watch|attribution_link|watch_popup|embed|feed|select_site|account|reporthistory|redirect");
// CHECKSTYLE:ON
2022-03-18 15:09:06 +01:00
private YoutubeChannelLinkHandlerFactory() {
}
2020-10-23 16:42:13 +02:00
public static YoutubeChannelLinkHandlerFactory getInstance() {
2022-03-18 15:09:06 +01:00
return INSTANCE;
}
2020-02-29 16:26:28 +01:00
/**
* Returns the URL to a channel from an ID.
2020-02-29 16:26:28 +01:00
*
* @param id the channel ID including e.g. 'channel/'
* @return the URL to the channel
2020-02-29 16:26:28 +01:00
*/
@Override
2022-03-18 15:09:06 +01:00
public String getUrl(final String id,
final List<String> contentFilters,
final String searchFilter) {
return "https://www.youtube.com/" + id;
2017-03-01 18:47:52 +01:00
}
2022-03-18 15:09:06 +01:00
/**
* Checks whether the given path conforms to custom short channel URLs like
* {@code youtube.com/yourcustomname}.
*
* @param splitPath the path segments array
* @return whether the value conform to short channel URLs
*/
private boolean isCustomShortChannelUrl(@Nonnull final String[] splitPath) {
2022-03-18 15:09:06 +01:00
return splitPath.length == 1 && !EXCLUDED_SEGMENTS.matcher(splitPath[0]).matches();
}
/**
* Checks whether the given path conforms to handle URLs like {@code youtube.com/@yourhandle}.
*
* @param splitPath the path segments array
* @return whether the value conform to handle URLs
*/
private boolean isHandle(@Nonnull final String[] splitPath) {
return splitPath.length > 0 && splitPath[0].startsWith("@");
}
@Override
2022-03-18 15:09:06 +01:00
public String getId(final String url) throws ParsingException {
try {
final URL urlObj = Utils.stringToURL(url);
String path = urlObj.getPath();
2022-03-18 15:09:06 +01:00
if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj)
|| YoutubeParsingHelper.isInvidiousURL(urlObj)
2022-03-18 15:09:06 +01:00
|| YoutubeParsingHelper.isHooktubeURL(urlObj))) {
throw new ParsingException("The URL given is not a YouTube URL");
}
// Remove leading "/"
path = path.substring(1);
String[] splitPath = path.split("/");
if (isHandle(splitPath)) {
// Handle YouTube handle URLs like youtube.com/@yourhandle
return splitPath[0];
} else if (isCustomShortChannelUrl(splitPath)) {
// Handle custom short channel URLs like youtube.com/yourcustomname
path = "c/" + path;
splitPath = path.split("/");
}
if (!path.startsWith("user/") && !path.startsWith("channel/")
2022-03-18 15:09:06 +01:00
&& !path.startsWith("c/")) {
throw new ParsingException(
"The given URL is not a channel, a user or a handle URL");
}
final String id = splitPath[1];
if (isBlank(id)) {
throw new ParsingException("The given ID is not a YouTube channel or user ID");
}
return splitPath[0] + "/" + id;
} catch (final Exception e) {
throw new ParsingException("Could not parse URL :" + e.getMessage(), e);
}
2017-03-01 18:47:52 +01:00
}
@Override
2022-03-18 15:09:06 +01:00
public boolean onAcceptUrl(final String url) {
try {
getId(url);
2022-03-18 15:09:06 +01:00
} catch (final ParsingException e) {
return false;
}
return true;
2017-03-01 18:47:52 +01:00
}
}