fixed tests by prepending HTTP to URLs without protocol and adding a check for null.

This commit is contained in:
Connectety-W 2019-01-20 01:31:30 +01:00
parent 97d72590fc
commit a6c972eff8
No known key found for this signature in database
GPG Key ID: 8F39B4F36D48B3F8
5 changed files with 36 additions and 12 deletions

View File

@ -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"))) {

View File

@ -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");

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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<Throwable> 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;
}
}
}