3131public class YoutubeCommentsExtractor extends CommentsExtractor {
3232
3333 /**
34- * The initial request's continuation token.
35- * Since we need to make two requests to get the comments,
34+ * Whether comments are disabled on video.
3635 */
37- private String initialToken ;
36+ private boolean commentsDisabled = true ;
3837
3938 /**
40- * Whether comments are disabled on video.
39+ * The total number of comments on video.
4140 */
42- private boolean commentsDisabled = true ;
41+ private int commentsCount = ( int ) ITEM_COUNT_UNKNOWN ;
4342
4443 /**
4544 * The second ajax <b>/next</b> response.
@@ -62,7 +61,7 @@ public InfoItemsPage<CommentsInfoItem> getInitialPage()
6261 return getInfoItemsPageForDisabledComments ();
6362 }
6463
65- return getPage ( getNextPage ( this . initialToken ) );
64+ return extractComments ( ajaxJson );
6665 }
6766
6867 /**
@@ -186,12 +185,17 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
186185 .getBytes (StandardCharsets .UTF_8 );
187186 // @formatter:on
188187
189- this . ajaxJson = getJsonPostResponse ("next" , body , localization );
188+ final var jsonObject = getJsonPostResponse ("next" , body , localization );
190189
190+ return extractComments (jsonObject );
191+ }
192+
193+ private InfoItemsPage <CommentsInfoItem > extractComments (final JsonObject jsonObject )
194+ throws ExtractionException {
191195 final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector (
192196 getServiceId ());
193197 collectCommentsFrom (collector );
194- return new InfoItemsPage <>(collector , getNextPage (ajaxJson ));
198+ return new InfoItemsPage <>(collector , getNextPage (jsonObject ));
195199 }
196200
197201 private void collectCommentsFrom (final CommentsInfoItemsCollector collector )
@@ -261,7 +265,18 @@ public void onFetchPage(@Nonnull final Downloader downloader)
261265 .getBytes (StandardCharsets .UTF_8 );
262266 // @formatter:on
263267
264- initialToken = findInitialCommentsToken (getJsonPostResponse ("next" , body , localization ));
268+ final String initialToken =
269+ findInitialCommentsToken (getJsonPostResponse ("next" , body , localization ));
270+
271+ // @formatter:off
272+ final byte [] ajaxBody = JsonWriter .string (
273+ prepareDesktopJsonBuilder (localization , getExtractorContentCountry ())
274+ .value ("continuation" , initialToken )
275+ .done ())
276+ .getBytes (StandardCharsets .UTF_8 );
277+ // @formatter:on
278+
279+ ajaxJson = getJsonPostResponse ("next" , ajaxBody , localization );
265280 }
266281
267282
@@ -272,17 +287,25 @@ public boolean isCommentsDisabled() {
272287
273288 @ Override
274289 public int getCommentsCount () throws ExtractionException {
275- final JsonObject countText = ajaxJson
276- .getArray ("onResponseReceivedEndpoints" ).getObject (0 )
277- .getObject ("reloadContinuationItemsCommand" )
278- .getArray ("continuationItems" ).getObject (0 )
279- .getObject ("commentsHeaderRenderer" )
280- .getObject ("countText" );
281-
282- try {
283- return Integer .parseInt (Utils .removeNonDigitCharacters (getTextFromObject (countText )));
284- } catch (final Exception e ) {
285- throw new ExtractionException ("Unable to get comments count" , e );
290+ assertPageFetched ();
291+
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+ }
286307 }
308+
309+ return commentsCount ;
287310 }
288311}
0 commit comments