Skip to content

Commit a6c972e

Browse files
committed
fixed tests by prepending HTTP to URLs without protocol and adding a check for null.
1 parent 97d7259 commit a6c972e

5 files changed

Lines changed: 36 additions & 12 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelLinkHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.schabi.newpipe.extractor.exceptions.ParsingException;
44
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
5+
import org.schabi.newpipe.extractor.utils.Utils;
56

67
import java.net.URL;
78
import java.util.List;
@@ -42,7 +43,7 @@ public String getUrl(String id, List<String> contentFilters, String searchFilter
4243
@Override
4344
public String getId(String url) throws ParsingException {
4445
try {
45-
URL urlObj = new URL(url);
46+
URL urlObj = Utils.stringToURL(url);
4647
String path = urlObj.getPath();
4748

4849
if (!(YoutubeParsingHelper.isYoutubeURL(urlObj) || urlObj.getHost().equalsIgnoreCase("hooktube.com"))) {

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubePlaylistLinkHandlerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public String getUrl(String id, List<String> contentFilters, String sortFilter)
2323
@Override
2424
public String getId(String url) throws ParsingException {
2525
try {
26-
URL urlObj = new URL(url);
26+
URL urlObj = Utils.stringToURL(url);
2727

2828
if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) {
2929
throw new ParsingException("the url given is not a Youtube-URL");

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,22 @@ public String getUrl(String id) {
5858
public String getId(String urlString) throws ParsingException, IllegalArgumentException {
5959
try {
6060
URI uri = new URI(urlString);
61+
String scheme = uri.getScheme();
6162

62-
if (uri.getScheme().equals("vnd.youtube")) {
63-
String scheme = uri.getSchemeSpecificPart();
64-
if (scheme.startsWith("//")) {
65-
urlString = "https:" + scheme;
63+
if (scheme != null && scheme.equals("vnd.youtube")) {
64+
String schemeSpecificPart = uri.getSchemeSpecificPart();
65+
if (schemeSpecificPart.startsWith("//")) {
66+
urlString = "https:" + schemeSpecificPart;
6667
} else {
67-
return assertIsID(scheme);
68+
return assertIsID(schemeSpecificPart);
6869
}
6970
}
7071
} catch (URISyntaxException ignored) {
7172
}
7273

7374
URL url;
7475
try {
75-
url = new URL(urlString);
76+
url = Utils.stringToURL(urlString);
7677
} catch (MalformedURLException e) {
7778
throw new IllegalArgumentException("The given URL is not valid");
7879
}
@@ -115,7 +116,7 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
115116

116117
URL decodedURL;
117118
try {
118-
decodedURL = new URL("http://www.youtube.com" + uQueryValue);
119+
decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue);
119120
} catch (MalformedURLException e) {
120121
throw new ParsingException("Error no suitable url: " + urlString);
121122
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeTrendingLinkHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
24+
import org.schabi.newpipe.extractor.utils.Utils;
2425

2526
import java.net.MalformedURLException;
2627
import java.net.URL;
@@ -41,7 +42,7 @@ public String getId(String url) {
4142
public boolean onAcceptUrl(final String url) {
4243
URL urlObj;
4344
try {
44-
urlObj = new URL(url);
45+
urlObj = Utils.stringToURL(url);
4546
} catch (MalformedURLException e) {
4647
return false;
4748
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.schabi.newpipe.extractor.exceptions.ParsingException;
44

55
import java.io.UnsupportedEncodingException;
6+
import java.net.MalformedURLException;
67
import java.net.URL;
78
import java.net.URLDecoder;
89
import java.util.List;
@@ -43,7 +44,7 @@ public static void checkUrl(String pattern, String url) throws ParsingException
4344
}
4445

4546
public static void printErrors(List<Throwable> errors) {
46-
for(Throwable e : errors) {
47+
for (Throwable e : errors) {
4748
e.printStackTrace();
4849
System.err.println("----------------");
4950
}
@@ -55,7 +56,7 @@ public static void printErrors(List<Throwable> errors) {
5556
public static String replaceHttpWithHttps(final String url) {
5657
if (url == null) return null;
5758

58-
if(!url.isEmpty() && url.startsWith(HTTP)) {
59+
if (!url.isEmpty() && url.startsWith(HTTP)) {
5960
return HTTPS + url.substring(HTTP.length());
6061
}
6162
return url;
@@ -99,4 +100,24 @@ public static String getQueryValue(URL url, String parameterName) {
99100

100101
return null;
101102
}
103+
104+
/**
105+
* converts a string to a URL-Object.
106+
* defaults to HTTP if no protocol is given
107+
*
108+
* @param url the string to be converted to a URL-Object
109+
* @return a URL-Object containing the url
110+
*/
111+
public static URL stringToURL(String url) throws MalformedURLException {
112+
try {
113+
return new URL(url);
114+
} catch (MalformedURLException e) {
115+
// if no protocol is given try prepending "http://"
116+
if (e.getMessage().equals("no protocol: " + url)) {
117+
return new URL(HTTP + url);
118+
}
119+
120+
throw e;
121+
}
122+
}
102123
}

0 commit comments

Comments
 (0)