fixed tests by prepending HTTP to URLs without protocol and adding a check for null.
This commit is contained in:
parent
97d72590fc
commit
a6c972eff8
|
@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor.services.youtube.linkHandler;
|
||||||
|
|
||||||
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.utils.Utils;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -42,7 +43,7 @@ public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
|
||||||
@Override
|
@Override
|
||||||
public String getId(String url) throws ParsingException {
|
public String getId(String url) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
URL urlObj = new URL(url);
|
URL urlObj = Utils.stringToURL(url);
|
||||||
String path = urlObj.getPath();
|
String path = urlObj.getPath();
|
||||||
|
|
||||||
if (!(YoutubeParsingHelper.isYoutubeURL(urlObj) || urlObj.getHost().equalsIgnoreCase("hooktube.com"))) {
|
if (!(YoutubeParsingHelper.isYoutubeURL(urlObj) || urlObj.getHost().equalsIgnoreCase("hooktube.com"))) {
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class YoutubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
|
||||||
@Override
|
@Override
|
||||||
public String getId(String url) throws ParsingException {
|
public String getId(String url) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
URL urlObj = new URL(url);
|
URL urlObj = Utils.stringToURL(url);
|
||||||
|
|
||||||
if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) {
|
if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) {
|
||||||
throw new ParsingException("the url given is not a Youtube-URL");
|
throw new ParsingException("the url given is not a Youtube-URL");
|
||||||
|
|
|
@ -58,13 +58,14 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
|
||||||
public String getId(String urlString) throws ParsingException, IllegalArgumentException {
|
public String getId(String urlString) throws ParsingException, IllegalArgumentException {
|
||||||
try {
|
try {
|
||||||
URI uri = new URI(urlString);
|
URI uri = new URI(urlString);
|
||||||
|
String scheme = uri.getScheme();
|
||||||
|
|
||||||
if (uri.getScheme().equals("vnd.youtube")) {
|
if (scheme != null && scheme.equals("vnd.youtube")) {
|
||||||
String scheme = uri.getSchemeSpecificPart();
|
String schemeSpecificPart = uri.getSchemeSpecificPart();
|
||||||
if (scheme.startsWith("//")) {
|
if (schemeSpecificPart.startsWith("//")) {
|
||||||
urlString = "https:" + scheme;
|
urlString = "https:" + schemeSpecificPart;
|
||||||
} else {
|
} else {
|
||||||
return assertIsID(scheme);
|
return assertIsID(schemeSpecificPart);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (URISyntaxException ignored) {
|
} catch (URISyntaxException ignored) {
|
||||||
|
@ -72,7 +73,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
|
||||||
|
|
||||||
URL url;
|
URL url;
|
||||||
try {
|
try {
|
||||||
url = new URL(urlString);
|
url = Utils.stringToURL(urlString);
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
throw new IllegalArgumentException("The given URL is not valid");
|
throw new IllegalArgumentException("The given URL is not valid");
|
||||||
}
|
}
|
||||||
|
@ -115,7 +116,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
|
||||||
|
|
||||||
URL decodedURL;
|
URL decodedURL;
|
||||||
try {
|
try {
|
||||||
decodedURL = new URL("http://www.youtube.com" + uQueryValue);
|
decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue);
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
throw new ParsingException("Error no suitable url: " + urlString);
|
throw new ParsingException("Error no suitable url: " + urlString);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ package org.schabi.newpipe.extractor.services.youtube.linkHandler;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
|
||||||
|
import org.schabi.newpipe.extractor.utils.Utils;
|
||||||
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
@ -41,7 +42,7 @@ public class YoutubeTrendingLinkHandlerFactory extends ListLinkHandlerFactory {
|
||||||
public boolean onAcceptUrl(final String url) {
|
public boolean onAcceptUrl(final String url) {
|
||||||
URL urlObj;
|
URL urlObj;
|
||||||
try {
|
try {
|
||||||
urlObj = new URL(url);
|
urlObj = Utils.stringToURL(url);
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.utils;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -99,4 +100,24 @@ public class Utils {
|
||||||
|
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue