From 3760bd70a88b49b456840ae655598a23892f3eca Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sun, 24 Mar 2024 00:17:22 +0100 Subject: [PATCH] [YouTube] Add base implementation for show InfoItems As there are multiple show UI elements which share a lot of common data, a base implementation, an abstract class named YoutubeBaseShowInfoItemExtractor, has been created to handle common cases. --- .../YoutubeBaseShowInfoItemExtractor.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeBaseShowInfoItemExtractor.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeBaseShowInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeBaseShowInfoItemExtractor.java new file mode 100644 index 000000000..67254302f --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeBaseShowInfoItemExtractor.java @@ -0,0 +1,65 @@ +package org.schabi.newpipe.extractor.services.youtube.extractors; + +import com.grack.nanojson.JsonObject; +import org.schabi.newpipe.extractor.Image; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor; +import org.schabi.newpipe.extractor.utils.Utils; + +import javax.annotation.Nonnull; +import java.util.List; + +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailsFromInfoItem; +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint; + +/** + * The base {@link PlaylistInfoItemExtractor} for shows playlists UI elements. + */ +abstract class YoutubeBaseShowInfoItemExtractor implements PlaylistInfoItemExtractor { + + @Nonnull + protected final JsonObject showRenderer; + + YoutubeBaseShowInfoItemExtractor(@Nonnull final JsonObject showRenderer) { + this.showRenderer = showRenderer; + } + + @Override + public String getName() throws ParsingException { + return showRenderer.getString("title"); + } + + @Override + public String getUrl() throws ParsingException { + return getUrlFromNavigationEndpoint(showRenderer.getObject("navigationEndpoint")); + } + + @Nonnull + @Override + public List getThumbnails() throws ParsingException { + return getThumbnailsFromInfoItem(showRenderer.getObject("thumbnailRenderer") + .getObject("showCustomThumbnailRenderer")); + } + + @Override + public long getStreamCount() throws ParsingException { + // The stream count should be always returned in the first text object for English + // localizations, but the complete text is parsed for reliability purposes + final String streamCountText = getTextFromObject( + showRenderer.getObject("thumbnailOverlays") + .getObject("thumbnailOverlayBottomPanelRenderer") + .getObject("text")); + if (streamCountText == null) { + throw new ParsingException("Could not get stream count"); + } + + try { + // The data returned could be a human/shortened number, but no show with more than 1000 + // videos has been found at the time this code was written + return Long.parseLong(Utils.removeNonDigitCharacters(streamCountText)); + } catch (final NumberFormatException e) { + throw new ParsingException("Could not convert stream count to a long", e); + } + } +}