[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
*/
@Nullable
private String findInitialCommentsToken(final JsonObject nextResponse)
throws ExtractionException {
final String token = JsonUtils.getArray(nextResponse,
"contents.twoColumnWatchNextResults.results.results.contents")
private String findInitialCommentsToken(final JsonObject 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()
// Only use JsonObjects
.filter(JsonObject.class::isInstance)
@ -105,6 +110,16 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
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
private InfoItemsPage<CommentsInfoItem> getInfoItemsPageForDisabledComments() {
return new InfoItemsPage<>(Collections.emptyList(), null, Collections.emptyList());