|
| 1 | +package org.schabi.newpipe.extractor.services.youtube.extractors; |
| 2 | + |
| 3 | +import com.grack.nanojson.JsonObject; |
| 4 | +import org.schabi.newpipe.extractor.Image; |
| 5 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 6 | +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor; |
| 7 | +import org.schabi.newpipe.extractor.utils.Utils; |
| 8 | + |
| 9 | +import javax.annotation.Nonnull; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; |
| 13 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailsFromInfoItem; |
| 14 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint; |
| 15 | + |
| 16 | +/** |
| 17 | + * The base {@link PlaylistInfoItemExtractor} for shows playlists UI elements. |
| 18 | + */ |
| 19 | +abstract class YoutubeBaseShowInfoItemExtractor implements PlaylistInfoItemExtractor { |
| 20 | + |
| 21 | + @Nonnull |
| 22 | + protected final JsonObject showRenderer; |
| 23 | + |
| 24 | + YoutubeBaseShowInfoItemExtractor(@Nonnull final JsonObject showRenderer) { |
| 25 | + this.showRenderer = showRenderer; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getName() throws ParsingException { |
| 30 | + return showRenderer.getString("title"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getUrl() throws ParsingException { |
| 35 | + return getUrlFromNavigationEndpoint(showRenderer.getObject("navigationEndpoint")); |
| 36 | + } |
| 37 | + |
| 38 | + @Nonnull |
| 39 | + @Override |
| 40 | + public List<Image> getThumbnails() throws ParsingException { |
| 41 | + return getThumbnailsFromInfoItem(showRenderer.getObject("thumbnailRenderer") |
| 42 | + .getObject("showCustomThumbnailRenderer")); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public long getStreamCount() throws ParsingException { |
| 47 | + // The stream count should be always returned in the first text object for English |
| 48 | + // localizations, but the complete text is parsed for reliability purposes |
| 49 | + final String streamCountText = getTextFromObject( |
| 50 | + showRenderer.getObject("thumbnailOverlays") |
| 51 | + .getObject("thumbnailOverlayBottomPanelRenderer") |
| 52 | + .getObject("text")); |
| 53 | + if (streamCountText == null) { |
| 54 | + throw new ParsingException("Could not get stream count"); |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + // The data returned could be a human/shortened number, but no show with more than 1000 |
| 59 | + // videos has been found at the time this code was written |
| 60 | + return Long.parseLong(Utils.removeNonDigitCharacters(streamCountText)); |
| 61 | + } catch (final NumberFormatException e) { |
| 62 | + throw new ParsingException("Could not convert stream count to a long", e); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments