Skip to content

Commit 3d512d5

Browse files
committed
Only display direct replies as replies
1 parent f196155 commit 3d512d5

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page) throws Extractio
6464
final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(
6565
getServiceId());
6666

67+
// Replies typically do not have a next page, but that's not always the case.
68+
final boolean hasNextPage;
6769
if (page.hasContent()) {
6870
// This page contains the whole previously fetched comments.
6971
// We need to get the comments which are replies to the comment with the page's id.
7072
json = (JsonObject) page.getContent();
7173
try {
7274
final int commentId = Integer.parseInt(page.getId());
73-
collectRepliesFrom(collector, json, commentId, page.getUrl());
75+
hasNextPage = collectRepliesFrom(collector, json, commentId, page.getUrl());
7476
} catch (final NumberFormatException e) {
7577
throw new ParsingException("Got invalid comment id", e);
7678
}
@@ -81,13 +83,18 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page) throws Extractio
8183

8284
try {
8385
json = JsonParser.object().from(response.responseBody());
86+
hasNextPage = json.has("next_href");
8487
} catch (final JsonParserException e) {
8588
throw new ParsingException("Could not parse json", e);
8689
}
8790
collectStreamsFrom(collector, json);
8891
}
8992

90-
return new InfoItemsPage<>(collector, new Page(json.getString("next_href")));
93+
if (hasNextPage) {
94+
return new InfoItemsPage<>(collector, new Page(json.getString("next_href")));
95+
} else {
96+
return new InfoItemsPage<>(collector, null);
97+
}
9198
}
9299

93100
@Override
@@ -108,12 +115,13 @@ private void collectStreamsFrom(final CommentsInfoItemsCollector collector,
108115
}
109116
}
110117

111-
private void collectRepliesFrom(final CommentsInfoItemsCollector collector,
118+
private boolean collectRepliesFrom(final CommentsInfoItemsCollector collector,
112119
final JsonObject json,
113120
final int id,
114121
final String url) throws ParsingException {
115122
JsonObject originalComment = null;
116123
final JsonArray entries = json.getArray(COLLECTION);
124+
boolean moreReplies = false;
117125
for (int i = 0; i < entries.size(); i++) {
118126
final JsonObject comment = entries.getObject(i);
119127
if (comment.getInt("id") == id) {
@@ -123,10 +131,15 @@ private void collectRepliesFrom(final CommentsInfoItemsCollector collector,
123131
if (originalComment != null
124132
&& SoundcloudParsingHelper.isReplyTo(originalComment, comment)) {
125133
collector.commit(new SoundcloudCommentsInfoItemExtractor(
126-
json, i, entries.getObject(i), url));
127-
134+
json, i, entries.getObject(i), url, originalComment));
135+
// There might be more replies to the originalComment,
136+
// especially if the original comment is at the end of the list.
137+
if (i == entries.size() - 1 && json.has("next_href")) {
138+
moreReplies = true;
139+
}
128140
}
129141
}
142+
return moreReplies;
130143
}
131144

132145
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudCommentsLinkHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public final class SoundcloudCommentsLinkHandlerFactory extends ListLinkHandlerF
1515
private static final SoundcloudCommentsLinkHandlerFactory INSTANCE =
1616
new SoundcloudCommentsLinkHandlerFactory();
1717

18-
private static final String OFFSET_PATTERN = "https://api-v2.soundcloud.com/tracks/([0-9a-z]+)/comments?([0-9a-z/&])?offset=([0-9])+"
18+
private static final String OFFSET_PATTERN = "https://api-v2.soundcloud.com/tracks/"
19+
+ "([0-9a-z]+)/comments?([0-9a-z/&])?offset=([0-9])+";
1920

2021
private SoundcloudCommentsLinkHandlerFactory() {
2122
}

0 commit comments

Comments
 (0)