Skip to content

Commit

Permalink
add music/playlist/album/dj/djmusic comment and hot comment
Browse files Browse the repository at this point in the history
  • Loading branch information
FengLiuFeseliud committed Jul 3, 2024
1 parent e849d7a commit 089d915
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 225 deletions.
459 changes: 289 additions & 170 deletions src/main/java/fengliu/cloudmusic/command/MusicCommand.java

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions src/main/java/fengliu/cloudmusic/music163/ICanComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
import java.util.Map;

public interface ICanComment {
String getCommentId();

ApiPage getComments(boolean hot);

default ApiPage comments(HttpClient api, long id, boolean hot) {
default ApiPage comments(HttpClient api, long id, String threadId, boolean hot) {
Map<String, Object> data = new HashMap<String, Object>();
data.put("rid", id);
data.put("limit", 24);

String commentId = this.getCommentId();
String path = "/api/v1/resource/%s/%s".formatted(hot ? "hotcomments" : "comments", commentId);
String path = "/api/v1/resource/%s/%s".formatted(hot ? "hotcomments" : "comments", threadId);
JsonObject json = api.POST_API(path, data);

int total = json.get("total").getAsInt();
Expand All @@ -40,11 +37,11 @@ protected JsonArray getNewPageDataJsonArray(JsonObject result) {

@Override
protected TextClickItem putPageItem(Object data) {
Comment comment = new Comment(this.api, (JsonObject) data, commentId);
Comment comment = new Comment(this.api, (JsonObject) data, threadId);
return new TextClickItem(
Text.literal(comment.getPageItem()),
Text.translatable(IdUtil.getShowInfo("page.comment")),
"/cloudmusic comment %s %s".formatted(comment.id, commentId)
"/cloudmusic comment %s %s".formatted(comment.id, threadId)
);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/fengliu/cloudmusic/music163/Music163Obj.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fengliu.cloudmusic.music163;

import com.google.gson.JsonObject;

import fengliu.cloudmusic.util.HttpClient;

public class Music163Obj {
Expand All @@ -15,4 +14,6 @@ public class Music163Obj {
public Music163Obj(HttpClient api, JsonObject data){
this.api = api;
}


}
27 changes: 18 additions & 9 deletions src/main/java/fengliu/cloudmusic/music163/data/Album.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fengliu.cloudmusic.util.HttpClient;
import fengliu.cloudmusic.util.IdUtil;
import fengliu.cloudmusic.util.TextClickItem;
import fengliu.cloudmusic.util.page.ApiPage;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand All @@ -19,7 +20,7 @@
/**
* 专辑对象
*/
public class Album extends Music163Obj implements IMusicList, ICanSubscribe {
public class Album extends Music163Obj implements IMusicList, ICanSubscribe, ICanComment {
public final long id;
public final String name;
public final String cover;
Expand All @@ -29,6 +30,7 @@ public class Album extends Music163Obj implements IMusicList, ICanSubscribe {
public final String[] description;
private JsonArray songs;
private List<IMusic> musics;
public final String threadId;

public Album(HttpClient api, JsonObject album) {
super(api, album);
Expand All @@ -41,22 +43,28 @@ public Album(HttpClient api, JsonObject album) {
this.size = album.get("size").getAsInt();
this.artists = album.get("artists").getAsJsonArray();
this.alias = album.get("alias").getAsJsonArray();
if(!album.get("description").isJsonNull()){
if (!album.get("description").isJsonNull()) {
this.description = album.get("description").getAsString().split("\n");
} else {
this.description = null;
}
this.threadId = "R_AL_3_%s".formatted(this.id);
}

@Override
public ApiPage getComments(boolean hot) {
return this.comments(this.api, this.id, this.threadId, hot);
}

@Override
public void printToChatHud(FabricClientCommandSource source) {
source.sendFeedback(Text.literal(""));

if(this.alias.size() == 0){
if (this.alias.size() == 0) {
source.sendFeedback(Text.literal(this.name));
}else{
} else {
StringBuilder aliasName = new StringBuilder();
for(JsonElement alia: this.alias.asList()){
for (JsonElement alia : this.alias.asList()) {
aliasName.append(alia.getAsString()).append(" / ");
}
aliasName = new StringBuilder(aliasName.substring(0, aliasName.length() - 3));
Expand Down Expand Up @@ -94,6 +102,8 @@ public void printToChatHud(FabricClientCommandSource source) {

source.sendFeedback(TextClickItem.combine(
new TextClickItem("play", "/cloudmusic album play " + this.id),
new TextClickItem("hot.comment", "/cloudmusic album hotComment " + this.id),
new TextClickItem("comment", "/cloudmusic album comment " + this.id),
new TextClickItem("subscribe", "/cloudmusic album subscribe " + this.id),
new TextClickItem("unsubscribe", "/cloudmusic album unsubscribe " + this.id),
new TextClickItem("shar", Shares.ALBUM.getShar(this.id))
Expand All @@ -102,7 +112,7 @@ public void printToChatHud(FabricClientCommandSource source) {

@Override
public List<IMusic> getMusics() {
if(this.musics != null){
if (this.musics != null) {
return this.musics;
}

Expand All @@ -119,7 +129,7 @@ public void subscribe() {
data.put("id", this.id);

JsonObject json = this.api.POST_API("/api/album/sub", data);
if(json.has("message")){
if (json.has("message")) {
throw new ActionException(json.get("message").getAsString());
}
}
Expand All @@ -130,9 +140,8 @@ public void unsubscribe() {
data.put("id", this.id);

JsonObject json = this.api.POST_API("/api/album/unsub", data);
if(json.has("message")){
if (json.has("message")) {
throw new ActionException(json.get("message").getAsString());
}
}

}
29 changes: 19 additions & 10 deletions src/main/java/fengliu/cloudmusic/music163/data/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class Comment extends Music163Obj implements IPrint {
public final JsonArray beReplied;
public final JsonObject ipLocation;


/**
* 初始化对象
*
Expand Down Expand Up @@ -78,25 +77,35 @@ protected TextClickItem putPageItem(Object data) {
};
}

public Text getBeContent() {
JsonObject beReplied = this.beReplied.get(0).getAsJsonObject();
String beContent;
if (!beReplied.get("content").isJsonNull()) {
beContent = beReplied.get("content").getAsString();
} else {
beContent = beReplied.get("status").getAsInt() == -50 ? Text.translatable("cloudmusic.info.page.comment.null").getString() : Text.translatable("cloudmusic.info.page.comment.err.null").getString();
}
return Text.translatable("cloudmusic.info.comment.be.replied", beReplied.get("user").getAsJsonObject().get("nickname").getAsString(),
beReplied.get("ipLocation").getAsJsonObject().get("location").getAsString(), beContent);
}

public String getPageItem() {
return "§b%s - %s: §r§7%s%s - id: %s".formatted(this.user.get("nickname").getAsString(), this.ipLocation.get("location").getAsString(),
if (!this.beReplied.isEmpty()) {
return "%s§r§7 - §b%s - %s: §r§f%s §7- %s - id: %s".formatted(this.getBeContent().getString(), this.user.get("nickname").getAsString(), this.ipLocation.get("location").getAsString(),
this.content, Text.translatable("cloudmusic.page.item.comments.like", this.likedCount).getString(), this.id);
}
return "§b%s - %s: §r§f%s §7- %s - id: %s".formatted(this.user.get("nickname").getAsString(), this.ipLocation.get("location").getAsString(),
this.content, Text.translatable("cloudmusic.page.item.comments.like", this.likedCount).getString(), this.id);
}

@Override
public void printToChatHud(FabricClientCommandSource source) {
if (!this.beReplied.isEmpty()) {
JsonObject beReplied = this.beReplied.get(0).getAsJsonObject();
String beContent = beReplied.get("content").getAsString();
if (beContent == null) {
beContent = beReplied.get("status").getAsInt() == -50 ? Text.translatable("cloudmusic.info.comment.null").toString() : Text.translatable("cloudmusic.info.comment.err.null").toString();
}
source.sendFeedback(Text.translatable("cloudmusic.info.comment.be.replied", beReplied.get("user").getAsJsonObject().get("nickname"),
beReplied.get("ipLocation").getAsJsonObject().get("location").getAsString(), beContent));
source.sendFeedback(this.getBeContent());
source.sendFeedback(Text.literal(""));
}

source.sendFeedback(Text.translatable("cloudmusic.info.comment.content", this.user.get("nickname").getAsString(), this.ipLocation.get("location").getAsString(), this.content));
source.sendFeedback(Text.literal("%s - %s: %s".formatted(this.user.get("nickname").getAsString(), this.ipLocation.get("location").getAsString(), this.content)));
source.sendFeedback(Text.translatable("cloudmusic.info.comment.time", this.timeStr));
source.sendFeedback(Text.translatable("cloudmusic.page.item.comments.like", this.likedCount));
}
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/fengliu/cloudmusic/music163/data/DjMusic.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import com.google.gson.JsonObject;
import fengliu.cloudmusic.config.Configs;
import fengliu.cloudmusic.music163.ActionException;
import fengliu.cloudmusic.music163.IMusic;
import fengliu.cloudmusic.music163.Music163Obj;
import fengliu.cloudmusic.music163.Shares;
import fengliu.cloudmusic.music163.*;
import fengliu.cloudmusic.util.HttpClient;
import fengliu.cloudmusic.util.TextClickItem;
import fengliu.cloudmusic.util.page.ApiPage;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.text.Text;

import java.util.HashMap;
import java.util.Map;

public class DjMusic extends Music163Obj implements IMusic {
public class DjMusic extends Music163Obj implements IMusic, ICanComment {
private final HttpClient api;
public final long id;
public final long mainTrackId;
Expand All @@ -26,6 +24,7 @@ public class DjMusic extends Music163Obj implements IMusic {
public final long likedCount;
public final String[] description;
public final long duration;
public final String threadId;

/**
* 初始化对象
Expand All @@ -51,6 +50,7 @@ public DjMusic(HttpClient api, JsonObject data) {

this.description = data.get("description").getAsString().split("\n");
this.duration = data.get("duration").getAsLong();
this.threadId = "A_DJ_1_%s".formatted(this.id);
}

@Override
Expand Down Expand Up @@ -78,7 +78,7 @@ public String getPlayUrl(){

JsonObject result = playApi.POST_API("/api/song/enhance/player/url/v1", data);
JsonObject music = result.get("data").getAsJsonArray().get(0).getAsJsonObject();
if(music.get("code").getAsInt() != 200){
if (music.get("code").getAsInt() != 200) {
throw new ActionException(Text.translatable("cloudmusic.exception.music.get.url", this.name));
}
return music.get("url").getAsString();
Expand All @@ -89,6 +89,11 @@ public long getDuration() {
return this.duration;
}

@Override
public ApiPage getComments(boolean hot) {
return this.comments(this.api, this.id, this.threadId, hot);
}

@Override
public void printToChatHud(FabricClientCommandSource source) {
source.sendFeedback(Text.literal(""));
Expand Down Expand Up @@ -121,6 +126,8 @@ public void printToChatHud(FabricClientCommandSource source) {

source.sendFeedback(TextClickItem.combine(
new TextClickItem("play", "/cloudmusic dj music play " + this.id),
new TextClickItem("hot.comment", "/cloudmusic dj music hotComment " + this.id),
new TextClickItem("comment", "/cloudmusic dj music comment " + this.id),
new TextClickItem("shar", Shares.DJ_MUSIC.getShar(this.id))
));
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/fengliu/cloudmusic/music163/data/DjRadio.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import fengliu.cloudmusic.config.Configs;
import fengliu.cloudmusic.music163.ICanComment;
import fengliu.cloudmusic.music163.ICanSubscribe;
import fengliu.cloudmusic.music163.IPrint;
import fengliu.cloudmusic.music163.Shares;
import fengliu.cloudmusic.util.HttpClient;
import fengliu.cloudmusic.util.IdUtil;
import fengliu.cloudmusic.util.MusicPlayer;
import fengliu.cloudmusic.util.TextClickItem;
import fengliu.cloudmusic.util.page.ApiPage;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand All @@ -18,7 +20,7 @@
import java.util.HashMap;
import java.util.Map;

public class DjRadio extends MusicPlayer implements ICanSubscribe, IPrint {
public class DjRadio extends MusicPlayer implements ICanSubscribe, IPrint, ICanComment {
private final HttpClient api;
public final Long id;
public final String name;
Expand All @@ -32,6 +34,7 @@ public class DjRadio extends MusicPlayer implements ICanSubscribe, IPrint {
public final long subCount;
public final long shareCount;
public final String[] description;
public final String threadId;

public DjRadio(HttpClient api, JsonObject data) {
super(new ArrayList<>());
Expand All @@ -48,6 +51,7 @@ public DjRadio(HttpClient api, JsonObject data) {
this.subCount = data.get("subCount").getAsLong();
this.shareCount = data.get("shareCount").getAsLong();
this.description = data.get("desc").getAsString().split("\n");
this.threadId = "A_DJ_1_%s".formatted(this.id);
}

/**
Expand All @@ -73,9 +77,9 @@ public void run() {
while(this.loopPlayIn){
playMusic();

if(this.playIn == this.playList.size() - 1){
if (this.playIn == this.playList.size() - 1) {
this.addDjMusic();
if (this.playIn == this.playList.size() - 1 && !Configs.PLAY.PLAY_LOOP.getBooleanValue()){
if (this.playIn == this.playList.size() - 1 && !Configs.PLAY.PLAY_LOOP.getBooleanValue()) {
this.loopPlayIn = false;
}
}
Expand All @@ -84,6 +88,11 @@ public void run() {
}
}

@Override
public ApiPage getComments(boolean hot) {
return this.comments(this.api, this.id, this.threadId, hot);
}

@Override
public void subscribe() {
Map<String, Object> data = new HashMap<>();
Expand Down Expand Up @@ -140,6 +149,8 @@ public void printToChatHud(FabricClientCommandSource source) {

source.sendFeedback(TextClickItem.combine(
new TextClickItem("play", "/cloudmusic dj play " + this.id),
new TextClickItem("hot.comment", "/cloudmusic dj hotComment " + this.id),
new TextClickItem("comment", "/cloudmusic dj comment " + this.id),
new TextClickItem("subscribe", "/cloudmusic dj subscribe " + this.id),
new TextClickItem("unsubscribe", "/cloudmusic dj unsubscribe " + this.id),
new TextClickItem("shar", Shares.DJ_RADIO.getShar(this.id))
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/fengliu/cloudmusic/music163/data/Music.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Music extends Music163Obj implements IMusic, ICanComment {
public final JsonObject album;
public final long duration;
public final String picUrl;
public final String threadId;
public JsonObject freeTrialInfo = null;

public static String getArtistsName(JsonArray artists) {
Expand Down Expand Up @@ -73,15 +74,17 @@ public Music(HttpClient api, JsonObject data, @Nullable String cover) {

if(data.has("dt")){
this.duration = data.get("dt").getAsLong() / 1000;
}else{
} else {
this.duration = data.get("duration").getAsLong() / 1000;
}

if(this.album.has("picUrl")){
if (this.album.has("picUrl")) {
this.picUrl = this.album.get("picUrl").getAsString();
}else{
} else {
this.picUrl = cover;
}

this.threadId = "R_SO_4_%s".formatted(this.id);
}

/**
Expand Down Expand Up @@ -233,14 +236,9 @@ public long getDuration() {
return this.duration * 1000;
}

@Override
public String getCommentId() {
return "R_SO_4_%s".formatted(this.id);
}

@Override
public ApiPage getComments(boolean hot) {
return this.comments(this.api, this.id, hot);
return this.comments(this.api, this.id, this.threadId, hot);
}

@Override
Expand Down
Loading

0 comments on commit 089d915

Please sign in to comment.