From 9d5201f40e3ff52f053a2182f343050aef182865 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sun, 24 Mar 2024 00:19:30 +0100 Subject: [PATCH] [YouTube] Add support for showRenderers in search results --- .../extractors/YoutubeSearchExtractor.java | 11 +++- .../YoutubeShowRendererInfoItemExtractor.java | 57 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeShowRendererInfoItemExtractor.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java index 491d53164..a0be72cc8 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java @@ -238,9 +238,14 @@ public class YoutubeSearchExtractor extends SearchExtractor { } else if (extractChannelResults && item.has("channelRenderer")) { collector.commit(new YoutubeChannelInfoItemExtractor( item.getObject("channelRenderer"))); - } else if (extractPlaylistResults && item.has("playlistRenderer")) { - collector.commit(new YoutubePlaylistInfoItemExtractor( - item.getObject("playlistRenderer"))); + } else if (extractPlaylistResults) { + if (item.has("playlistRenderer")) { + collector.commit(new YoutubePlaylistInfoItemExtractor( + item.getObject("playlistRenderer"))); + } else if (item.has("showRenderer")) { + collector.commit(new YoutubeShowRendererInfoItemExtractor( + item.getObject("showRenderer"))); + } } } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeShowRendererInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeShowRendererInfoItemExtractor.java new file mode 100644 index 000000000..c7119907e --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeShowRendererInfoItemExtractor.java @@ -0,0 +1,57 @@ +package org.schabi.newpipe.extractor.services.youtube.extractors; + +import com.grack.nanojson.JsonObject; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +import javax.annotation.Nonnull; + +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromObject; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + +/** + * A {@link YoutubeBaseShowInfoItemExtractor} implementation for {@code showRenderer}s. + */ +class YoutubeShowRendererInfoItemExtractor extends YoutubeBaseShowInfoItemExtractor { + + @Nonnull + private final JsonObject shortBylineText; + @Nonnull + private final JsonObject longBylineText; + + YoutubeShowRendererInfoItemExtractor(@Nonnull final JsonObject showRenderer) { + super(showRenderer); + this.shortBylineText = showRenderer.getObject("shortBylineText"); + this.longBylineText = showRenderer.getObject("longBylineText"); + } + + @Override + public String getUploaderName() throws ParsingException { + String name = getTextFromObject(longBylineText); + if (isNullOrEmpty(name)) { + name = getTextFromObject(shortBylineText); + if (isNullOrEmpty(name)) { + throw new ParsingException("Could not get uploader name"); + } + } + return name; + } + + @Override + public String getUploaderUrl() throws ParsingException { + String uploaderUrl = getUrlFromObject(longBylineText); + if (uploaderUrl == null) { + uploaderUrl = getUrlFromObject(shortBylineText); + if (uploaderUrl == null) { + throw new ParsingException("Could not get uploader URL"); + } + } + return uploaderUrl; + } + + @Override + public boolean isUploaderVerified() throws ParsingException { + // We do not have this information in showRenderers + return false; + } +}