|
33 | 33 | import com.grack.nanojson.JsonParserException; |
34 | 34 | import com.grack.nanojson.JsonWriter; |
35 | 35 | import org.jsoup.nodes.Entities; |
| 36 | + |
| 37 | +import org.schabi.newpipe.extractor.Image; |
| 38 | +import org.schabi.newpipe.extractor.Image.ResolutionLevel; |
36 | 39 | import org.schabi.newpipe.extractor.MetaInfo; |
37 | 40 | import org.schabi.newpipe.extractor.downloader.Response; |
38 | 41 | import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException; |
|
69 | 72 | import java.util.Random; |
70 | 73 | import java.util.Set; |
71 | 74 | import java.util.regex.Pattern; |
| 75 | +import java.util.stream.Collectors; |
72 | 76 | import java.util.stream.Stream; |
73 | 77 |
|
74 | 78 | import javax.annotation.Nonnull; |
@@ -1133,17 +1137,61 @@ public static String fixThumbnailUrl(@Nonnull final String thumbnailUrl) { |
1133 | 1137 | return result; |
1134 | 1138 | } |
1135 | 1139 |
|
1136 | | - public static String getThumbnailUrlFromInfoItem(final JsonObject infoItem) |
| 1140 | + /** |
| 1141 | + * Get thumbnails from a {@link JsonObject} representing a YouTube |
| 1142 | + * {@link org.schabi.newpipe.extractor.InfoItem InfoItem}. |
| 1143 | + * |
| 1144 | + * <p> |
| 1145 | + * Thumbnails are got from the {@code thumbnails} {@link JsonArray} inside the {@code thumbnail} |
| 1146 | + * {@link JsonObject} of the YouTube {@link org.schabi.newpipe.extractor.InfoItem InfoItem}, |
| 1147 | + * using {@link #getImagesFromThumbnailsArray(JsonArray)}. |
| 1148 | + * </p> |
| 1149 | + * |
| 1150 | + * @param infoItem a YouTube {@link org.schabi.newpipe.extractor.InfoItem InfoItem} |
| 1151 | + * @return an unmodifiable list of {@link Image}s found in the {@code thumbnails} |
| 1152 | + * {@link JsonArray} |
| 1153 | + * @throws ParsingException if an exception occurs when |
| 1154 | + * {@link #getImagesFromThumbnailsArray(JsonArray)} is executed |
| 1155 | + */ |
| 1156 | + @Nonnull |
| 1157 | + public static List<Image> getThumbnailsFromInfoItem(@Nonnull final JsonObject infoItem) |
1137 | 1158 | throws ParsingException { |
1138 | | - // TODO: Don't simply get the first item, but look at all thumbnails and their resolution |
1139 | 1159 | try { |
1140 | | - return fixThumbnailUrl(infoItem.getObject("thumbnail").getArray("thumbnails") |
1141 | | - .getObject(0).getString("url")); |
| 1160 | + return getImagesFromThumbnailsArray(infoItem.getObject("thumbnail") |
| 1161 | + .getArray("thumbnails")); |
1142 | 1162 | } catch (final Exception e) { |
1143 | | - throw new ParsingException("Could not get thumbnail url", e); |
| 1163 | + throw new ParsingException("Could not get thumbnails from InfoItem", e); |
1144 | 1164 | } |
1145 | 1165 | } |
1146 | 1166 |
|
| 1167 | + /** |
| 1168 | + * Get images from a YouTube {@code thumbnails} {@link JsonArray}. |
| 1169 | + * |
| 1170 | + * <p> |
| 1171 | + * The properties of the {@link Image}s created will be set using the corresponding ones of |
| 1172 | + * thumbnail items. |
| 1173 | + * </p> |
| 1174 | + * |
| 1175 | + * @param thumbnails a YouTube {@code thumbnails} {@link JsonArray} |
| 1176 | + * @return an unmodifiable list of {@link Image}s extracted from the given {@link JsonArray} |
| 1177 | + */ |
| 1178 | + @Nonnull |
| 1179 | + public static List<Image> getImagesFromThumbnailsArray( |
| 1180 | + @Nonnull final JsonArray thumbnails) { |
| 1181 | + return thumbnails.stream() |
| 1182 | + .filter(JsonObject.class::isInstance) |
| 1183 | + .map(JsonObject.class::cast) |
| 1184 | + .filter(thumbnail -> !isNullOrEmpty(thumbnail.getString("url"))) |
| 1185 | + .map(thumbnail -> { |
| 1186 | + final int height = thumbnail.getInt("height", Image.HEIGHT_UNKNOWN); |
| 1187 | + return new Image(fixThumbnailUrl(thumbnail.getString("url")), |
| 1188 | + height, |
| 1189 | + thumbnail.getInt("width", Image.WIDTH_UNKNOWN), |
| 1190 | + ResolutionLevel.fromHeight(height)); |
| 1191 | + }) |
| 1192 | + .collect(Collectors.toUnmodifiableList()); |
| 1193 | + } |
| 1194 | + |
1147 | 1195 | @Nonnull |
1148 | 1196 | public static String getValidJsonResponseBody(@Nonnull final Response response) |
1149 | 1197 | throws ParsingException, MalformedURLException { |
|
0 commit comments