11package org .schabi .newpipe .extractor .services .youtube .extractors ;
22
3- import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getJsonPostResponse ;
4- import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getTextFromObject ;
5- import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareDesktopJsonBuilder ;
6- import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
7-
8- import java .io .IOException ;
9- import java .nio .charset .StandardCharsets ;
10- import java .util .Collections ;
11- import java .util .List ;
12- import java .util .Optional ;
13-
14- import javax .annotation .Nonnull ;
15- import javax .annotation .Nullable ;
16-
3+ import com .grack .nanojson .JsonArray ;
4+ import com .grack .nanojson .JsonObject ;
5+ import com .grack .nanojson .JsonWriter ;
176import org .schabi .newpipe .extractor .Page ;
187import org .schabi .newpipe .extractor .StreamingService ;
198import org .schabi .newpipe .extractor .comments .CommentsExtractor ;
2514import org .schabi .newpipe .extractor .linkhandler .ListLinkHandler ;
2615import org .schabi .newpipe .extractor .localization .Localization ;
2716import org .schabi .newpipe .extractor .utils .JsonUtils ;
28-
29- import com .grack .nanojson .JsonArray ;
30- import com .grack .nanojson .JsonObject ;
31- import com .grack .nanojson .JsonWriter ;
3217import org .schabi .newpipe .extractor .utils .Utils ;
3318
19+ import javax .annotation .Nonnull ;
20+ import javax .annotation .Nullable ;
21+ import java .io .IOException ;
22+ import java .nio .charset .StandardCharsets ;
23+ import java .util .Collections ;
24+ import java .util .List ;
25+
26+ import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getJsonPostResponse ;
27+ import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getTextFromObject ;
28+ import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareDesktopJsonBuilder ;
29+ import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
30+
3431public class YoutubeCommentsExtractor extends CommentsExtractor {
3532
36- private JsonObject nextResponse ;
33+ /**
34+ * The initial request's continuation token.
35+ * Since we need to make two requests to get the comments,
36+ */
37+ private String initialToken ;
3738
3839 /**
39- * Caching mechanism and holder of the commentsDisabled value.
40- * <br/>
41- * Initial value = empty -> unknown if comments are disabled or not<br/>
42- * Some method calls {@link #findInitialCommentsToken()}
43- * -> value is set<br/>
44- * If the method or another one that is depending on disabled comments
45- * is now called again, the method execution can avoid unnecessary calls
40+ * Whether comments are disabled on video.
4641 */
47- @ SuppressWarnings ( "OptionalUsedAsFieldOrParameterType" )
48- private Optional < Boolean > optCommentsDisabled = Optional . empty ();
42+ private boolean commentsDisabled = true ;
43+
4944 /**
5045 * The second ajax <b>/next</b> response.
5146 */
@@ -63,31 +58,25 @@ public InfoItemsPage<CommentsInfoItem> getInitialPage()
6358 throws IOException , ExtractionException {
6459
6560 // Check if findInitialCommentsToken was already called and optCommentsDisabled initialized
66- if (optCommentsDisabled .orElse (false )) {
67- return getInfoItemsPageForDisabledComments ();
68- }
69-
70- // Get the token
71- final String commentsToken = findInitialCommentsToken ();
72- // Check if the comments have been disabled
73- if (optCommentsDisabled .get ()) {
61+ if (commentsDisabled ) {
7462 return getInfoItemsPageForDisabledComments ();
7563 }
7664
77- return getPage (getNextPage (commentsToken ));
65+ return getPage (getNextPage (this . initialToken ));
7866 }
7967
8068 /**
8169 * Finds the initial comments token and initializes commentsDisabled.
8270 * <br/>
83- * Also sets {@link #optCommentsDisabled }.
71+ * Also sets {@link #commentsDisabled }.
8472 *
8573 * @return the continuation token or null if none was found
8674 */
8775 @ Nullable
88- private String findInitialCommentsToken () throws ExtractionException {
76+ private String findInitialCommentsToken (final JsonObject nextResponse )
77+ throws ExtractionException {
8978 final String token = JsonUtils .getArray (nextResponse ,
90- "contents.twoColumnWatchNextResults.results.results.contents" )
79+ "contents.twoColumnWatchNextResults.results.results.contents" )
9180 .stream ()
9281 // Only use JsonObjects
9382 .filter (JsonObject .class ::isInstance )
@@ -118,7 +107,7 @@ private String findInitialCommentsToken() throws ExtractionException {
118107 .orElse (null );
119108
120109 // The comments are disabled if we couldn't get a token
121- optCommentsDisabled = Optional . of ( token == null ) ;
110+ commentsDisabled = token == null ;
122111
123112 return token ;
124113 }
@@ -129,9 +118,9 @@ private InfoItemsPage<CommentsInfoItem> getInfoItemsPageForDisabledComments() {
129118 }
130119
131120 @ Nullable
132- private Page getNextPage (@ Nonnull final JsonObject ajaxJson ) throws ExtractionException {
121+ private Page getNextPage (@ Nonnull final JsonObject jsonObject ) throws ExtractionException {
133122 final JsonArray onResponseReceivedEndpoints =
134- ajaxJson .getArray ("onResponseReceivedEndpoints" );
123+ jsonObject .getArray ("onResponseReceivedEndpoints" );
135124
136125 // Prevent ArrayIndexOutOfBoundsException
137126 if (onResponseReceivedEndpoints .isEmpty ()) {
@@ -179,19 +168,23 @@ private Page getNextPage(final String continuation) throws ParsingException {
179168 @ Override
180169 public InfoItemsPage <CommentsInfoItem > getPage (final Page page )
181170 throws IOException , ExtractionException {
182- if (optCommentsDisabled .orElse (false )) {
171+
172+ if (commentsDisabled ) {
183173 return getInfoItemsPageForDisabledComments ();
184174 }
175+
185176 if (page == null || isNullOrEmpty (page .getId ())) {
186177 throw new IllegalArgumentException ("Page doesn't have the continuation." );
187178 }
188179
189180 final Localization localization = getExtractorLocalization ();
181+ // @formatter:off
190182 final byte [] body = JsonWriter .string (
191183 prepareDesktopJsonBuilder (localization , getExtractorContentCountry ())
192184 .value ("continuation" , page .getId ())
193185 .done ())
194186 .getBytes (StandardCharsets .UTF_8 );
187+ // @formatter:on
195188
196189 this .ajaxJson = getJsonPostResponse ("next" , body , localization );
197190
@@ -201,7 +194,8 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
201194 return new InfoItemsPage <>(collector , getNextPage (ajaxJson ));
202195 }
203196
204- private void collectCommentsFrom (final CommentsInfoItemsCollector collector ) throws ParsingException {
197+ private void collectCommentsFrom (final CommentsInfoItemsCollector collector )
198+ throws ParsingException {
205199
206200 final JsonArray onResponseReceivedEndpoints =
207201 ajaxJson .getArray ("onResponseReceivedEndpoints" );
@@ -259,25 +253,21 @@ private void collectCommentsFrom(final CommentsInfoItemsCollector collector) thr
259253 public void onFetchPage (@ Nonnull final Downloader downloader )
260254 throws IOException , ExtractionException {
261255 final Localization localization = getExtractorLocalization ();
256+ // @formatter:off
262257 final byte [] body = JsonWriter .string (
263258 prepareDesktopJsonBuilder (localization , getExtractorContentCountry ())
264259 .value ("videoId" , getId ())
265260 .done ())
266261 .getBytes (StandardCharsets .UTF_8 );
262+ // @formatter:on
267263
268- nextResponse = getJsonPostResponse ("next" , body , localization );
264+ initialToken = findInitialCommentsToken ( getJsonPostResponse ("next" , body , localization ) );
269265 }
270266
271267
272268 @ Override
273- public boolean isCommentsDisabled () throws ExtractionException {
274- // Check if commentsDisabled has to be initialized
275- if (!optCommentsDisabled .isPresent ()) {
276- // Initialize commentsDisabled
277- this .findInitialCommentsToken ();
278- }
279-
280- return optCommentsDisabled .get ();
269+ public boolean isCommentsDisabled () {
270+ return commentsDisabled ;
281271 }
282272
283273 @ Override
0 commit comments