Skip to content

Commit 6aa69a2

Browse files
committed
Fix inconsistency in youtube channel urls
Urls from the youtube search extractor were "https://www.youtube.com/user/NAME" instead of "https://www.youtube.com/channel/ID". This fixes TeamNewPipe/NewPipe#2167
1 parent 5f65788 commit 6aa69a2

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
@SuppressWarnings("WeakerAccess")
4949
public class YoutubeChannelExtractor extends ChannelExtractor {
50+
/*package-private*/ static final String CHANNEL_URL_BASE = "https://www.youtube.com/channel/";
5051
private static final String CHANNEL_FEED_BASE = "https://www.youtube.com/feeds/videos.xml?channel_id=";
5152
private static final String CHANNEL_URL_PARAMETERS = "/videos?view=0&flow=list&sort=dd&live_view=10000";
5253

@@ -72,7 +73,7 @@ public String getNextPageUrl() throws ExtractionException {
7273
@Override
7374
public String getUrl() throws ParsingException {
7475
try {
75-
return "https://www.youtube.com/channel/" + getId();
76+
return CHANNEL_URL_BASE + getId();
7677
} catch (ParsingException e) {
7778
return super.getUrl();
7879
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelInfoItemExtractor.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.schabi.newpipe.extractor.exceptions.ParsingException;
66
import org.schabi.newpipe.extractor.utils.Utils;
77

8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
10+
811
/*
912
* Created by Christian Schabesberger on 12.02.17.
1013
*
@@ -53,8 +56,20 @@ public String getName() throws ParsingException {
5356

5457
@Override
5558
public String getUrl() throws ParsingException {
56-
return el.select("a[class*=\"yt-uix-tile-link\"]").first()
57-
.attr("abs:href");
59+
String buttonTrackingUrl = el.select("button[class*=\"yt-uix-button\"]").first()
60+
.attr("abs:data-href");
61+
62+
Pattern channelIdPattern = Pattern.compile("(?:.*?)\\%252Fchannel\\%252F(.+?)\\%26(?:.*)");
63+
Matcher match = channelIdPattern.matcher(buttonTrackingUrl);
64+
65+
if (match.matches()) {
66+
return YoutubeChannelExtractor.CHANNEL_URL_BASE + match.group(1);
67+
} else {
68+
// fallback method just in case youtube changes things; it should never run and tests will fail
69+
// provides an url with "/user/NAME", that is inconsistent with stream and channel extractor
70+
return el.select("a[class*=\"yt-uix-tile-link\"]").first()
71+
.attr("abs:href");
72+
}
5873
}
5974

6075
@Override

0 commit comments

Comments
 (0)