From f79ce1f52a451c9b8722f09d9cb6885d91446918 Mon Sep 17 00:00:00 2001
From: litetex <40789489+litetex@users.noreply.github.com>
Date: Sat, 12 Feb 2022 15:49:33 +0100
Subject: [PATCH] Refactored YoutubeCommentsExtractor
* Use Java Streaming API
* Use StandardCharsets
* Prevented several NPEs/ArrayIndexOutOfBound
* Reformatted some code so that it's easier readable
---
.../extractors/YoutubeCommentsExtractor.java | 157 ++++++++++--------
1 file changed, 87 insertions(+), 70 deletions(-)
diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java
index d5f65264..32cf559b 100644
--- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java
+++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java
@@ -2,10 +2,10 @@ 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.prepareDesktopJsonBuilder;
-import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
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;
@@ -17,7 +17,6 @@ import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.comments.CommentsExtractor;
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
-import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor;
import org.schabi.newpipe.extractor.comments.CommentsInfoItemsCollector;
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -38,7 +37,7 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
* Caching mechanism and holder of the commentsDisabled value.
*
* Initial value = empty -> unknown if comments are disabled or not
- * Some method calls {@link YoutubeCommentsExtractor#findInitialCommentsToken()}
+ * Some method calls {@link #findInitialCommentsToken()}
* -> value is set
* If the method or another one that is depending on disabled comments
* is now called again, the method execution can avoid unnecessary calls
@@ -74,45 +73,46 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
/**
* Finds the initial comments token and initializes commentsDisabled.
+ * Also set
*
* @return the continuation token or null if none was found
*/
@Nullable
private String findInitialCommentsToken() throws ExtractionException {
+ final String token = JsonUtils.getArray(nextResponse,
+ "contents.twoColumnWatchNextResults.results.results.contents")
+ .stream()
+ // Only use JsonObjects
+ .filter(JsonObject.class::isInstance)
+ .map(JsonObject.class::cast)
+ // Only process JsonObjects that have a itemSectionRenderer
+ .filter(jObj -> jObj.has("itemSectionRenderer"))
+ // Check if the comment-section is present
+ .filter(jObj -> {
+ try {
+ return "comments-section".equals(
+ JsonUtils.getString(jObj, "itemSectionRenderer.targetId"));
+ } catch (final ParsingException ex) {
+ return false;
+ }
+ })
+ .findFirst()
+ // Extract the token (or null in case of error)
+ .map(itemSectionRenderer -> {
+ try {
+ return JsonUtils.getString(
+ itemSectionRenderer
+ .getObject("itemSectionRenderer")
+ .getArray("contents").getObject(0),
+ "continuationItemRenderer.continuationEndpoint.continuationCommand.token");
+ } catch (final ParsingException ex) {
+ return null;
+ }
+ })
+ .orElse(null);
- final JsonArray jArray = JsonUtils.getArray(nextResponse,
- "contents.twoColumnWatchNextResults.results.results.contents");
-
- final Optional