Skip to content

Commit b086e9d

Browse files
committed
[YouTube] Fix id extraction for some channels
Some channels had no reliable way to get the redirected id in the response, so saving it for later was a valid alternative.
1 parent 00d1ed4 commit b086e9d

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
4646
private JsonObject initialData;
4747
private JsonObject videoTab;
4848

49+
/**
50+
* Some channels have response redirects and the only way to reliably get the id is by saving it.
51+
*<p>
52+
* "Movies & Shows":
53+
* <pre>
54+
* UCuJcl0Ju-gPDoksRjK1ya-w ┐
55+
* UChBfWrfBXL9wS6tQtgjt_OQ ├ UClgRkhTL3_hImCAmdLfDE4g
56+
* UCok7UTQQEP1Rsctxiv3gwSQ ┘
57+
* </pre>
58+
*/
59+
private String redirectedChannelId;
60+
4961
public YoutubeChannelExtractor(StreamingService service, ListLinkHandler linkHandler) {
5062
super(service, linkHandler);
5163
}
@@ -80,6 +92,7 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr
8092
}
8193

8294
url = "https://www.youtube.com/channel/" + browseId + "/videos?pbj=1&view=0&flow=grid";
95+
redirectedChannelId = browseId;
8396
level++;
8497
} else {
8598
ajaxJson = jsonResponse;
@@ -117,10 +130,17 @@ public String getUrl() throws ParsingException {
117130
@Nonnull
118131
@Override
119132
public String getId() throws ParsingException {
120-
try {
121-
return initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getString("channelId");
122-
} catch (Exception e) {
123-
throw new ParsingException("Could not get channel id", e);
133+
final String channelId = initialData
134+
.getObject("header", EMPTY_OBJECT)
135+
.getObject("c4TabbedHeaderRenderer", EMPTY_OBJECT)
136+
.getString("channelId", EMPTY_STRING);
137+
138+
if (!channelId.isEmpty()) {
139+
return channelId;
140+
} else if (redirectedChannelId != null && !redirectedChannelId.isEmpty()) {
141+
return redirectedChannelId;
142+
} else {
143+
throw new ParsingException("Could not get channel id");
124144
}
125145
}
126146

0 commit comments

Comments
 (0)