From b086e9db3fc73c631da8c7c35974b06b3d5a5392 Mon Sep 17 00:00:00 2001 From: Mauricio Colli Date: Sat, 14 Mar 2020 02:34:29 -0300 Subject: [PATCH] [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. --- .../extractors/YoutubeChannelExtractor.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java index 1aeb65ed4..385980745 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java @@ -46,6 +46,18 @@ public class YoutubeChannelExtractor extends ChannelExtractor { private JsonObject initialData; private JsonObject videoTab; + /** + * Some channels have response redirects and the only way to reliably get the id is by saving it. + *

+ * "Movies & Shows": + *

+     * UCuJcl0Ju-gPDoksRjK1ya-w ┐
+     * UChBfWrfBXL9wS6tQtgjt_OQ ├ UClgRkhTL3_hImCAmdLfDE4g
+     * UCok7UTQQEP1Rsctxiv3gwSQ ┘
+     * 
+ */ + private String redirectedChannelId; + public YoutubeChannelExtractor(StreamingService service, ListLinkHandler linkHandler) { super(service, linkHandler); } @@ -80,6 +92,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { } url = "https://www.youtube.com/channel/" + browseId + "/videos?pbj=1&view=0&flow=grid"; + redirectedChannelId = browseId; level++; } else { ajaxJson = jsonResponse; @@ -117,10 +130,17 @@ public class YoutubeChannelExtractor extends ChannelExtractor { @Nonnull @Override public String getId() throws ParsingException { - try { - return initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getString("channelId"); - } catch (Exception e) { - throw new ParsingException("Could not get channel id", e); + final String channelId = initialData + .getObject("header", EMPTY_OBJECT) + .getObject("c4TabbedHeaderRenderer", EMPTY_OBJECT) + .getString("channelId", EMPTY_STRING); + + if (!channelId.isEmpty()) { + return channelId; + } else if (redirectedChannelId != null && !redirectedChannelId.isEmpty()) { + return redirectedChannelId; + } else { + throw new ParsingException("Could not get channel id"); } }