Don't cache comments count and return early on page fetch if no token.

This commit is contained in:
Kavin 2022-12-08 12:10:33 +00:00
parent 2974dfaa48
commit 98a90fd9c8
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
1 changed files with 21 additions and 22 deletions

View File

@ -33,12 +33,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
/** /**
* Whether comments are disabled on video. * Whether comments are disabled on video.
*/ */
private boolean commentsDisabled = true; private boolean commentsDisabled;
/**
* The total number of comments on video.
*/
private int commentsCount = (int) ITEM_COUNT_UNKNOWN;
/** /**
* The second ajax <b>/next</b> response. * The second ajax <b>/next</b> response.
@ -268,6 +263,10 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
final String initialToken = final String initialToken =
findInitialCommentsToken(getJsonPostResponse("next", body, localization)); findInitialCommentsToken(getJsonPostResponse("next", body, localization));
if (initialToken == null) {
return;
}
// @formatter:off // @formatter:off
final byte[] ajaxBody = JsonWriter.string( final byte[] ajaxBody = JsonWriter.string(
prepareDesktopJsonBuilder(localization, getExtractorContentCountry()) prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
@ -289,23 +288,23 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
public int getCommentsCount() throws ExtractionException { public int getCommentsCount() throws ExtractionException {
assertPageFetched(); assertPageFetched();
if (commentsCount == ITEM_COUNT_UNKNOWN) { if (commentsDisabled) {
final JsonObject countText = ajaxJson return -1;
.getArray("onResponseReceivedEndpoints").getObject(0)
.getObject("reloadContinuationItemsCommand")
.getArray("continuationItems").getObject(0)
.getObject("commentsHeaderRenderer")
.getObject("countText");
try {
commentsCount = Integer.parseInt(
Utils.removeNonDigitCharacters(getTextFromObject(countText))
);
} catch (final Exception e) {
throw new ExtractionException("Unable to get comments count", e);
}
} }
return commentsCount; final JsonObject countText = ajaxJson
.getArray("onResponseReceivedEndpoints").getObject(0)
.getObject("reloadContinuationItemsCommand")
.getArray("continuationItems").getObject(0)
.getObject("commentsHeaderRenderer")
.getObject("countText");
try {
return Integer.parseInt(
Utils.removeNonDigitCharacters(getTextFromObject(countText))
);
} catch (final Exception e) {
throw new ExtractionException("Unable to get comments count", e);
}
} }
} }