|
1 | 1 | package org.schabi.newpipe.extractor.services.media_ccc.extractors; |
2 | 2 |
|
3 | 3 | import com.grack.nanojson.JsonArray; |
| 4 | +import com.grack.nanojson.JsonObject; |
4 | 5 | import com.grack.nanojson.JsonParser; |
5 | 6 | import com.grack.nanojson.JsonParserException; |
| 7 | +import org.schabi.newpipe.extractor.Image; |
| 8 | +import org.schabi.newpipe.extractor.Image.ResolutionLevel; |
6 | 9 | import org.schabi.newpipe.extractor.downloader.Downloader; |
7 | 10 | import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
8 | 11 | import org.schabi.newpipe.extractor.exceptions.ParsingException; |
9 | 12 | import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; |
10 | 13 | import org.schabi.newpipe.extractor.localization.Localization; |
11 | 14 |
|
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import javax.annotation.Nullable; |
12 | 17 | import java.io.IOException; |
13 | 18 | import java.time.OffsetDateTime; |
14 | 19 | import java.time.format.DateTimeParseException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
15 | 23 | import java.util.regex.Pattern; |
16 | 24 |
|
| 25 | +import static org.schabi.newpipe.extractor.Image.HEIGHT_UNKNOWN; |
| 26 | +import static org.schabi.newpipe.extractor.Image.WIDTH_UNKNOWN; |
| 27 | +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
| 28 | + |
17 | 29 | public final class MediaCCCParsingHelper { |
18 | | - // {conference_slug}/{room_slug} |
| 30 | + // conference_slug/room_slug |
19 | 31 | private static final Pattern LIVE_STREAM_ID_PATTERN = Pattern.compile("\\w+/\\w+"); |
20 | 32 | private static JsonArray liveStreams = null; |
21 | 33 |
|
@@ -69,4 +81,98 @@ public static JsonArray getLiveStreams(final Downloader downloader, |
69 | 81 | } |
70 | 82 | return liveStreams; |
71 | 83 | } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get an {@link Image} list from a given image logo URL. |
| 87 | + * |
| 88 | + * <p> |
| 89 | + * If the image URL is null or empty, an empty list is returned; otherwise, a singleton list is |
| 90 | + * returned containing an {@link Image} with the image URL with its height, width and |
| 91 | + * resolution unknown. |
| 92 | + * </p> |
| 93 | + * |
| 94 | + * @param logoImageUrl a logo image URL, which can be null or empty |
| 95 | + * @return an unmodifiable list of {@link Image}s, which is always empty or a singleton |
| 96 | + */ |
| 97 | + @Nonnull |
| 98 | + public static List<Image> getImageListFromLogoImageUrl(@Nullable final String logoImageUrl) { |
| 99 | + if (isNullOrEmpty(logoImageUrl)) { |
| 100 | + return List.of(); |
| 101 | + } |
| 102 | + |
| 103 | + return List.of(new Image(logoImageUrl, HEIGHT_UNKNOWN, WIDTH_UNKNOWN, |
| 104 | + ResolutionLevel.UNKNOWN)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get the {@link Image} list of thumbnails from a given stream item. |
| 109 | + * |
| 110 | + * <p> |
| 111 | + * MediaCCC API provides two thumbnails for a stream item: a {@code thumb_url} one, which is |
| 112 | + * medium quality and a {@code poster_url} one, which is high quality in most cases. |
| 113 | + * </p> |
| 114 | + * |
| 115 | + * @param streamItem a stream JSON item of MediaCCC's API, which must not be null |
| 116 | + * @return an unmodifiable list, which is never null but can be empty. |
| 117 | + */ |
| 118 | + @Nonnull |
| 119 | + public static List<Image> getThumbnailsFromStreamItem(@Nonnull final JsonObject streamItem) { |
| 120 | + return getThumbnailsFromObject(streamItem, "thumb_url", "poster_url"); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get the {@link Image} list of thumbnails from a given live stream item. |
| 125 | + * |
| 126 | + * <p> |
| 127 | + * MediaCCC API provides two URL thumbnails for a livestream item: a {@code thumb} one, |
| 128 | + * which should be medium quality and a {@code poster_url} one, which should be high quality. |
| 129 | + * </p> |
| 130 | + * |
| 131 | + * @param liveStreamItem a stream JSON item of MediaCCC's API, which must not be null |
| 132 | + * @return an unmodifiable list, which is never null but can be empty. |
| 133 | + */ |
| 134 | + @Nonnull |
| 135 | + public static List<Image> getThumbnailsFromLiveStreamItem( |
| 136 | + @Nonnull final JsonObject liveStreamItem) { |
| 137 | + return getThumbnailsFromObject(liveStreamItem, "thumb", "poster"); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Utility method to get an {@link Image} list of thumbnails from a stream or a livestream. |
| 142 | + * |
| 143 | + * <p> |
| 144 | + * MediaCCC's API thumbnails come from two elements: a {@code thumb} element, which links to a |
| 145 | + * medium thumbnail and a {@code poster} element, which links to a high thumbnail. |
| 146 | + * </p> |
| 147 | + * <p> |
| 148 | + * Thumbnails are only added if their URLs are not null or empty. |
| 149 | + * </p> |
| 150 | + * |
| 151 | + * @param streamOrLivestreamItem a (live)stream JSON item of MediaCCC's API, which must not be |
| 152 | + * null |
| 153 | + * @param thumbUrlKey the name of the {@code thumb} URL key |
| 154 | + * @param posterUrlKey the name of the {@code poster} URL key |
| 155 | + * @return an unmodifiable list, which is never null but can be empty. |
| 156 | + */ |
| 157 | + @Nonnull |
| 158 | + private static List<Image> getThumbnailsFromObject( |
| 159 | + @Nonnull final JsonObject streamOrLivestreamItem, |
| 160 | + @Nonnull final String thumbUrlKey, |
| 161 | + @Nonnull final String posterUrlKey) { |
| 162 | + final List<Image> imageList = new ArrayList<>(2); |
| 163 | + |
| 164 | + final String thumbUrl = streamOrLivestreamItem.getString(thumbUrlKey); |
| 165 | + if (!isNullOrEmpty(thumbUrl)) { |
| 166 | + imageList.add(new Image(thumbUrl, HEIGHT_UNKNOWN, WIDTH_UNKNOWN, |
| 167 | + ResolutionLevel.MEDIUM)); |
| 168 | + } |
| 169 | + |
| 170 | + final String posterUrl = streamOrLivestreamItem.getString(posterUrlKey); |
| 171 | + if (!isNullOrEmpty(posterUrl)) { |
| 172 | + imageList.add(new Image(posterUrl, HEIGHT_UNKNOWN, WIDTH_UNKNOWN, |
| 173 | + ResolutionLevel.HIGH)); |
| 174 | + } |
| 175 | + |
| 176 | + return Collections.unmodifiableList(imageList); |
| 177 | + } |
72 | 178 | } |
0 commit comments