Skip to content

Commit 8114ce6

Browse files
authored
Merge pull request #132 from connectety/master
refactored YouTube-linkHandler
2 parents f7c7b9d + 19288c1 commit 8114ce6

7 files changed

Lines changed: 305 additions & 92 deletions

File tree

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
22

3-
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
43
import org.schabi.newpipe.extractor.exceptions.ParsingException;
5-
import org.schabi.newpipe.extractor.utils.Parser;
4+
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
5+
import org.schabi.newpipe.extractor.utils.Utils;
66

7+
import java.net.URL;
78
import java.util.List;
89

910
/*
@@ -29,25 +30,53 @@
2930
public class YoutubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
3031

3132
private static final YoutubeChannelLinkHandlerFactory instance = new YoutubeChannelLinkHandlerFactory();
32-
private static final String ID_PATTERN = "/(user/[A-Za-z0-9_-]*|channel/[A-Za-z0-9_-]*)";
3333

3434
public static YoutubeChannelLinkHandlerFactory getInstance() {
3535
return instance;
3636
}
3737

3838
@Override
39-
public String getId(String url) throws ParsingException {
40-
return Parser.matchGroup1(ID_PATTERN, url);
39+
public String getUrl(String id, List<String> contentFilters, String searchFilter) {
40+
return "https://www.youtube.com/" + id;
4141
}
4242

4343
@Override
44-
public String getUrl(String id, List<String> contentFilters, String searchFilter) {
45-
return "https://www.youtube.com/" + id;
44+
public String getId(String url) throws ParsingException {
45+
try {
46+
URL urlObj = Utils.stringToURL(url);
47+
String path = urlObj.getPath();
48+
49+
if (!(YoutubeParsingHelper.isYoutubeURL(urlObj) || urlObj.getHost().equalsIgnoreCase("hooktube.com"))) {
50+
throw new ParsingException("the URL given is not a Youtube-URL");
51+
}
52+
53+
if (!path.startsWith("/user/") && !path.startsWith("/channel/")) {
54+
throw new ParsingException("the URL given is neither a channel nor an user");
55+
}
56+
57+
// remove leading "/"
58+
path = path.substring(1);
59+
60+
String[] splitPath = path.split("/");
61+
String id = splitPath[1];
62+
63+
if (id == null || !id.matches("[A-Za-z0-9_-]+")) {
64+
throw new ParsingException("The given id is not a Youtube-Video-ID");
65+
}
66+
67+
return splitPath[0] + "/" + id;
68+
} catch (final Exception exception) {
69+
throw new ParsingException("Error could not parse url :" + exception.getMessage(), exception);
70+
}
4671
}
4772

4873
@Override
4974
public boolean onAcceptUrl(String url) {
50-
return (url.contains("youtube") || url.contains("youtu.be") || url.contains("hooktube.com"))
51-
&& (url.contains("/user/") || url.contains("/channel/"));
75+
try {
76+
getId(url);
77+
} catch (ParsingException e) {
78+
return false;
79+
}
80+
return true;
5281
}
5382
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import org.schabi.newpipe.extractor.exceptions.ParsingException;
55

6+
import java.net.URL;
7+
68
/*
79
* Created by Christian Schabesberger on 02.03.16.
810
*
@@ -28,6 +30,42 @@ public class YoutubeParsingHelper {
2830
private YoutubeParsingHelper() {
2931
}
3032

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+
46+
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
52+
String host = url.getHost();
53+
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
54+
|| host.equalsIgnoreCase("m.youtube.com");
55+
}
56+
57+
public static boolean isYoutubeALikeURL(URL url) {
58+
// make sure its http or https
59+
if (!isHTTP(url))
60+
return false;
61+
62+
// make sure its a known youtube url
63+
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+
}
68+
3169
public static long parseDurationString(String input)
3270
throws ParsingException, NumberFormatException {
3371

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
22

3-
4-
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
53
import org.schabi.newpipe.extractor.exceptions.ParsingException;
6-
import org.schabi.newpipe.extractor.utils.Parser;
4+
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
5+
import org.schabi.newpipe.extractor.utils.Utils;
76

7+
import java.net.URL;
88
import java.util.List;
99

1010
public class YoutubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
1111

1212
private static final YoutubePlaylistLinkHandlerFactory instance = new YoutubePlaylistLinkHandlerFactory();
13-
private static final String ID_PATTERN = "([\\-a-zA-Z0-9_]{10,})";
1413

1514
public static YoutubePlaylistLinkHandlerFactory getInstance() {
1615
return instance;
@@ -24,17 +23,35 @@ public String getUrl(String id, List<String> contentFilters, String sortFilter)
2423
@Override
2524
public String getId(String url) throws ParsingException {
2625
try {
27-
return Parser.matchGroup1("list=" + ID_PATTERN, url);
26+
URL urlObj = Utils.stringToURL(url);
27+
28+
if (!YoutubeParsingHelper.isYoutubeURL(urlObj)) {
29+
throw new ParsingException("the url given is not a Youtube-URL");
30+
}
31+
32+
String listID = Utils.getQueryValue(urlObj, "list");
33+
34+
if (listID == null) {
35+
throw new ParsingException("the url given does not include a playlist");
36+
}
37+
38+
if (!listID.matches("[a-zA-Z0-9_-]{10,}")) {
39+
throw new ParsingException("the list-ID given in the URL does not match the list pattern");
40+
}
41+
42+
return listID;
2843
} catch (final Exception exception) {
2944
throw new ParsingException("Error could not parse url :" + exception.getMessage(), exception);
3045
}
3146
}
3247

33-
3448
@Override
3549
public boolean onAcceptUrl(final String url) {
36-
final boolean hasNotEmptyUrl = url != null && !url.isEmpty();
37-
final boolean isYoutubeDomain = hasNotEmptyUrl && (url.contains("youtube") || url.contains("youtu.be"));
38-
return isYoutubeDomain && url.contains("list=");
50+
try {
51+
getId(url);
52+
} catch (ParsingException e) {
53+
return false;
54+
}
55+
return true;
3956
}
4057
}

0 commit comments

Comments
 (0)