|
| 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.ListExtractor; |
| 6 | +import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 7 | +import org.schabi.newpipe.extractor.playlist.PlaylistInfo; |
| 8 | +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor; |
| 9 | +import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory; |
| 10 | +import org.schabi.newpipe.extractor.utils.Utils; |
| 11 | + |
| 12 | +import javax.annotation.Nonnull; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractPlaylistTypeFromPlaylistId; |
| 16 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getImagesFromThumbnailsArray; |
| 17 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint; |
| 18 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.hasArtistOrVerifiedIconBadgeAttachment; |
| 19 | + |
| 20 | +public class YoutubeMixOrPlaylistLockupInfoItemExtractor implements PlaylistInfoItemExtractor { |
| 21 | + |
| 22 | + @Nonnull |
| 23 | + private final JsonObject lockupViewModel; |
| 24 | + @Nonnull |
| 25 | + private final JsonObject thumbnailViewModel; |
| 26 | + @Nonnull |
| 27 | + private final JsonObject lockupMetadataViewModel; |
| 28 | + @Nonnull |
| 29 | + private final JsonObject firstMetadataRow; |
| 30 | + @Nonnull |
| 31 | + private PlaylistInfo.PlaylistType playlistType; |
| 32 | + |
| 33 | + public YoutubeMixOrPlaylistLockupInfoItemExtractor(@Nonnull final JsonObject lockupViewModel) { |
| 34 | + this.lockupViewModel = lockupViewModel; |
| 35 | + this.thumbnailViewModel = lockupViewModel.getObject("contentImage") |
| 36 | + .getObject("collectionThumbnailViewModel") |
| 37 | + .getObject("primaryThumbnail") |
| 38 | + .getObject("thumbnailViewModel"); |
| 39 | + this.lockupMetadataViewModel = lockupViewModel.getObject("metadata") |
| 40 | + .getObject("lockupMetadataViewModel"); |
| 41 | + /* |
| 42 | + The metadata rows are structured in the following way: |
| 43 | + 1st part: uploader info, playlist type, playlist updated date |
| 44 | + 2nd part: space row |
| 45 | + 3rd element: first video |
| 46 | + 4th (not always returned for playlists with less than 2 items?): second video |
| 47 | + 5th element (always returned, but at a different index for playlists with less than 2 |
| 48 | + items?): Show full playlist |
| 49 | +
|
| 50 | + The first metadata row has the following structure: |
| 51 | + 1st array element: uploader info |
| 52 | + 2nd element: playlist type (course, playlist, podcast) |
| 53 | + 3rd element (not always returned): playlist updated date |
| 54 | + */ |
| 55 | + this.firstMetadataRow = lockupMetadataViewModel.getObject("metadata") |
| 56 | + .getObject("contentMetadataViewModel") |
| 57 | + .getArray("metadataRows") |
| 58 | + .getObject(0); |
| 59 | + |
| 60 | + try { |
| 61 | + this.playlistType = extractPlaylistTypeFromPlaylistId(getPlaylistId()); |
| 62 | + } catch (final ParsingException e) { |
| 63 | + // If we cannot extract the playlist type, fall back to the normal one |
| 64 | + this.playlistType = PlaylistInfo.PlaylistType.NORMAL; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public String getUploaderName() throws ParsingException { |
| 70 | + return firstMetadataRow.getArray("metadataParts") |
| 71 | + .getObject(0) |
| 72 | + .getObject("text") |
| 73 | + .getString("content"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String getUploaderUrl() throws ParsingException { |
| 78 | + if (playlistType != PlaylistInfo.PlaylistType.NORMAL) { |
| 79 | + // If the playlist is a mix, there is no uploader as they are auto-generated |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + return getUrlFromNavigationEndpoint( |
| 84 | + firstMetadataRow.getArray("metadataParts") |
| 85 | + .getObject(0) |
| 86 | + .getObject("text") |
| 87 | + .getArray("commandRuns") |
| 88 | + .getObject(0) |
| 89 | + .getObject("onTap") |
| 90 | + .getObject("innertubeCommand")); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean isUploaderVerified() throws ParsingException { |
| 95 | + if (playlistType != PlaylistInfo.PlaylistType.NORMAL) { |
| 96 | + // If the playlist is a mix, there is no uploader as they are auto-generated |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return hasArtistOrVerifiedIconBadgeAttachment( |
| 101 | + firstMetadataRow.getArray("metadataParts") |
| 102 | + .getObject(0) |
| 103 | + .getObject("text") |
| 104 | + .getArray("attachmentRuns")); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public long getStreamCount() throws ParsingException { |
| 109 | + if (playlistType != PlaylistInfo.PlaylistType.NORMAL) { |
| 110 | + // If the playlist is a mix, we are not able to get its stream count |
| 111 | + return ListExtractor.ITEM_COUNT_INFINITE; |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + return Long.parseLong(Utils.removeNonDigitCharacters( |
| 116 | + thumbnailViewModel.getArray("overlays") |
| 117 | + .stream() |
| 118 | + .filter(JsonObject.class::isInstance) |
| 119 | + .map(JsonObject.class::cast) |
| 120 | + .filter(overlay -> overlay.has("thumbnailOverlayBadgeViewModel")) |
| 121 | + .findFirst() |
| 122 | + .orElseThrow(() -> new ParsingException( |
| 123 | + "Could not get thumbnailOverlayBadgeViewModel")) |
| 124 | + .getObject("thumbnailOverlayBadgeViewModel") |
| 125 | + .getArray("thumbnailBadges") |
| 126 | + .stream() |
| 127 | + .filter(JsonObject.class::isInstance) |
| 128 | + .map(JsonObject.class::cast) |
| 129 | + .filter(badge -> badge.has("thumbnailBadgeViewModel")) |
| 130 | + .findFirst() |
| 131 | + .orElseThrow(() -> |
| 132 | + new ParsingException("Could not get thumbnailBadgeViewModel")) |
| 133 | + .getObject("thumbnailBadgeViewModel") |
| 134 | + .getString("text"))); |
| 135 | + } catch (final Exception e) { |
| 136 | + throw new ParsingException("Could not get playlist stream count", e); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getName() throws ParsingException { |
| 142 | + return lockupMetadataViewModel.getObject("title") |
| 143 | + .getString("content"); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public String getUrl() throws ParsingException { |
| 148 | + // If the playlist item is a mix, we cannot return just its playlist ID as mix playlists |
| 149 | + // are not viewable in playlist pages |
| 150 | + // Use directly getUrlFromNavigationEndpoint in this case, which returns the watch URL with |
| 151 | + // the mix playlist |
| 152 | + if (playlistType == PlaylistInfo.PlaylistType.NORMAL) { |
| 153 | + try { |
| 154 | + return YoutubePlaylistLinkHandlerFactory.getInstance().getUrl(getPlaylistId()); |
| 155 | + } catch (final Exception ignored) { |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return getUrlFromNavigationEndpoint(lockupViewModel.getObject("rendererContext") |
| 160 | + .getObject("commandContext") |
| 161 | + .getObject("onTap") |
| 162 | + .getObject("innertubeCommand")); |
| 163 | + } |
| 164 | + |
| 165 | + @Nonnull |
| 166 | + @Override |
| 167 | + public List<Image> getThumbnails() throws ParsingException { |
| 168 | + return getImagesFromThumbnailsArray(thumbnailViewModel.getObject("image") |
| 169 | + .getArray("sources")); |
| 170 | + } |
| 171 | + |
| 172 | + @Nonnull |
| 173 | + @Override |
| 174 | + public PlaylistInfo.PlaylistType getPlaylistType() throws ParsingException { |
| 175 | + return playlistType; |
| 176 | + } |
| 177 | + |
| 178 | + private String getPlaylistId() throws ParsingException { |
| 179 | + String id = lockupViewModel.getString("contentId"); |
| 180 | + if (Utils.isNullOrEmpty(id)) { |
| 181 | + id = lockupViewModel.getObject("rendererContext") |
| 182 | + .getObject("commandContext") |
| 183 | + .getObject("watchEndpoint") |
| 184 | + .getString("playlistId"); |
| 185 | + } |
| 186 | + |
| 187 | + if (Utils.isNullOrEmpty(id)) { |
| 188 | + throw new ParsingException("Could not get playlist ID"); |
| 189 | + } |
| 190 | + |
| 191 | + return id; |
| 192 | + } |
| 193 | +} |
0 commit comments