From e9bbc5dace095777c6ca0361506f2662647a95b0 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Sun, 4 Dec 2022 14:32:50 +0100 Subject: [PATCH] Use full names for comment authors and their mentions in replies if available --- .../SoundcloudCommentsInfoItemExtractor.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java index 79b27f1b2..a6d2b9a7f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java @@ -20,20 +20,30 @@ import java.util.Objects; public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtractor { public static final String USER = "user"; public static final String BODY = "body"; + public static final String USER_PERMALINK = "permalink"; private final JsonObject json; private final int index; private final JsonObject item; private final String url; + private final JsonObject superComment; private int replyCount = CommentsInfoItem.UNKNOWN_REPLY_COUNT; 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.index = index; this.item = item; 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 @@ -43,12 +53,30 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr @Override 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 = "" + + "@" + user.getString("full_name") + ""; + commentContent = commentContent + .replace("@" + user.getString(USER_PERMALINK), link) + .replace("@" + superComment.getInt("user_id"), link); + + return new Description(commentContent, Description.HTML); } @Override 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 @@ -105,7 +133,7 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr ServiceList.SoundCloud.getServiceId()); final JsonArray jsonArray = new JsonArray(); // 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. final JsonArray allItems = json.getArray(SoundcloudCommentsExtractor.COLLECTION); for (int i = index + 1; i < allItems.size(); i++) { @@ -114,7 +142,8 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr if (commentContent.startsWith(mention)) { replies.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()) { // Only the comments directly after the original comment // starting with the mention of the comment's creator