Skip to content

Commit adfad08

Browse files
committed
[YouTube] Add utility methods to get images from InfoItems and thumbnails arrays
Unmodifiable lists of Images are returned, parsed from a given YouTube "thumbnails" JSON array. These methods will be used in all YouTube extractors and InfoItems, as the structures between content types (videos, channels, playlists, ...) are common.
1 parent d56b880 commit adfad08

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import com.grack.nanojson.JsonParserException;
3434
import com.grack.nanojson.JsonWriter;
3535
import org.jsoup.nodes.Entities;
36+
37+
import org.schabi.newpipe.extractor.Image;
38+
import org.schabi.newpipe.extractor.Image.ResolutionLevel;
3639
import org.schabi.newpipe.extractor.MetaInfo;
3740
import org.schabi.newpipe.extractor.downloader.Response;
3841
import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException;
@@ -69,6 +72,7 @@
6972
import java.util.Random;
7073
import java.util.Set;
7174
import java.util.regex.Pattern;
75+
import java.util.stream.Collectors;
7276
import java.util.stream.Stream;
7377

7478
import javax.annotation.Nonnull;
@@ -1133,17 +1137,61 @@ public static String fixThumbnailUrl(@Nonnull final String thumbnailUrl) {
11331137
return result;
11341138
}
11351139

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)
11371158
throws ParsingException {
1138-
// TODO: Don't simply get the first item, but look at all thumbnails and their resolution
11391159
try {
1140-
return fixThumbnailUrl(infoItem.getObject("thumbnail").getArray("thumbnails")
1141-
.getObject(0).getString("url"));
1160+
return getImagesFromThumbnailsArray(infoItem.getObject("thumbnail")
1161+
.getArray("thumbnails"));
11421162
} 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);
11441164
}
11451165
}
11461166

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+
11471195
@Nonnull
11481196
public static String getValidJsonResponseBody(@Nonnull final Response response)
11491197
throws ParsingException, MalformedURLException {

0 commit comments

Comments
 (0)