Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support short custom YouTube channel URLs #409

Merged
merged 9 commits into from
Oct 25, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ 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
Bartoshr marked this conversation as resolved.
Show resolved Hide resolved
*/
public boolean isCustomShortChannelUrl(String[] splitPath) {
TobiGr marked this conversation as resolved.
Show resolved Hide resolved
return splitPath.length == 1 && !splitPath[0].matches("playlist|watch");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Thank you for review. Do you think that simply adding "attribution_link" to exlude those links with regex would be sufficent solution ? I couldn't come up with something better that that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmh, probably a list of all pages not pointing to a channel should be added to that regex

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@Override
public String getId(String url) throws ParsingException {
try {
Expand All @@ -60,14 +71,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
Bartoshr marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -30,6 +30,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 Down