[YouTube] Fix ParsingException when comments are unavailable in a video (#1040)

Co-authored-by: bjs <bjs@elect-it.com>
Co-authored-by: Audric V. <74829229+AudricV@users.noreply.github.com>
Co-authored-by: Kavin <20838718+FireMasterK@users.noreply.github.com>
This commit is contained in:
Björn Sigurbergsson 2023-03-30 17:58:06 +00:00 committed by GitHub
parent 8d1303e18f
commit 1b6fe5edd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 4 deletions

View File

@ -66,10 +66,15 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
* @return the continuation token or null if none was found * @return the continuation token or null if none was found
*/ */
@Nullable @Nullable
private String findInitialCommentsToken(final JsonObject nextResponse) private String findInitialCommentsToken(final JsonObject nextResponse) {
throws ExtractionException { final JsonArray contents = getJsonContents(nextResponse);
final String token = JsonUtils.getArray(nextResponse,
"contents.twoColumnWatchNextResults.results.results.contents") // 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)
@ -105,6 +110,16 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
return token; return token;
} }
@Nullable
private JsonArray getJsonContents(final JsonObject nextResponse) {
try {
return JsonUtils.getArray(nextResponse,
"contents.twoColumnWatchNextResults.results.results.contents");
} catch (final ParsingException e) {
return null;
}
}
@Nonnull @Nonnull
private InfoItemsPage<CommentsInfoItem> getInfoItemsPageForDisabledComments() { private InfoItemsPage<CommentsInfoItem> getInfoItemsPageForDisabledComments() {
return new InfoItemsPage<>(Collections.emptyList(), null, Collections.emptyList()); return new InfoItemsPage<>(Collections.emptyList(), null, Collections.emptyList());