Skip to content

Commit e4e5ccc

Browse files
authored
Merge pull request #1442 from G-flat/support_more_yt_channels
[YouTube] Support more YouTube channel URLs
2 parents 48d27be + 9831bcc commit e4e5ccc

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public String getUrl(final String id,
7171
* @return whether the value conform to short channel URLs
7272
*/
7373
private boolean isCustomShortChannelUrl(@Nonnull final String[] splitPath) {
74-
return splitPath.length == 1 && !EXCLUDED_SEGMENTS.matcher(splitPath[0]).matches();
74+
return splitPath.length == 1 && !splitPath[0].isEmpty()
75+
&& !EXCLUDED_SEGMENTS.matcher(splitPath[0]).matches();
7576
}
7677

7778
/**
@@ -99,15 +100,12 @@ public String getId(final String url) throws ParsingException, UnsupportedOperat
99100
// Remove leading "/"
100101
path = path.substring(1);
101102

102-
String[] splitPath = path.split("/");
103+
final String[] splitPath = path.split("/");
103104

104-
if (isHandle(splitPath)) {
105-
// Handle YouTube handle URLs like youtube.com/@yourhandle
105+
if (isHandle(splitPath) || isCustomShortChannelUrl(splitPath)) {
106+
// Handle YouTube handle URLs like youtube.com/@yourhandle and
107+
// custom short channel URLs like youtube.com/yourcustomname
106108
return splitPath[0];
107-
} else if (isCustomShortChannelUrl(splitPath)) {
108-
// Handle custom short channel URLs like youtube.com/yourcustomname
109-
path = "c/" + path;
110-
splitPath = path.split("/");
111109
}
112110

113111
if (!path.startsWith("user/") && !path.startsWith("channel/")

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLinkHandlerFactoryTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ void acceptUrlTest() throws ParsingException {
5353

5454
// do not accept URLs which are not channels
5555
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/watch?v=jZViOEv90dI&t=100"));
56+
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/watch"));
5657
assertFalse(linkHandler.acceptUrl("http://www.youtube.com/watch_popup?v=uEJuoEs1UxY"));
58+
assertFalse(linkHandler.acceptUrl("http://www.youtube.com/watch_popup"));
5759
assertFalse(linkHandler.acceptUrl("http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare"));
60+
assertFalse(linkHandler.acceptUrl("http://www.youtube.com/attribution_link"));
5861
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/playlist?list=PLW5y1tjAOzI3orQNF1yGGVL5x-pR2K1d"));
62+
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/playlist"));
5963
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/embed/jZViOEv90dI"));
64+
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/embed"));
6065
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/feed/subscriptions?list=PLz8YL4HVC87WJQDzVoY943URKQCsHS9XV"));
61-
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/?app=desktop&persist_app=1"));
66+
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/feed"));
67+
assertFalse(linkHandler.acceptUrl("https://www.youtube.com"));
6268
assertFalse(linkHandler.acceptUrl("https://m.youtube.com/select_site"));
6369
}
6470

@@ -88,5 +94,8 @@ void getIdFromUrl() throws ParsingException {
8894

8995
assertEquals("@Gronkh", linkHandler.fromUrl("https://www.youtube.com/@Gronkh?ucbcb=1").getId());
9096
assertEquals("@YouTubeCreators", linkHandler.fromUrl("https://www.youtube.com/@YouTubeCreators/shorts").getId());
97+
98+
assertEquals("PewDiePie", linkHandler.fromUrl("https://www.youtube.com/PewDiePie").getId());
99+
assertEquals("DreamTraps", linkHandler.fromUrl("https://www.youtube.com/DreamTraps").getId());
91100
}
92101
}

0 commit comments

Comments
 (0)