Skip to content

Commit 2f40861

Browse files
committed
[MediaCCC] Add utility methods to get image lists from conference logos and streams
These three new methods, added in MediaCCCParsingHelper, getImageListFromImageUrl(String), getThumbnailsFromStreamItem(JsonObject) and getThumbnailsFromLiveStreamItem(JsonObject) (the last two are based on a common method, getThumbnailsFromObject(JsonObject, String, String)), return an empty list if the case no image URL could be extracted. Images returned have their height and width unknown and a resolution level depending on the image key of the JSON API response.
1 parent 71cda03 commit 2f40861

1 file changed

Lines changed: 107 additions & 1 deletion

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCParsingHelper.java

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
package org.schabi.newpipe.extractor.services.media_ccc.extractors;
22

33
import com.grack.nanojson.JsonArray;
4+
import com.grack.nanojson.JsonObject;
45
import com.grack.nanojson.JsonParser;
56
import com.grack.nanojson.JsonParserException;
7+
import org.schabi.newpipe.extractor.Image;
8+
import org.schabi.newpipe.extractor.Image.ResolutionLevel;
69
import org.schabi.newpipe.extractor.downloader.Downloader;
710
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
811
import org.schabi.newpipe.extractor.exceptions.ParsingException;
912
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
1013
import org.schabi.newpipe.extractor.localization.Localization;
1114

15+
import javax.annotation.Nonnull;
16+
import javax.annotation.Nullable;
1217
import java.io.IOException;
1318
import java.time.OffsetDateTime;
1419
import java.time.format.DateTimeParseException;
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.List;
1523
import java.util.regex.Pattern;
1624

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+
1729
public final class MediaCCCParsingHelper {
18-
// {conference_slug}/{room_slug}
30+
// conference_slug/room_slug
1931
private static final Pattern LIVE_STREAM_ID_PATTERN = Pattern.compile("\\w+/\\w+");
2032
private static JsonArray liveStreams = null;
2133

@@ -69,4 +81,98 @@ public static JsonArray getLiveStreams(final Downloader downloader,
6981
}
7082
return liveStreams;
7183
}
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+
}
72178
}

0 commit comments

Comments
 (0)