From 98a90fd9c8fa02c11bb2a8a04cd2b5cf3b7c3028 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 8 Dec 2022 12:10:33 +0000 Subject: [PATCH] Don't cache comments count and return early on page fetch if no token. --- .../extractors/YoutubeCommentsExtractor.java | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java index 92d46118..176317a3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java @@ -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 /next response. @@ -268,6 +263,10 @@ public class YoutubeCommentsExtractor extends CommentsExtractor { final String initialToken = findInitialCommentsToken(getJsonPostResponse("next", body, localization)); + if (initialToken == null) { + return; + } + // @formatter:off final byte[] ajaxBody = JsonWriter.string( prepareDesktopJsonBuilder(localization, getExtractorContentCountry()) @@ -289,23 +288,23 @@ public class YoutubeCommentsExtractor extends CommentsExtractor { 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); + } } }