From 0e67d820bcbe65d1c7de0e8d4cbf607b13317fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Rumi=C5=84ski?= Date: Thu, 22 Oct 2020 20:13:29 +0200 Subject: [PATCH] Use static regex pattern for excluded path segments --- .../YoutubeChannelLinkHandlerFactory.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java index 288c28b28..af6bd12ca 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java @@ -1,5 +1,6 @@ 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.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; @@ -49,6 +50,7 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { return "https://www.youtube.com/" + id; } + /** * Returns true if path conform to * custom short channel URLs like youtube.com/yourcustomname @@ -56,15 +58,17 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { * @param splitPath path segments array * @return true - if value conform to short channel URL, false - not */ - private boolean isCustomShortChannelUrl(String[] splitPath) { - return splitPath.length == 1 && - !splitPath[0].matches("playlist|watch|attribution_link|watch_popup|embed|feed|select_site"); + private boolean isCustomShortChannelUrl(final String[] splitPath) { + return splitPath.length == 1 && !excludedSegments.matcher(splitPath[0]).matches(); } + private static final Pattern excludedSegments = + Pattern.compile("playlist|watch|attribution_link|watch_popup|embed|feed|select_site"); + @Override public String getId(String url) throws ParsingException { try { - URL urlObj = Utils.stringToURL(url); + final URL urlObj = Utils.stringToURL(url); String path = urlObj.getPath(); 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"); } - String id = splitPath[1]; + final String id = splitPath[1]; if (id == null || !id.matches("[A-Za-z0-9_-]+")) { throw new ParsingException("The given id is not a Youtube-Video-ID");