Check if null instead of iterating an empty Collection.

This commit is contained in:
Kavin 2023-03-21 16:10:54 +00:00
parent 4c1af8cb6d
commit 7686c85fe7
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
1 changed files with 10 additions and 3 deletions

View File

@ -68,7 +68,14 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
@Nullable @Nullable
private String findInitialCommentsToken(final JsonObject nextResponse) private String findInitialCommentsToken(final JsonObject nextResponse)
throws ExtractionException { throws ExtractionException {
final String token = getJsonContents(nextResponse) final JsonArray contents = getJsonContents(nextResponse);
// For videos where comments are unavailable, this would be null
if (contents == null) {
return null;
}
final String token = contents
.stream() .stream()
// Only use JsonObjects // Only use JsonObjects
.filter(JsonObject.class::isInstance) .filter(JsonObject.class::isInstance)
@ -104,13 +111,13 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
return token; return token;
} }
@Nonnull @Nullable
private JsonArray getJsonContents(final JsonObject nextResponse) { private JsonArray getJsonContents(final JsonObject nextResponse) {
try { try {
return JsonUtils.getArray(nextResponse, return JsonUtils.getArray(nextResponse,
"contents.twoColumnWatchNextResults.results.results.contents"); "contents.twoColumnWatchNextResults.results.results.contents");
} catch (final ParsingException e) { } catch (final ParsingException e) {
return new JsonArray(Collections.emptyList()); return null;
} }
} }