Skip to content

Commit

Permalink
Only display direct replies as replies
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiGr committed Dec 4, 2022
1 parent f196155 commit 3d512d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ public InfoItemsPage<CommentsInfoItem> 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);
}
Expand All @@ -81,13 +83,18 @@ public InfoItemsPage<CommentsInfoItem> 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
Expand All @@ -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) {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down

0 comments on commit 3d512d5

Please sign in to comment.