Cleanup and remove optional.

This commit is contained in:
Kavin 2022-12-08 00:41:49 +00:00
parent 22f71b010c
commit 67ef4f4c30
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 46 additions and 60 deletions

View file

@ -50,7 +50,7 @@ public final class CommentsInfo extends ListInfo<CommentsInfoItem> {
commentsInfo.setRelatedItems(initialCommentsPage.getItems()); commentsInfo.setRelatedItems(initialCommentsPage.getItems());
try { try {
commentsInfo.setCommentsCount(commentsExtractor.getCommentsCount()); commentsInfo.setCommentsCount(commentsExtractor.getCommentsCount());
} catch (Exception e) { } catch (final Exception e) {
commentsInfo.addError(e); commentsInfo.addError(e);
} }
commentsInfo.setNextPage(initialCommentsPage.getNextPage()); commentsInfo.setNextPage(initialCommentsPage.getNextPage());
@ -92,8 +92,6 @@ public final class CommentsInfo extends ListInfo<CommentsInfoItem> {
} }
/** /**
* @return <code>true</code> if the comments are disabled otherwise <code>false</code> (default)
* @apiNote Warning: This method is experimental and may get removed in a future release.
* @return {@code true} if the comments are disabled otherwise {@code false} (default) * @return {@code true} if the comments are disabled otherwise {@code false} (default)
* @see CommentsExtractor#isCommentsDisabled() * @see CommentsExtractor#isCommentsDisabled()
*/ */
@ -102,8 +100,6 @@ public final class CommentsInfo extends ListInfo<CommentsInfoItem> {
} }
/** /**
* @param commentsDisabled <code>true</code> if the comments are disabled otherwise <code>false</code>
* @apiNote Warning: This method is experimental and may get removed in a future release.
* @param commentsDisabled {@code true} if the comments are disabled otherwise {@code false} * @param commentsDisabled {@code true} if the comments are disabled otherwise {@code false}
*/ */
public void setCommentsDisabled(final boolean commentsDisabled) { public void setCommentsDisabled(final boolean commentsDisabled) {
@ -124,7 +120,7 @@ public final class CommentsInfo extends ListInfo<CommentsInfoItem> {
* *
* @param commentsCount the commentsCount to set. * @param commentsCount the commentsCount to set.
*/ */
public void setCommentsCount(int commentsCount) { public void setCommentsCount(final int commentsCount) {
this.commentsCount = commentsCount; this.commentsCount = commentsCount;
} }
} }

View file

@ -1,19 +1,8 @@
package org.schabi.newpipe.extractor.services.youtube.extractors; package org.schabi.newpipe.extractor.services.youtube.extractors;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse; import com.grack.nanojson.JsonArray;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; import com.grack.nanojson.JsonObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; import com.grack.nanojson.JsonWriter;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.comments.CommentsExtractor; import org.schabi.newpipe.extractor.comments.CommentsExtractor;
@ -25,27 +14,33 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.localization.Localization; import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.JsonUtils;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonWriter;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
public class YoutubeCommentsExtractor extends CommentsExtractor { public class YoutubeCommentsExtractor extends CommentsExtractor {
private JsonObject nextResponse; /**
* The initial request's continuation token.
* Since we need to make two requests to get the comments,
*/
private String initialToken;
/** /**
* Caching mechanism and holder of the commentsDisabled value. * Whether comments are disabled on video.
* <br/>
* Initial value = empty -> unknown if comments are disabled or not<br/>
* Some method calls {@link #findInitialCommentsToken()}
* -> value is set<br/>
* If the method or another one that is depending on disabled comments
* is now called again, the method execution can avoid unnecessary calls
*/ */
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") private boolean commentsDisabled = true;
private Optional<Boolean> optCommentsDisabled = Optional.empty();
/** /**
* The second ajax <b>/next</b> response. * The second ajax <b>/next</b> response.
*/ */
@ -63,31 +58,25 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
throws IOException, ExtractionException { throws IOException, ExtractionException {
// Check if findInitialCommentsToken was already called and optCommentsDisabled initialized // Check if findInitialCommentsToken was already called and optCommentsDisabled initialized
if (optCommentsDisabled.orElse(false)) { if (commentsDisabled) {
return getInfoItemsPageForDisabledComments(); return getInfoItemsPageForDisabledComments();
} }
// Get the token return getPage(getNextPage(this.initialToken));
final String commentsToken = findInitialCommentsToken();
// Check if the comments have been disabled
if (optCommentsDisabled.get()) {
return getInfoItemsPageForDisabledComments();
}
return getPage(getNextPage(commentsToken));
} }
/** /**
* Finds the initial comments token and initializes commentsDisabled. * Finds the initial comments token and initializes commentsDisabled.
* <br/> * <br/>
* Also sets {@link #optCommentsDisabled}. * Also sets {@link #commentsDisabled}.
* *
* @return the continuation token or null if none was found * @return the continuation token or null if none was found
*/ */
@Nullable @Nullable
private String findInitialCommentsToken() throws ExtractionException { private String findInitialCommentsToken(final JsonObject nextResponse)
throws ExtractionException {
final String token = JsonUtils.getArray(nextResponse, final String token = JsonUtils.getArray(nextResponse,
"contents.twoColumnWatchNextResults.results.results.contents") "contents.twoColumnWatchNextResults.results.results.contents")
.stream() .stream()
// Only use JsonObjects // Only use JsonObjects
.filter(JsonObject.class::isInstance) .filter(JsonObject.class::isInstance)
@ -118,7 +107,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
.orElse(null); .orElse(null);
// The comments are disabled if we couldn't get a token // The comments are disabled if we couldn't get a token
optCommentsDisabled = Optional.of(token == null); commentsDisabled = token == null;
return token; return token;
} }
@ -129,9 +118,9 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
} }
@Nullable @Nullable
private Page getNextPage(@Nonnull final JsonObject ajaxJson) throws ExtractionException { private Page getNextPage(@Nonnull final JsonObject jsonObject) throws ExtractionException {
final JsonArray onResponseReceivedEndpoints = final JsonArray onResponseReceivedEndpoints =
ajaxJson.getArray("onResponseReceivedEndpoints"); jsonObject.getArray("onResponseReceivedEndpoints");
// Prevent ArrayIndexOutOfBoundsException // Prevent ArrayIndexOutOfBoundsException
if (onResponseReceivedEndpoints.isEmpty()) { if (onResponseReceivedEndpoints.isEmpty()) {
@ -179,19 +168,23 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
@Override @Override
public InfoItemsPage<CommentsInfoItem> getPage(final Page page) public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
throws IOException, ExtractionException { throws IOException, ExtractionException {
if (optCommentsDisabled.orElse(false)) {
if (commentsDisabled) {
return getInfoItemsPageForDisabledComments(); return getInfoItemsPageForDisabledComments();
} }
if (page == null || isNullOrEmpty(page.getId())) { if (page == null || isNullOrEmpty(page.getId())) {
throw new IllegalArgumentException("Page doesn't have the continuation."); throw new IllegalArgumentException("Page doesn't have the continuation.");
} }
final Localization localization = getExtractorLocalization(); final Localization localization = getExtractorLocalization();
// @formatter:off
final byte[] body = JsonWriter.string( final byte[] body = JsonWriter.string(
prepareDesktopJsonBuilder(localization, getExtractorContentCountry()) prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
.value("continuation", page.getId()) .value("continuation", page.getId())
.done()) .done())
.getBytes(StandardCharsets.UTF_8); .getBytes(StandardCharsets.UTF_8);
// @formatter:on
this.ajaxJson = getJsonPostResponse("next", body, localization); this.ajaxJson = getJsonPostResponse("next", body, localization);
@ -201,7 +194,8 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
return new InfoItemsPage<>(collector, getNextPage(ajaxJson)); return new InfoItemsPage<>(collector, getNextPage(ajaxJson));
} }
private void collectCommentsFrom(final CommentsInfoItemsCollector collector) throws ParsingException { private void collectCommentsFrom(final CommentsInfoItemsCollector collector)
throws ParsingException {
final JsonArray onResponseReceivedEndpoints = final JsonArray onResponseReceivedEndpoints =
ajaxJson.getArray("onResponseReceivedEndpoints"); ajaxJson.getArray("onResponseReceivedEndpoints");
@ -259,25 +253,21 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
public void onFetchPage(@Nonnull final Downloader downloader) public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException { throws IOException, ExtractionException {
final Localization localization = getExtractorLocalization(); final Localization localization = getExtractorLocalization();
// @formatter:off
final byte[] body = JsonWriter.string( final byte[] body = JsonWriter.string(
prepareDesktopJsonBuilder(localization, getExtractorContentCountry()) prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
.value("videoId", getId()) .value("videoId", getId())
.done()) .done())
.getBytes(StandardCharsets.UTF_8); .getBytes(StandardCharsets.UTF_8);
// @formatter:on
nextResponse = getJsonPostResponse("next", body, localization); initialToken = findInitialCommentsToken(getJsonPostResponse("next", body, localization));
} }
@Override @Override
public boolean isCommentsDisabled() throws ExtractionException { public boolean isCommentsDisabled() {
// Check if commentsDisabled has to be initialized return commentsDisabled;
if (!optCommentsDisabled.isPresent()) {
// Initialize commentsDisabled
this.findInitialCommentsToken();
}
return optCommentsDisabled.get();
} }
@Override @Override