From a6c972eff8ae69d85c1dd74cb43fdcf59dd142c0 Mon Sep 17 00:00:00 2001 From: Connectety-W Date: Sun, 20 Jan 2019 01:31:30 +0100 Subject: [PATCH] fixed tests by prepending HTTP to URLs without protocol and adding a check for null. --- .../YoutubeChannelLinkHandlerFactory.java | 3 ++- .../YoutubePlaylistLinkHandlerFactory.java | 2 +- .../YoutubeStreamLinkHandlerFactory.java | 15 +++++------ .../YoutubeTrendingLinkHandlerFactory.java | 3 ++- .../schabi/newpipe/extractor/utils/Utils.java | 25 +++++++++++++++++-- 5 files changed, 36 insertions(+), 12 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 da207b278..e3522b313 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 @@ -2,6 +2,7 @@ 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.utils.Utils; import java.net.URL; import java.util.List; @@ -42,7 +43,7 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { @Override public String getId(String url) throws ParsingException { try { - URL urlObj = new URL(url); + URL urlObj = Utils.stringToURL(url); String path = urlObj.getPath(); if (!(YoutubeParsingHelper.isYoutubeURL(urlObj) || urlObj.getHost().equalsIgnoreCase("hooktube.com"))) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubePlaylistLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubePlaylistLinkHandlerFactory.java index 91944122d..008aeb933 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubePlaylistLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubePlaylistLinkHandlerFactory.java @@ -23,7 +23,7 @@ public class YoutubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory { @Override public String getId(String url) throws ParsingException { try { - URL urlObj = new URL(url); + URL urlObj = Utils.stringToURL(url); if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) { throw new ParsingException("the url given is not a Youtube-URL"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java index e479e74c9..3e793960f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java @@ -58,13 +58,14 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { public String getId(String urlString) throws ParsingException, IllegalArgumentException { try { URI uri = new URI(urlString); + String scheme = uri.getScheme(); - if (uri.getScheme().equals("vnd.youtube")) { - String scheme = uri.getSchemeSpecificPart(); - if (scheme.startsWith("//")) { - urlString = "https:" + scheme; + if (scheme != null && scheme.equals("vnd.youtube")) { + String schemeSpecificPart = uri.getSchemeSpecificPart(); + if (schemeSpecificPart.startsWith("//")) { + urlString = "https:" + schemeSpecificPart; } else { - return assertIsID(scheme); + return assertIsID(schemeSpecificPart); } } } catch (URISyntaxException ignored) { @@ -72,7 +73,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { URL url; try { - url = new URL(urlString); + url = Utils.stringToURL(urlString); } catch (MalformedURLException e) { throw new IllegalArgumentException("The given URL is not valid"); } @@ -115,7 +116,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory { URL decodedURL; try { - decodedURL = new URL("http://www.youtube.com" + uQueryValue); + decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue); } catch (MalformedURLException e) { throw new ParsingException("Error no suitable url: " + urlString); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeTrendingLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeTrendingLinkHandlerFactory.java index 123da9d1a..253e9cd8a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeTrendingLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeTrendingLinkHandlerFactory.java @@ -21,6 +21,7 @@ package org.schabi.newpipe.extractor.services.youtube.linkHandler; */ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; +import org.schabi.newpipe.extractor.utils.Utils; import java.net.MalformedURLException; import java.net.URL; @@ -41,7 +42,7 @@ public class YoutubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory { public boolean onAcceptUrl(final String url) { URL urlObj; try { - urlObj = new URL(url); + urlObj = Utils.stringToURL(url); } catch (MalformedURLException e) { return false; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java index 65f3ce24c..d4b8db432 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java @@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.utils; import org.schabi.newpipe.extractor.exceptions.ParsingException; import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.util.List; @@ -43,7 +44,7 @@ public class Utils { } public static void printErrors(List errors) { - for(Throwable e : errors) { + for (Throwable e : errors) { e.printStackTrace(); System.err.println("----------------"); } @@ -55,7 +56,7 @@ public class Utils { public static String replaceHttpWithHttps(final String url) { if (url == null) return null; - if(!url.isEmpty() && url.startsWith(HTTP)) { + if (!url.isEmpty() && url.startsWith(HTTP)) { return HTTPS + url.substring(HTTP.length()); } return url; @@ -99,4 +100,24 @@ public class Utils { return null; } + + /** + * converts a string to a URL-Object. + * defaults to HTTP if no protocol is given + * + * @param url the string to be converted to a URL-Object + * @return a URL-Object containing the url + */ + public static URL stringToURL(String url) throws MalformedURLException { + try { + return new URL(url); + } catch (MalformedURLException e) { + // if no protocol is given try prepending "http://" + if (e.getMessage().equals("no protocol: " + url)) { + return new URL(HTTP + url); + } + + throw e; + } + } } \ No newline at end of file