Skip to content

Commit 7493ed9

Browse files
committed
split isYoutubeALikeURL into multiple methods
1 parent 2ede47d commit 7493ed9

6 files changed

Lines changed: 36 additions & 35 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
@@ -46,7 +46,8 @@ public String getId(String url) throws ParsingException {
4646
URL urlObj = Utils.stringToURL(url);
4747
String path = urlObj.getPath();
4848

49-
if (!YoutubeParsingHelper.isYoutubeALikeURL(urlObj)) { // fixme: accepts youtu.be and youtube-nocookie.com
49+
if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj) ||
50+
YoutubeParsingHelper.isInvidioURL(urlObj) || YoutubeParsingHelper.isHooktubeURL(urlObj))) {
5051
throw new ParsingException("the URL given is not a Youtube-URL");
5152
}
5253

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,25 @@ public class YoutubeParsingHelper {
3030
private YoutubeParsingHelper() {
3131
}
3232

33-
private static boolean isHTTP(URL url) {
34-
// make sure its http or https
35-
String protocol = url.getProtocol();
36-
if (!protocol.equals("http") && !protocol.equals("https")) {
37-
return false;
38-
}
39-
40-
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
41-
boolean setsNoPort = url.getPort() == -1;
42-
43-
return setsNoPort || usesDefaultPort;
44-
}
45-
4633
public static boolean isYoutubeURL(URL url) {
47-
// make sure its http or https
48-
if (!isHTTP(url))
49-
return false;
50-
51-
// make sure its a known youtube url
5234
String host = url.getHost();
5335
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
5436
|| host.equalsIgnoreCase("m.youtube.com");
5537
}
5638

57-
public static boolean isYoutubeALikeURL(URL url) {
58-
// make sure its http or https
59-
if (!isHTTP(url))
60-
return false;
39+
public static boolean isYoutubeServiceURL(URL url) {
40+
String host = url.getHost();
41+
return host.equalsIgnoreCase("www.youtube-nocookie.com") || host.equalsIgnoreCase("youtu.be");
42+
}
6143

62-
// make sure its a known youtube url
44+
public static boolean isHooktubeURL(URL url) {
6345
String host = url.getHost();
64-
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
65-
|| host.equalsIgnoreCase("m.youtube.com") || host.equalsIgnoreCase("www.youtube-nocookie.com")
66-
|| host.equalsIgnoreCase("youtu.be") || host.equalsIgnoreCase("hooktube.com")
67-
|| host.equalsIgnoreCase("invidio.us");
46+
return host.equalsIgnoreCase("hooktube.com");
47+
}
48+
49+
public static boolean isInvidioURL(URL url) {
50+
String host = url.getHost();
51+
return host.equalsIgnoreCase("invidio.us") || host.equalsIgnoreCase("www.invidio.us");
6852
}
6953

7054
public static long parseDurationString(String input)

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
@@ -25,7 +25,7 @@ public String getId(String url) throws ParsingException {
2525
try {
2626
URL urlObj = Utils.stringToURL(url);
2727

28-
if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) {
28+
if (!Utils.isHTTP(urlObj) || !YoutubeParsingHelper.isYoutubeURL(urlObj)) {
2929
throw new ParsingException("the url given is not a Youtube-URL");
3030
}
3131

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
8585
path = path.substring(1);
8686
}
8787

88-
if (!YoutubeParsingHelper.isYoutubeALikeURL(url)) {
88+
if (!Utils.isHTTP(url) || !(YoutubeParsingHelper.isYoutubeURL(url) ||
89+
YoutubeParsingHelper.isYoutubeServiceURL(url) || YoutubeParsingHelper.isHooktubeURL(url) ||
90+
YoutubeParsingHelper.isInvidioURL(url))) {
8991
if (host.equalsIgnoreCase("googleads.g.doubleclick.net")) {
9092
throw new FoundAdException("Error found ad: " + urlString);
9193
}
@@ -159,8 +161,9 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
159161
}
160162
// there is no break-statement here on purpose so the next code-block gets also run for hooktube
161163
}
162-
163-
case "INVIDIO.US": { // code-block for hooktube.com and invidio.us
164+
165+
case "WWW.INVIDIO.US":
166+
case "INVIDIO.US": { // code-block for hooktube.com and invidio.us
164167
if (path.equals("watch")) {
165168
String viewQueryValue = Utils.getQueryValue(url, "v");
166169
if (viewQueryValue != null) {
@@ -169,10 +172,10 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
169172
}
170173
if (path.startsWith("embed/")) {
171174
String id = path.substring("embed/".length());
172-
175+
173176
return assertIsID(id);
174177
}
175-
178+
176179
break;
177180
}
178181
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public boolean onAcceptUrl(final String url) {
4848
}
4949

5050
String urlPath = urlObj.getPath();
51-
return YoutubeParsingHelper.isYoutubeURL(urlObj) && urlPath.equals("/feed/trending");
51+
return Utils.isHTTP(urlObj) && (YoutubeParsingHelper.isYoutubeURL(urlObj)) && urlPath.equals("/feed/trending");
5252
}
5353
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,17 @@ public static URL stringToURL(String url) throws MalformedURLException {
120120
throw e;
121121
}
122122
}
123+
124+
public static boolean isHTTP(URL url) {
125+
// make sure its http or https
126+
String protocol = url.getProtocol();
127+
if (!protocol.equals("http") && !protocol.equals("https")) {
128+
return false;
129+
}
130+
131+
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
132+
boolean setsNoPort = url.getPort() == -1;
133+
134+
return setsNoPort || usesDefaultPort;
135+
}
123136
}

0 commit comments

Comments
 (0)