Skip to content

Commit

Permalink
Don't cache comments count and return early on page fetch if no token.
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed Dec 8, 2022
1 parent be78a8d commit db241e7
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
/**
* Whether comments are disabled on video.
*/
private boolean commentsDisabled = true;

/**
* The total number of comments on video.
*/
private int commentsCount = (int) ITEM_COUNT_UNKNOWN;
private boolean commentsDisabled;

/**
* The second ajax <b>/next</b> response.
Expand Down Expand Up @@ -268,6 +263,10 @@ public void onFetchPage(@Nonnull final Downloader downloader)
final String initialToken =
findInitialCommentsToken(getJsonPostResponse("next", body, localization));

if (initialToken == null) {
return;
}

// @formatter:off
final byte[] ajaxBody = JsonWriter.string(
prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
Expand All @@ -289,23 +288,23 @@ public boolean isCommentsDisabled() {
public int getCommentsCount() throws ExtractionException {
assertPageFetched();

if (commentsCount == ITEM_COUNT_UNKNOWN) {
final JsonObject countText = ajaxJson
.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);
}
if (commentsDisabled) {
return -1;
}

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);
}
}
}

0 comments on commit db241e7

Please sign in to comment.