Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public String getUrl(String id, List<String> contentFilters, String searchFilter
return "https://www.youtube.com/" + id;
}

/**
* Returns true if path conform to
* custom short channel URLs like youtube.com/yourcustomname
*
* @param splitPath path segments array
* @return true - if value conform to short channel URL, false - not
*/
private boolean isCustomShortChannelUrl(String[] splitPath) {
return splitPath.length == 1 &&
!splitPath[0].matches("playlist|watch|attribution_link|watch_popup|embed|feed|select_site");
}

@Override
public String getId(String url) throws ParsingException {
try {
Expand All @@ -60,14 +72,20 @@ public String getId(String url) throws ParsingException {
throw new ParsingException("the URL given is not a Youtube-URL");
}

if (!path.startsWith("/user/") && !path.startsWith("/channel/") && !path.startsWith("/c/")) {
throw new ParsingException("the URL given is neither a channel nor an user");
}

// remove leading "/"
path = path.substring(1);

String[] splitPath = path.split("/");

// Handle custom short channel URLs like youtube.com/yourcustomname
if (isCustomShortChannelUrl(splitPath)) {
path = "c/" + path;
splitPath = path.split("/");
}

if (!path.startsWith("user/") && !path.startsWith("channel/") && !path.startsWith("c/")) {
throw new ParsingException("the URL given is neither a channel nor an user");
}

String id = splitPath[1];

if (id == null || !id.matches("[A-Za-z0-9_-]+")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
Expand All @@ -30,6 +31,8 @@ public void acceptUrlTest() throws ParsingException {

assertTrue(linkHandler.acceptUrl("https://www.youtube.com/c/creatoracademy"));

assertTrue(linkHandler.acceptUrl("https://youtube.com/DIMENSI0N"));

assertTrue(linkHandler.acceptUrl("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA"));
assertTrue(linkHandler.acceptUrl("https://www.youtube.com/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1"));

Expand All @@ -44,6 +47,18 @@ public void acceptUrlTest() throws ParsingException {

assertTrue(linkHandler.acceptUrl("https://invidio.us/channel/UClq42foiSgl7sSpLupnugGA"));
assertTrue(linkHandler.acceptUrl("https://invidio.us/channel/UClq42foiSgl7sSpLupnugGA/videos?disable_polymer=1"));
assertTrue(linkHandler.acceptUrl("https://www.youtube.com/watchismo"));


// do not accept URLs which are not channels
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/watch?v=jZViOEv90dI&t=100"));
assertFalse(linkHandler.acceptUrl("http://www.youtube.com/watch_popup?v=uEJuoEs1UxY"));
assertFalse(linkHandler.acceptUrl("http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare"));
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/playlist?list=PLW5y1tjAOzI3orQNF1yGGVL5x-pR2K1d"));
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/embed/jZViOEv90dI"));
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/feed/subscriptions?list=PLz8YL4HVC87WJQDzVoY943URKQCsHS9XV"));
assertFalse(linkHandler.acceptUrl("https://www.youtube.com/?app=desktop&persist_app=1"));
assertFalse(linkHandler.acceptUrl("https://m.youtube.com/select_site"));
}

@Test
Expand Down