Skip to content

Commit 98a90fd

Browse files
committed
Don't cache comments count and return early on page fetch if no token.
1 parent 2974dfa commit 98a90fd

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
3333
/**
3434
* Whether comments are disabled on video.
3535
*/
36-
private boolean commentsDisabled = true;
37-
38-
/**
39-
* The total number of comments on video.
40-
*/
41-
private int commentsCount = (int) ITEM_COUNT_UNKNOWN;
36+
private boolean commentsDisabled;
4237

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

266+
if (initialToken == null) {
267+
return;
268+
}
269+
271270
// @formatter:off
272271
final byte[] ajaxBody = JsonWriter.string(
273272
prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
@@ -289,23 +288,23 @@ public boolean isCommentsDisabled() {
289288
public int getCommentsCount() throws ExtractionException {
290289
assertPageFetched();
291290

292-
if (commentsCount == ITEM_COUNT_UNKNOWN) {
293-
final JsonObject countText = ajaxJson
294-
.getArray("onResponseReceivedEndpoints").getObject(0)
295-
.getObject("reloadContinuationItemsCommand")
296-
.getArray("continuationItems").getObject(0)
297-
.getObject("commentsHeaderRenderer")
298-
.getObject("countText");
299-
300-
try {
301-
commentsCount = Integer.parseInt(
302-
Utils.removeNonDigitCharacters(getTextFromObject(countText))
303-
);
304-
} catch (final Exception e) {
305-
throw new ExtractionException("Unable to get comments count", e);
306-
}
291+
if (commentsDisabled) {
292+
return -1;
307293
}
308294

309-
return commentsCount;
295+
final JsonObject countText = ajaxJson
296+
.getArray("onResponseReceivedEndpoints").getObject(0)
297+
.getObject("reloadContinuationItemsCommand")
298+
.getArray("continuationItems").getObject(0)
299+
.getObject("commentsHeaderRenderer")
300+
.getObject("countText");
301+
302+
try {
303+
return Integer.parseInt(
304+
Utils.removeNonDigitCharacters(getTextFromObject(countText))
305+
);
306+
} catch (final Exception e) {
307+
throw new ExtractionException("Unable to get comments count", e);
308+
}
310309
}
311310
}

0 commit comments

Comments
 (0)