split isYoutubeALikeURL into multiple methods

This commit is contained in:
Connectety-L 2019-01-27 01:28:51 +01:00
parent 2ede47d36c
commit 7493ed903b
No known key found for this signature in database
GPG Key ID: 853F15C538FCE6DF
6 changed files with 37 additions and 36 deletions

View File

@ -46,7 +46,8 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
URL urlObj = Utils.stringToURL(url);
String path = urlObj.getPath();
if (!YoutubeParsingHelper.isYoutubeALikeURL(urlObj)) { // fixme: accepts youtu.be and youtube-nocookie.com
if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj) ||
YoutubeParsingHelper.isInvidioURL(urlObj) || YoutubeParsingHelper.isHooktubeURL(urlObj))) {
throw new ParsingException("the URL given is not a Youtube-URL");
}

View File

@ -30,41 +30,25 @@ public class YoutubeParsingHelper {
private YoutubeParsingHelper() {
}
private static boolean isHTTP(URL url) {
// make sure its http or https
String protocol = url.getProtocol();
if (!protocol.equals("http") && !protocol.equals("https")) {
return false;
}
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
boolean setsNoPort = url.getPort() == -1;
return setsNoPort || usesDefaultPort;
}
public static boolean isYoutubeURL(URL url) {
// make sure its http or https
if (!isHTTP(url))
return false;
// make sure its a known youtube url
String host = url.getHost();
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|| host.equalsIgnoreCase("m.youtube.com");
}
public static boolean isYoutubeALikeURL(URL url) {
// make sure its http or https
if (!isHTTP(url))
return false;
// make sure its a known youtube url
public static boolean isYoutubeServiceURL(URL url) {
String host = url.getHost();
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|| host.equalsIgnoreCase("m.youtube.com") || host.equalsIgnoreCase("www.youtube-nocookie.com")
|| host.equalsIgnoreCase("youtu.be") || host.equalsIgnoreCase("hooktube.com")
|| host.equalsIgnoreCase("invidio.us");
return host.equalsIgnoreCase("www.youtube-nocookie.com") || host.equalsIgnoreCase("youtu.be");
}
public static boolean isHooktubeURL(URL url) {
String host = url.getHost();
return host.equalsIgnoreCase("hooktube.com");
}
public static boolean isInvidioURL(URL url) {
String host = url.getHost();
return host.equalsIgnoreCase("invidio.us") || host.equalsIgnoreCase("www.invidio.us");
}
public static long parseDurationString(String input)

View File

@ -25,7 +25,7 @@ public class YoutubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
try {
URL urlObj = Utils.stringToURL(url);
if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) {
if (!Utils.isHTTP(urlObj) || !YoutubeParsingHelper.isYoutubeURL(urlObj)) {
throw new ParsingException("the url given is not a Youtube-URL");
}

View File

@ -85,7 +85,9 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
path = path.substring(1);
}
if (!YoutubeParsingHelper.isYoutubeALikeURL(url)) {
if (!Utils.isHTTP(url) || !(YoutubeParsingHelper.isYoutubeURL(url) ||
YoutubeParsingHelper.isYoutubeServiceURL(url) || YoutubeParsingHelper.isHooktubeURL(url) ||
YoutubeParsingHelper.isInvidioURL(url))) {
if (host.equalsIgnoreCase("googleads.g.doubleclick.net")) {
throw new FoundAdException("Error found ad: " + urlString);
}
@ -159,8 +161,9 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
}
// there is no break-statement here on purpose so the next code-block gets also run for hooktube
}
case "INVIDIO.US": { // code-block for hooktube.com and invidio.us
case "WWW.INVIDIO.US":
case "INVIDIO.US": { // code-block for hooktube.com and invidio.us
if (path.equals("watch")) {
String viewQueryValue = Utils.getQueryValue(url, "v");
if (viewQueryValue != null) {
@ -169,10 +172,10 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
}
if (path.startsWith("embed/")) {
String id = path.substring("embed/".length());
return assertIsID(id);
}
break;
}
}

View File

@ -48,6 +48,6 @@ public class YoutubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
}
String urlPath = urlObj.getPath();
return YoutubeParsingHelper.isYoutubeURL(urlObj) && urlPath.equals("/feed/trending");
return Utils.isHTTP(urlObj) && (YoutubeParsingHelper.isYoutubeURL(urlObj)) && urlPath.equals("/feed/trending");
}
}

View File

@ -120,4 +120,17 @@ public class Utils {
throw e;
}
}
public static boolean isHTTP(URL url) {
// make sure its http or https
String protocol = url.getProtocol();
if (!protocol.equals("http") && !protocol.equals("https")) {
return false;
}
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
boolean setsNoPort = url.getPort() == -1;
return setsNoPort || usesDefaultPort;
}
}