Skip to content

Commit 34547a3

Browse files
committed
Fix YouTubeChannelExtractor
It was given some inconsistent results (/user and /channel), now it only returns /channel urls don't matter what the original is (at least when calling the getCleanUrl() method).
1 parent a8a4eaf commit 34547a3

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,32 @@ public YoutubeChannelExtractor(StreamingService service, String url, String next
5959
public void fetchPage() throws IOException, ExtractionException {
6060
Downloader downloader = NewPipe.getDownloader();
6161

62-
String channelUrl = getCleanUrl() + CHANNEL_URL_PARAMETERS;
62+
String channelUrl = super.getCleanUrl() + CHANNEL_URL_PARAMETERS;
6363
String pageContent = downloader.download(channelUrl);
6464
doc = Jsoup.parse(pageContent, channelUrl);
6565

6666
nextStreamsUrl = getNextStreamsUrlFrom(doc);
6767
nextStreamsAjax = null;
6868
}
6969

70+
@Override
71+
public String getCleanUrl() {
72+
try {
73+
return "https://www.youtube.com/channel/" + getId();
74+
} catch (ParsingException e) {
75+
return super.getCleanUrl();
76+
}
77+
}
78+
7079
@Override
7180
public String getId() throws ParsingException {
7281
try {
73-
return getUrlIdHandler().getId(getCleanUrl());
82+
Element element = doc.getElementsByClass("yt-uix-subscription-button").first();
83+
if (element == null) element = doc.getElementsByClass("yt-uix-subscription-preferences-button").first();
84+
85+
return element.attr("data-channel-external-id");
7486
} catch (Exception e) {
75-
throw new ParsingException("Could not get channel id");
87+
throw new ParsingException("Could not get channel id", e);
7688
}
7789
}
7890

@@ -110,8 +122,7 @@ public String getBannerUrl() throws ParsingException {
110122
@Override
111123
public String getFeedUrl() throws ParsingException {
112124
try {
113-
String channelId = doc.getElementsByClass("yt-uix-subscription-button").first().attr("data-channel-external-id");
114-
return channelId == null ? "" : CHANNEL_FEED_BASE + channelId;
125+
return CHANNEL_FEED_BASE + getId();
115126
} catch (Exception e) {
116127
throw new ParsingException("Could not get feed url", e);
117128
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class YoutubeChannelExtractorTest {
4141
public void setUp() throws Exception {
4242
NewPipe.init(Downloader.getInstance());
4343
extractor = YouTube.getService()
44-
.getChannelExtractor("https://www.youtube.com/channel/UCYJ61XIK64sp6ZFFS8sctxw");
44+
.getChannelExtractor("https://www.youtube.com/user/Gronkh");
4545
}
4646

4747
@Test
@@ -54,6 +54,16 @@ public void testGetName() throws Exception {
5454
assertEquals(extractor.getName(), "Gronkh");
5555
}
5656

57+
@Test
58+
public void testGetId() throws Exception {
59+
assertEquals(extractor.getId(), "UCYJ61XIK64sp6ZFFS8sctxw");
60+
}
61+
62+
@Test
63+
public void testGetUrl() throws Exception {
64+
assertEquals(extractor.getCleanUrl(), "https://www.youtube.com/channel/UCYJ61XIK64sp6ZFFS8sctxw");
65+
}
66+
5767
@Test
5868
public void testGetDescription() throws Exception {
5969
assertEquals(extractor.getDescription(), "★ ★ ★ KLICK MICH HART, DU SAU! :D ★ ★ ★ Zart im Schmelz und süffig im Abgang. Ungebremster Spieltrieb seit 1896. Tägliche Folgen nonstop seit dem 01.04.2010!...");
@@ -71,7 +81,7 @@ public void testGetBannerUrl() throws Exception {
7181

7282
@Test
7383
public void testGetFeedUrl() throws Exception {
74-
assertTrue(extractor.getFeedUrl(), extractor.getFeedUrl().contains("feed"));
84+
assertEquals(extractor.getFeedUrl(), "https://www.youtube.com/feeds/videos.xml?channel_id=UCYJ61XIK64sp6ZFFS8sctxw");
7585
}
7686

7787
@Test

0 commit comments

Comments
 (0)