Skip to content

Commit

Permalink
Cleanup and remove optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed Dec 8, 2022
1 parent 533b2fa commit d158f51
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static CommentsInfo getInfo(final CommentsExtractor commentsExtractor)
commentsInfo.setRelatedItems(initialCommentsPage.getItems());
try {
commentsInfo.setCommentsCount(commentsExtractor.getCommentsCount());
} catch (Exception e) {
} catch (final Exception e) {
commentsInfo.addError(e);
}
commentsInfo.setNextPage(initialCommentsPage.getNextPage());
Expand Down Expand Up @@ -92,8 +92,6 @@ public void setCommentsExtractor(final CommentsExtractor commentsExtractor) {
}

/**
* @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)
* @see CommentsExtractor#isCommentsDisabled()
*/
Expand All @@ -102,8 +100,6 @@ public boolean isCommentsDisabled() {
}

/**
* @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}
*/
public void setCommentsDisabled(final boolean commentsDisabled) {
Expand All @@ -124,7 +120,7 @@ public int getCommentsCount() {
*
* @param commentsCount the commentsCount to set.
*/
public void setCommentsCount(int commentsCount) {
public void setCommentsCount(final int commentsCount) {
this.commentsCount = commentsCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
package org.schabi.newpipe.extractor.services.youtube.extractors;

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;

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 com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonWriter;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.comments.CommentsExtractor;
Expand All @@ -25,27 +14,33 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.localization.Localization;
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 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 {

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.
* <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
* Whether comments are disabled on video.
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private Optional<Boolean> optCommentsDisabled = Optional.empty();
private boolean commentsDisabled = true;

/**
* The second ajax <b>/next</b> response.
*/
Expand All @@ -63,31 +58,25 @@ public InfoItemsPage<CommentsInfoItem> getInitialPage()
throws IOException, ExtractionException {

// Check if findInitialCommentsToken was already called and optCommentsDisabled initialized
if (optCommentsDisabled.orElse(false)) {
return getInfoItemsPageForDisabledComments();
}

// Get the token
final String commentsToken = findInitialCommentsToken();
// Check if the comments have been disabled
if (optCommentsDisabled.get()) {
if (commentsDisabled) {
return getInfoItemsPageForDisabledComments();
}

return getPage(getNextPage(commentsToken));
return getPage(getNextPage(this.initialToken));
}

/**
* Finds the initial comments token and initializes commentsDisabled.
* <br/>
* Also sets {@link #optCommentsDisabled}.
* Also sets {@link #commentsDisabled}.
*
* @return the continuation token or null if none was found
*/
@Nullable
private String findInitialCommentsToken() throws ExtractionException {
private String findInitialCommentsToken(final JsonObject nextResponse)
throws ExtractionException {
final String token = JsonUtils.getArray(nextResponse,
"contents.twoColumnWatchNextResults.results.results.contents")
"contents.twoColumnWatchNextResults.results.results.contents")
.stream()
// Only use JsonObjects
.filter(JsonObject.class::isInstance)
Expand Down Expand Up @@ -118,7 +107,7 @@ private String findInitialCommentsToken() throws ExtractionException {
.orElse(null);

// The comments are disabled if we couldn't get a token
optCommentsDisabled = Optional.of(token == null);
commentsDisabled = token == null;

return token;
}
Expand All @@ -129,9 +118,9 @@ private InfoItemsPage<CommentsInfoItem> getInfoItemsPageForDisabledComments() {
}

@Nullable
private Page getNextPage(@Nonnull final JsonObject ajaxJson) throws ExtractionException {
private Page getNextPage(@Nonnull final JsonObject jsonObject) throws ExtractionException {
final JsonArray onResponseReceivedEndpoints =
ajaxJson.getArray("onResponseReceivedEndpoints");
jsonObject.getArray("onResponseReceivedEndpoints");

// Prevent ArrayIndexOutOfBoundsException
if (onResponseReceivedEndpoints.isEmpty()) {
Expand Down Expand Up @@ -179,19 +168,23 @@ private Page getNextPage(final String continuation) throws ParsingException {
@Override
public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
throws IOException, ExtractionException {
if (optCommentsDisabled.orElse(false)) {

if (commentsDisabled) {
return getInfoItemsPageForDisabledComments();
}

if (page == null || isNullOrEmpty(page.getId())) {
throw new IllegalArgumentException("Page doesn't have the continuation.");
}

final Localization localization = getExtractorLocalization();
// @formatter:off
final byte[] body = JsonWriter.string(
prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
.value("continuation", page.getId())
.done())
.getBytes(StandardCharsets.UTF_8);
// @formatter:on

this.ajaxJson = getJsonPostResponse("next", body, localization);

Expand All @@ -201,7 +194,8 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
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 =
ajaxJson.getArray("onResponseReceivedEndpoints");
Expand Down Expand Up @@ -259,25 +253,21 @@ private void collectCommentsFrom(final CommentsInfoItemsCollector collector) thr
public void onFetchPage(@Nonnull final Downloader downloader)
throws IOException, ExtractionException {
final Localization localization = getExtractorLocalization();
// @formatter:off
final byte[] body = JsonWriter.string(
prepareDesktopJsonBuilder(localization, getExtractorContentCountry())
.value("videoId", getId())
.done())
.getBytes(StandardCharsets.UTF_8);
// @formatter:on

nextResponse = getJsonPostResponse("next", body, localization);
initialToken = findInitialCommentsToken(getJsonPostResponse("next", body, localization));
}


@Override
public boolean isCommentsDisabled() throws ExtractionException {
// Check if commentsDisabled has to be initialized
if (!optCommentsDisabled.isPresent()) {
// Initialize commentsDisabled
this.findInitialCommentsToken();
}

return optCommentsDisabled.get();
public boolean isCommentsDisabled() {
return commentsDisabled;
}

@Override
Expand Down

0 comments on commit d158f51

Please sign in to comment.