Skip to content

Commit 6f83315

Browse files
committed
[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.
1 parent 81c0d80 commit 6f83315

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.OffsetDateTime;
2323
import java.time.ZoneOffset;
2424
import java.time.format.DateTimeParseException;
25+
import java.util.ArrayList;
2526
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.stream.Collectors;
@@ -198,6 +199,47 @@ public static List<Image> getBannersFromAccountOrVideoChannelObject(
198199
"banners", "banner");
199200
}
200201

202+
/**
203+
* Get thumbnails from a playlist or a video item {@link JsonObject}.
204+
*
205+
* <p>
206+
* PeerTube provides two thumbnails in its API: a low one, represented by the value of the
207+
* {@code thumbnailPath} key, and a medium one, represented by the value of the
208+
* {@code previewPath} key.
209+
* </p>
210+
*
211+
* <p>
212+
* If a value is not null or empty, an {@link Image} will be added to the list returned with
213+
* the URL to the thumbnail ({@code baseUrl + value}), a height and a width unknown and the
214+
* corresponding resolution level (see above).
215+
* </p>
216+
*
217+
* @param baseUrl the base URL of the PeerTube instance
218+
* @param playlistOrVideoItemObject the playlist or the video item {@link JsonObject}, which
219+
* must not be null
220+
* @return an unmodifiable list of {@link Image}s, which is never null but can be empty
221+
*/
222+
@Nonnull
223+
public static List<Image> getThumbnailsFromPlaylistOrVideoItem(
224+
@Nonnull final String baseUrl,
225+
@Nonnull final JsonObject playlistOrVideoItemObject) {
226+
final List<Image> imageList = new ArrayList<>(2);
227+
228+
final String thumbnailPath = playlistOrVideoItemObject.getString("thumbnailPath");
229+
if (!isNullOrEmpty(thumbnailPath)) {
230+
imageList.add(new Image(baseUrl + thumbnailPath, HEIGHT_UNKNOWN, WIDTH_UNKNOWN,
231+
ResolutionLevel.LOW));
232+
}
233+
234+
final String previewPath = playlistOrVideoItemObject.getString("previewPath");
235+
if (!isNullOrEmpty(previewPath)) {
236+
imageList.add(new Image(baseUrl + previewPath, HEIGHT_UNKNOWN, WIDTH_UNKNOWN,
237+
ResolutionLevel.MEDIUM));
238+
}
239+
240+
return Collections.unmodifiableList(imageList);
241+
}
242+
201243
/**
202244
* Utility method to get avatars and banners from video channels and accounts from given name
203245
* keys.

0 commit comments

Comments
 (0)