Skip to content

Commit 67ef4f4

Browse files
committed
Cleanup and remove optional.
1 parent 22f71b0 commit 67ef4f4

2 files changed

Lines changed: 46 additions & 60 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfo.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static CommentsInfo getInfo(final CommentsExtractor commentsExtractor)
5050
commentsInfo.setRelatedItems(initialCommentsPage.getItems());
5151
try {
5252
commentsInfo.setCommentsCount(commentsExtractor.getCommentsCount());
53-
} catch (Exception e) {
53+
} catch (final Exception e) {
5454
commentsInfo.addError(e);
5555
}
5656
commentsInfo.setNextPage(initialCommentsPage.getNextPage());
@@ -92,8 +92,6 @@ public void setCommentsExtractor(final CommentsExtractor commentsExtractor) {
9292
}
9393

9494
/**
95-
* @return <code>true</code> if the comments are disabled otherwise <code>false</code> (default)
96-
* @apiNote Warning: This method is experimental and may get removed in a future release.
9795
* @return {@code true} if the comments are disabled otherwise {@code false} (default)
9896
* @see CommentsExtractor#isCommentsDisabled()
9997
*/
@@ -102,8 +100,6 @@ public boolean isCommentsDisabled() {
102100
}
103101

104102
/**
105-
* @param commentsDisabled <code>true</code> if the comments are disabled otherwise <code>false</code>
106-
* @apiNote Warning: This method is experimental and may get removed in a future release.
107103
* @param commentsDisabled {@code true} if the comments are disabled otherwise {@code false}
108104
*/
109105
public void setCommentsDisabled(final boolean commentsDisabled) {
@@ -124,7 +120,7 @@ public int getCommentsCount() {
124120
*
125121
* @param commentsCount the commentsCount to set.
126122
*/
127-
public void setCommentsCount(int commentsCount) {
123+
public void setCommentsCount(final int commentsCount) {
128124
this.commentsCount = commentsCount;
129125
}
130126
}

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

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
package 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;
176
import org.schabi.newpipe.extractor.Page;
187
import org.schabi.newpipe.extractor.StreamingService;
198
import org.schabi.newpipe.extractor.comments.CommentsExtractor;
@@ -25,27 +14,33 @@
2514
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
2615
import org.schabi.newpipe.extractor.localization.Localization;
2716
import 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;
3217
import 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+
3431
public 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

Comments
 (0)