Use full names for comment authors and their mentions in replies if available

This commit is contained in:
TobiGr 2022-12-04 14:32:50 +01:00
parent d41a4a1350
commit e9bbc5dace

View File

@ -20,20 +20,30 @@ import java.util.Objects;
public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtractor { public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
public static final String USER = "user"; public static final String USER = "user";
public static final String BODY = "body"; public static final String BODY = "body";
public static final String USER_PERMALINK = "permalink";
private final JsonObject json; private final JsonObject json;
private final int index; private final int index;
private final JsonObject item; private final JsonObject item;
private final String url; private final String url;
private final JsonObject superComment;
private int replyCount = CommentsInfoItem.UNKNOWN_REPLY_COUNT; private int replyCount = CommentsInfoItem.UNKNOWN_REPLY_COUNT;
private Page repliesPage = null; private Page repliesPage = null;
public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index, final JsonObject item, final String url) { public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index,
final JsonObject item, final String url,
@Nullable final JsonObject superComment) {
this.json = json; this.json = json;
this.index = index; this.index = index;
this.item = item; this.item = item;
this.url = url; this.url = url;
this.superComment = superComment;
}
public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index,
final JsonObject item, final String url) {
this(json, index, item, url, null);
} }
@Override @Override
@ -43,12 +53,30 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr
@Override @Override
public Description getCommentText() { public Description getCommentText() {
return new Description(item.getString(BODY), Description.PLAIN_TEXT); String commentContent = item.getString(BODY);
if (superComment == null) {
return new Description(commentContent, Description.PLAIN_TEXT);
}
// This comment is a reply to another comment.
// Therefore, the comment starts with the mention of the original comment's author.
// The account is automatically linked by the SoundCloud web UI.
// We need to do this manually.
final JsonObject user = superComment.getObject("user");
final String link = "<a href=\"" + user.getString("permalink_url") + "\">"
+ "@" + user.getString("full_name") + "</a>";
commentContent = commentContent
.replace("@" + user.getString(USER_PERMALINK), link)
.replace("@" + superComment.getInt("user_id"), link);
return new Description(commentContent, Description.HTML);
} }
@Override @Override
public String getUploaderName() { public String getUploaderName() {
return item.getObject(USER).getString("username"); if (isNullOrEmpty(user.getString("full_name"))) {
return user.getString("username");
}
return user.getString("full_name");
} }
@Override @Override
@ -105,7 +133,7 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr
ServiceList.SoundCloud.getServiceId()); ServiceList.SoundCloud.getServiceId());
final JsonArray jsonArray = new JsonArray(); final JsonArray jsonArray = new JsonArray();
// Replies start with the mention of the user who created the original comment. // Replies start with the mention of the user who created the original comment.
final String mention = "@" + item.getObject(USER).getString("permalink"); final String mention = "@" + item.getObject(USER).getString(USER_PERMALINK);
// Loop through all comments which come after the original comment to find its replies. // Loop through all comments which come after the original comment to find its replies.
final JsonArray allItems = json.getArray(SoundcloudCommentsExtractor.COLLECTION); final JsonArray allItems = json.getArray(SoundcloudCommentsExtractor.COLLECTION);
for (int i = index + 1; i < allItems.size(); i++) { for (int i = index + 1; i < allItems.size(); i++) {
@ -114,7 +142,8 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr
if (commentContent.startsWith(mention)) { if (commentContent.startsWith(mention)) {
replies.add(comment); replies.add(comment);
jsonArray.add(comment); jsonArray.add(comment);
collector.commit(new SoundcloudCommentsInfoItemExtractor(json, i, comment, url)); collector.commit(new SoundcloudCommentsInfoItemExtractor(
json, i, comment, url, item));
} else if (!commentContent.startsWith("@") || replies.isEmpty()) { } else if (!commentContent.startsWith("@") || replies.isEmpty()) {
// Only the comments directly after the original comment // Only the comments directly after the original comment
// starting with the mention of the comment's creator // starting with the mention of the comment's creator