From ce7cbbc9a0e7d9fe99319600316a467c92632367 Mon Sep 17 00:00:00 2001 From: Stypox Date: Wed, 24 Mar 2021 09:04:43 +0100 Subject: [PATCH] [YouTube] Fix trending getName() --- .../services/youtube/YoutubeParsingHelper.java | 10 ++++++++++ .../extractors/YoutubeTrendingExtractor.java | 14 +++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java index 4368e1ce8..377cec9ae 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java @@ -571,6 +571,16 @@ public class YoutubeParsingHelper { return getTextFromObject(textObject, false); } + @Nullable + public static String getTextAtKey(final JsonObject jsonObject, final String key) + throws ParsingException { + if (jsonObject.isString(key)) { + return jsonObject.getString(key); + } else { + return getTextFromObject(jsonObject.getObject(key)); + } + } + public static String fixThumbnailUrl(String thumbnailUrl) { if (thumbnailUrl.startsWith("//")) { thumbnailUrl = thumbnailUrl.substring(2); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java index 613169c7a..f9316216a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java @@ -39,6 +39,7 @@ import java.io.IOException; import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse; +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextAtKey; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; @@ -70,18 +71,17 @@ public class YoutubeTrendingExtractor extends KioskExtractor { @Override public String getName() throws ParsingException { final JsonObject header = initialData.getObject("header"); - JsonObject title = null; + String name = null; if (header.has("feedTabbedHeaderRenderer")) { - title = header.getObject("feedTabbedHeaderRenderer").getObject("title"); + name = getTextAtKey(header.getObject("feedTabbedHeaderRenderer"), "title"); } else if (header.has("c4TabbedHeaderRenderer")) { - title = header.getObject("c4TabbedHeaderRenderer").getObject("title"); + name = getTextAtKey(header.getObject("c4TabbedHeaderRenderer"), "title"); } - String name = getTextFromObject(title); - if (!isNullOrEmpty(name)) { - return name; + if (isNullOrEmpty(name)) { + throw new ParsingException("Could not get Trending name"); } - throw new ParsingException("Could not get Trending name"); + return name; } @Nonnull