Use static regex pattern for excluded path segments

This commit is contained in:
Bartosz Rumiński 2020-10-22 20:13:29 +02:00
parent d3f80d1538
commit 0e67d820bc

View File

@ -1,5 +1,6 @@
package org.schabi.newpipe.extractor.services.youtube.linkHandler; package org.schabi.newpipe.extractor.services.youtube.linkHandler;
import java.util.regex.Pattern;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
@ -49,6 +50,7 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
return "https://www.youtube.com/" + id; return "https://www.youtube.com/" + id;
} }
/** /**
* Returns true if path conform to * Returns true if path conform to
* custom short channel URLs like youtube.com/yourcustomname * custom short channel URLs like youtube.com/yourcustomname
@ -56,15 +58,17 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
* @param splitPath path segments array * @param splitPath path segments array
* @return true - if value conform to short channel URL, false - not * @return true - if value conform to short channel URL, false - not
*/ */
private boolean isCustomShortChannelUrl(String[] splitPath) { private boolean isCustomShortChannelUrl(final String[] splitPath) {
return splitPath.length == 1 && return splitPath.length == 1 && !excludedSegments.matcher(splitPath[0]).matches();
!splitPath[0].matches("playlist|watch|attribution_link|watch_popup|embed|feed|select_site");
} }
private static final Pattern excludedSegments =
Pattern.compile("playlist|watch|attribution_link|watch_popup|embed|feed|select_site");
@Override @Override
public String getId(String url) throws ParsingException { public String getId(String url) throws ParsingException {
try { try {
URL urlObj = Utils.stringToURL(url); final URL urlObj = Utils.stringToURL(url);
String path = urlObj.getPath(); String path = urlObj.getPath();
if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj) || if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj) ||
@ -86,7 +90,7 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
throw new ParsingException("the URL given is neither a channel nor an user"); throw new ParsingException("the URL given is neither a channel nor an user");
} }
String id = splitPath[1]; final String id = splitPath[1];
if (id == null || !id.matches("[A-Za-z0-9_-]+")) { if (id == null || !id.matches("[A-Za-z0-9_-]+")) {
throw new ParsingException("The given id is not a Youtube-Video-ID"); throw new ParsingException("The given id is not a Youtube-Video-ID");