From 3d512d5777d987b6a7541fb07929d058e255c7ea Mon Sep 17 00:00:00 2001 From: TobiGr Date: Sat, 3 Dec 2022 16:29:52 +0100 Subject: [PATCH] Only display direct replies as replies --- .../SoundcloudCommentsExtractor.java | 23 +++++++++++++++---- .../SoundcloudCommentsLinkHandlerFactory.java | 3 ++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java index d4afe9f09c..d51a5fbc94 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java @@ -64,13 +64,15 @@ public InfoItemsPage getPage(final Page page) throws Extractio final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector( getServiceId()); + // Replies typically do not have a next page, but that's not always the case. + final boolean hasNextPage; if (page.hasContent()) { // This page contains the whole previously fetched comments. // We need to get the comments which are replies to the comment with the page's id. json = (JsonObject) page.getContent(); try { final int commentId = Integer.parseInt(page.getId()); - collectRepliesFrom(collector, json, commentId, page.getUrl()); + hasNextPage = collectRepliesFrom(collector, json, commentId, page.getUrl()); } catch (final NumberFormatException e) { throw new ParsingException("Got invalid comment id", e); } @@ -81,13 +83,18 @@ public InfoItemsPage getPage(final Page page) throws Extractio try { json = JsonParser.object().from(response.responseBody()); + hasNextPage = json.has("next_href"); } catch (final JsonParserException e) { throw new ParsingException("Could not parse json", e); } collectStreamsFrom(collector, json); } - return new InfoItemsPage<>(collector, new Page(json.getString("next_href"))); + if (hasNextPage) { + return new InfoItemsPage<>(collector, new Page(json.getString("next_href"))); + } else { + return new InfoItemsPage<>(collector, null); + } } @Override @@ -108,12 +115,13 @@ private void collectStreamsFrom(final CommentsInfoItemsCollector collector, } } - private void collectRepliesFrom(final CommentsInfoItemsCollector collector, + private boolean collectRepliesFrom(final CommentsInfoItemsCollector collector, final JsonObject json, final int id, final String url) throws ParsingException { JsonObject originalComment = null; final JsonArray entries = json.getArray(COLLECTION); + boolean moreReplies = false; for (int i = 0; i < entries.size(); i++) { final JsonObject comment = entries.getObject(i); if (comment.getInt("id") == id) { @@ -123,10 +131,15 @@ private void collectRepliesFrom(final CommentsInfoItemsCollector collector, if (originalComment != null && SoundcloudParsingHelper.isReplyTo(originalComment, comment)) { collector.commit(new SoundcloudCommentsInfoItemExtractor( - json, i, entries.getObject(i), url)); - + json, i, entries.getObject(i), url, originalComment)); + // There might be more replies to the originalComment, + // especially if the original comment is at the end of the list. + if (i == entries.size() - 1 && json.has("next_href")) { + moreReplies = true; + } } } + return moreReplies; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudCommentsLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudCommentsLinkHandlerFactory.java index 39e124698a..775ee10486 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudCommentsLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudCommentsLinkHandlerFactory.java @@ -15,7 +15,8 @@ public final class SoundcloudCommentsLinkHandlerFactory extends ListLinkHandlerF private static final SoundcloudCommentsLinkHandlerFactory INSTANCE = new SoundcloudCommentsLinkHandlerFactory(); - private static final String OFFSET_PATTERN = "https://api-v2.soundcloud.com/tracks/([0-9a-z]+)/comments?([0-9a-z/&])?offset=([0-9])+" + private static final String OFFSET_PATTERN = "https://api-v2.soundcloud.com/tracks/" + + "([0-9a-z]+)/comments?([0-9a-z/&])?offset=([0-9])+"; private SoundcloudCommentsLinkHandlerFactory() { }