From 6f8331524b49a973f449a96cdc943aa0415a2b70 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Thu, 9 Feb 2023 19:43:27 +0100 Subject: [PATCH] [PeerTube] Add utility method to get thumbnails of playlists and videos This method, getThumbnailsFromPlaylistOrVideoItem, has been added in PeertubeParsingHelper and returns the two image variants for playlists and videos. --- .../peertube/PeertubeParsingHelper.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java index ad11e6b39..4e8bd2d35 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java @@ -22,6 +22,7 @@ import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeParseException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -198,6 +199,47 @@ public final class PeertubeParsingHelper { "banners", "banner"); } + /** + * Get thumbnails from a playlist or a video item {@link JsonObject}. + * + *

+ * PeerTube provides two thumbnails in its API: a low one, represented by the value of the + * {@code thumbnailPath} key, and a medium one, represented by the value of the + * {@code previewPath} key. + *

+ * + *

+ * If a value is not null or empty, an {@link Image} will be added to the list returned with + * the URL to the thumbnail ({@code baseUrl + value}), a height and a width unknown and the + * corresponding resolution level (see above). + *

+ * + * @param baseUrl the base URL of the PeerTube instance + * @param playlistOrVideoItemObject the playlist or the video item {@link JsonObject}, which + * must not be null + * @return an unmodifiable list of {@link Image}s, which is never null but can be empty + */ + @Nonnull + public static List getThumbnailsFromPlaylistOrVideoItem( + @Nonnull final String baseUrl, + @Nonnull final JsonObject playlistOrVideoItemObject) { + final List imageList = new ArrayList<>(2); + + final String thumbnailPath = playlistOrVideoItemObject.getString("thumbnailPath"); + if (!isNullOrEmpty(thumbnailPath)) { + imageList.add(new Image(baseUrl + thumbnailPath, HEIGHT_UNKNOWN, WIDTH_UNKNOWN, + ResolutionLevel.LOW)); + } + + final String previewPath = playlistOrVideoItemObject.getString("previewPath"); + if (!isNullOrEmpty(previewPath)) { + imageList.add(new Image(baseUrl + previewPath, HEIGHT_UNKNOWN, WIDTH_UNKNOWN, + ResolutionLevel.MEDIUM)); + } + + return Collections.unmodifiableList(imageList); + } + /** * Utility method to get avatars and banners from video channels and accounts from given name * keys.