Skip to content

Commit 216a4eb

Browse files
committed
Complete fix inconsistency in youtube channel urls
It is not always possible to get the url in the form "https://www.youtube.com/channel/...", so a not has been added whenever that happens to be the case (i.e. only in InfoStreamItems).
1 parent 315c5c2 commit 216a4eb

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public String getName() throws ParsingException {
5050
try {
5151
return doc.select("div[id=pl-header] h1[class=pl-header-title]").first().text();
5252
} catch (Exception e) {
53-
throw new ParsingException("Could not get playlist name");
53+
throw new ParsingException("Could not get playlist name", e);
5454
}
5555
}
5656

@@ -59,7 +59,7 @@ public String getThumbnailUrl() throws ParsingException {
5959
try {
6060
return doc.select("div[id=pl-header] div[class=pl-header-thumb] img").first().attr("abs:src");
6161
} catch (Exception e) {
62-
throw new ParsingException("Could not get playlist thumbnail");
62+
throw new ParsingException("Could not get playlist thumbnail", e);
6363
}
6464
}
6565

@@ -72,9 +72,11 @@ public String getBannerUrl() {
7272
@Override
7373
public String getUploaderUrl() throws ParsingException {
7474
try {
75-
return doc.select("ul[class=\"pl-header-details\"] li").first().select("a").first().attr("abs:href");
75+
return YoutubeChannelExtractor.CHANNEL_URL_BASE +
76+
doc.select("button[class*=\"yt-uix-subscription-button\"]")
77+
.first().attr("data-channel-external-id");
7678
} catch (Exception e) {
77-
throw new ParsingException("Could not get playlist uploader name");
79+
throw new ParsingException("Could not get playlist uploader url", e);
7880
}
7981
}
8082

@@ -83,7 +85,7 @@ public String getUploaderName() throws ParsingException {
8385
try {
8486
return doc.select("span[class=\"qualified-channel-title-text\"]").first().select("a").first().text();
8587
} catch (Exception e) {
86-
throw new ParsingException("Could not get playlist uploader name");
88+
throw new ParsingException("Could not get playlist uploader name", e);
8789
}
8890
}
8991

@@ -92,7 +94,7 @@ public String getUploaderAvatarUrl() throws ParsingException {
9294
try {
9395
return doc.select("div[id=gh-banner] img[class=channel-header-profile-image]").first().attr("abs:src");
9496
} catch (Exception e) {
95-
throw new ParsingException("Could not get playlist uploader avatar");
97+
throw new ParsingException("Could not get playlist uploader avatar", e);
9698
}
9799
}
98100

@@ -248,6 +250,8 @@ public String getUploaderName() throws ParsingException {
248250

249251
@Override
250252
public String getUploaderUrl() throws ParsingException {
253+
// this url is not always in the form "/channel/..."
254+
// sometimes Youtube provides urls in the from "/user/..."
251255
return getUploaderLink().attr("abs:href");
252256
}
253257

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public String getUploaderName() throws ParsingException {
107107

108108
@Override
109109
public String getUploaderUrl() throws ParsingException {
110+
// this url is not always in the form "/channel/..."
111+
// sometimes Youtube provides urls in the from "/user/..."
110112
try {
111113
try {
112114
return item.select("div[class=\"yt-lockup-byline\"]").first()
@@ -119,7 +121,7 @@ public String getUploaderUrl() throws ParsingException {
119121
.text().split(" - ")[0];
120122
} catch (Exception e) {
121123
System.out.println(item.html());
122-
throw new ParsingException("Could not get uploader", e);
124+
throw new ParsingException("Could not get uploader url", e);
123125
}
124126
}
125127

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ public String getUploaderUrl() throws ParsingException {
126126
}
127127

128128
private Element getUploaderLink() {
129+
// this url is not always in the form "/channel/..."
130+
// sometimes Youtube provides urls in the from "/user/..."
129131
Element uploaderEl = el.select("div[class*=\"yt-lockup-byline \"]").first();
130132
return uploaderEl.select("a").first();
131133
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void testBannerUrl() throws Exception {
100100

101101
@Test
102102
public void testUploaderUrl() throws Exception {
103-
assertTrue(extractor.getUploaderUrl().contains("youtube.com"));
103+
assertEquals("https://www.youtube.com/channel/UCs72iRpTEuwV3y6pdWYLgiw", extractor.getUploaderUrl());
104104
}
105105

106106
@Test
@@ -185,8 +185,8 @@ public void testRelatedItems() throws Exception {
185185
public void testMoreRelatedItems() throws Exception {
186186
ListExtractor.InfoItemsPage<StreamInfoItem> currentPage
187187
= defaultTestMoreItems(extractor, ServiceList.YouTube.getServiceId());
188-
// Test for 2 more levels
189188

189+
// test for 2 more levels
190190
for (int i = 0; i < 2; i++) {
191191
currentPage = extractor.getPage(currentPage.getNextPageUrl());
192192
defaultTestListOfItems(YouTube.getServiceId(), currentPage.getItems(), currentPage.getErrors());
@@ -214,7 +214,7 @@ public void testBannerUrl() throws Exception {
214214

215215
@Test
216216
public void testUploaderUrl() throws Exception {
217-
assertTrue(extractor.getUploaderUrl().contains("youtube.com"));
217+
assertEquals("https://www.youtube.com/channel/UCHSPWoY1J5fbDVbcnyeqwdw", extractor.getUploaderUrl());
218218
}
219219

220220
@Test

0 commit comments

Comments
 (0)