Skip to content

Commit 75d875f

Browse files
committed
Use full names for comment authors and their mentions in replies
1 parent 3d512d5 commit 75d875f

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,30 @@
2020
public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
2121
public static final String USER = "user";
2222
public static final String BODY = "body";
23+
public static final String USER_PERMALINK = "permalink";
2324

2425
private final JsonObject json;
2526
private final int index;
2627
private final JsonObject item;
2728
private final String url;
29+
private final JsonObject superComment;
2830

2931
private int replyCount = CommentsInfoItem.UNKNOWN_REPLY_COUNT;
3032
private Page repliesPage = null;
3133

32-
public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index, final JsonObject item, final String url) {
34+
public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index,
35+
final JsonObject item, final String url,
36+
@Nullable final JsonObject superComment) {
3337
this.json = json;
3438
this.index = index;
3539
this.item = item;
3640
this.url = url;
41+
this.superComment = superComment;
42+
}
43+
44+
public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index,
45+
final JsonObject item, final String url) {
46+
this(json, index, item, url, null);
3747
}
3848

3949
@Override
@@ -43,12 +53,27 @@ public String getCommentId() {
4353

4454
@Override
4555
public Description getCommentText() {
46-
return new Description(item.getString(BODY), Description.PLAIN_TEXT);
56+
String commentContent = item.getString(BODY);
57+
if (superComment == null) {
58+
return new Description(commentContent, Description.PLAIN_TEXT);
59+
}
60+
// This comment is a reply to another comment.
61+
// Therefore, the comment starts with the mention of the original comment's author.
62+
// The account is automatically linked by the SoundCloud web UI.
63+
// We need to do this manually.
64+
final JsonObject user = superComment.getObject("user");
65+
final String link = "<a href=\"" + user.getString("permalink_url") + "\">"
66+
+ "@" + user.getString("full_name") + "</a>";
67+
commentContent = commentContent
68+
.replace("@" + user.getString(USER_PERMALINK), link)
69+
.replace("@" + superComment.getInt("user_id"), link);
70+
71+
return new Description(commentContent, Description.HTML);
4772
}
4873

4974
@Override
5075
public String getUploaderName() {
51-
return item.getObject(USER).getString("username");
76+
return item.getObject(USER).getString("full_name");
5277
}
5378

5479
@Override
@@ -105,7 +130,7 @@ public Page getReplies() {
105130
ServiceList.SoundCloud.getServiceId());
106131
final JsonArray jsonArray = new JsonArray();
107132
// Replies start with the mention of the user who created the original comment.
108-
final String mention = "@" + item.getObject(USER).getString("permalink");
133+
final String mention = "@" + item.getObject(USER).getString(USER_PERMALINK);
109134
// Loop through all comments which come after the original comment to find its replies.
110135
final JsonArray allItems = json.getArray(SoundcloudCommentsExtractor.COLLECTION);
111136
for (int i = index + 1; i < allItems.size(); i++) {
@@ -114,7 +139,8 @@ public Page getReplies() {
114139
if (commentContent.startsWith(mention)) {
115140
replies.add(comment);
116141
jsonArray.add(comment);
117-
collector.commit(new SoundcloudCommentsInfoItemExtractor(json, i, comment, url));
142+
collector.commit(new SoundcloudCommentsInfoItemExtractor(
143+
json, i, comment, url, item));
118144
} else if (!commentContent.startsWith("@") || replies.isEmpty()) {
119145
// Only the comments directly after the original comment
120146
// starting with the mention of the comment's creator

0 commit comments

Comments
 (0)