diff --git a/plugin/guessmusic/apiservice.go b/plugin/guessmusic/apiservice.go index 3d9b7fa6396..c473fb7af06 100644 --- a/plugin/guessmusic/apiservice.go +++ b/plugin/guessmusic/apiservice.go @@ -3,11 +3,14 @@ package guessmusic import ( "encoding/json" "math/rand" + "net/http" "net/url" "os" "strconv" "strings" "time" + + "github.com/FloatTech/floatbox/process" wyy "github.com/FloatTech/AnimeAPI/neteasemusic" "github.com/FloatTech/floatbox/file" @@ -303,6 +306,46 @@ func init() { }) } +// DownloadMusic 下载网易云音乐(歌曲ID,歌曲名称,下载路径) +func DownloadMusic(musicID int, musicName, pathOfMusic string) error { + + APIURL := cfg.APIURL + "song/url?id=" + strconv.Itoa(musicID) + data, err := web.GetData(APIURL) + if err != nil { + return err + } + var parsed musicDownloadURL + err = json.Unmarshal(data, &parsed) + if err != nil { + return err + } + + if parsed.Code != 200 { + err = errors.Errorf("requset code : %d", parsed.Code) + return err + } + + musicURL := parsed.Data.URL + downMusic := pathOfMusic + "/" + musicName + ".mp3" + + if file.IsNotExist(downMusic) { + // 检查歌曲是否存在 + response, err := http.Head(musicURL) + if err != nil { + return err + } + _ = response.Body.Close() + if response.StatusCode != 200 { + return errors.Errorf("Status Code: %d", response.StatusCode) + } + // 下载歌曲 + err = file.DownloadTo(musicURL, downMusic) + process.SleepAbout1sTo2s() + return err + } + return nil +} + // 随机从歌单下载歌曲(歌单ID, 音乐保存路径) func drawByAPI(playlistID int64, musicPath string) (musicName string, err error) { APIURL := cfg.APIURL + "playlist/track/all?id=" + strconv.FormatInt(playlistID, 10) + "&limit=1000" @@ -346,7 +389,7 @@ func drawByAPI(playlistID int64, musicPath string) (musicName string, err error) name += " - " + artistName } // 下载歌曲 - err = wyy.DownloadMusic(musicID, name, musicPath) + err = DownloadMusic(musicID, name, musicPath) if err == nil { musicName = name + ".mp3" if cfg.Local { @@ -401,7 +444,7 @@ func downloadlist(playlistID int64, musicPath string) error { musicName += " - " + artistName } // 下载歌曲 - err = wyy.DownloadMusic(musicID, musicName, musicPath) + err = DownloadMusic(musicID, musicName, musicPath) if err == nil { if cfg.Local { // 下载歌词 @@ -440,7 +483,7 @@ func downloadByOvooa(playlistID int64, musicPath string) (musicName string, err } name := musicList[0] // 下载歌曲 - err = wyy.DownloadMusic(mid, name, musicPath) + err = DownloadMusic(mid, name, musicPath) if err == nil { musicName = name + ".mp3" if cfg.Local { diff --git a/plugin/guessmusic/guessmusic.go b/plugin/guessmusic/guessmusic.go index 4b19175ab50..daa1b74e9a9 100644 --- a/plugin/guessmusic/guessmusic.go +++ b/plugin/guessmusic/guessmusic.go @@ -145,7 +145,8 @@ func init() { tick.Stop() after.Stop() ctx.SendChain(message.Reply(c.Event.MessageID), messageStr) - ctx.SendChain(message.Record("file:///" + pathOfMusic + musicName)) + //歌曲结束后不发送完整歌曲 + //ctx.SendChain(message.Record("file:///" + pathOfMusic + musicName)) } else { wait.Reset(40 * time.Second) tick.Reset(105 * time.Second) @@ -286,7 +287,7 @@ func gameMatch(c *zero.Ctx, beginner int64, musicInfo []string, answerTimes, tic switch { case answer == "取消": if c.Event.UserID == beginner { - return message.Text("游戏已取消,猜歌答案是\n", musicInfo[len(musicInfo)-1], "\n\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true + return message.Text("游戏已取消,猜歌答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true } return message.Text("你无权限取消"), answerTimes, tickTimes, false case answer == "提示": @@ -296,11 +297,11 @@ func gameMatch(c *zero.Ctx, beginner int64, musicInfo []string, answerTimes, tic } return message.Text("再听这段音频,要仔细听哦"), answerTimes, tickTimes, false case strings.Contains(musicInfo[0], answer) || strings.EqualFold(musicInfo[0], answer): - return message.Text("太棒了,你猜对歌曲名了!答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true + return message.Text("太棒了,你猜对歌曲名了!答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true case strings.Contains(musicInfo[1], answer) || strings.EqualFold(musicInfo[1], answer): - return message.Text("太棒了,你猜对歌手名了!答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true + return message.Text("太棒了,你猜对歌手名了!答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true case len(musicInfo) == 4 && (strings.Contains(musicInfo[2], answer) || strings.EqualFold(musicInfo[2], answer)): - return message.Text("太棒了,你猜对相关信息了!答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true + return message.Text("太棒了,你猜对相关信息了!答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true default: answerTimes++ tickTimes++ @@ -308,7 +309,7 @@ func gameMatch(c *zero.Ctx, beginner int64, musicInfo []string, answerTimes, tic case tickTimes > 2 && answerTimes < 6: return message.Text("答案不对哦,还有", 6-answerTimes, "次答题,加油啊~"), answerTimes, tickTimes, false case tickTimes > 2: - return message.Text("次数到了,没能猜出来。答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true + return message.Text("次数到了,没能猜出来。答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true default: return message.Text("答案不对,再听这段音频,要仔细听哦"), answerTimes, tickTimes, false } diff --git a/plugin/guessmusic/struct.go b/plugin/guessmusic/struct.go index cd4532ca3f5..8315b81577d 100644 --- a/plugin/guessmusic/struct.go +++ b/plugin/guessmusic/struct.go @@ -54,6 +54,49 @@ type qrInfo struct { } `json:"data"` } +type musicDownloadURL struct { + Data struct { + ID int `json:"id"` + URL string `json:"url"` + Br int `json:"br"` + Size int `json:"size"` + Md5 interface{} `json:"md5"` + Code int `json:"code"` + Expi int `json:"expi"` + Type interface{} `json:"type"` + Gain int `json:"gain"` + Peak interface{} `json:"peak"` + Fee int `json:"fee"` + Uf interface{} `json:"uf"` + Payed int `json:"payed"` + Flag int `json:"flag"` + CanExtend bool `json:"canExtend"` + FreeTrialInfo interface{} `json:"freeTrialInfo"` + Level interface{} `json:"level"` + EncodeType interface{} `json:"encodeType"` + ChannelLayout interface{} `json:"channelLayout"` + FreeTrialPrivilege struct { + ResConsumable bool `json:"resConsumable"` + UserConsumable bool `json:"userConsumable"` + ListenType interface{} `json:"listenType"` + CannotListenReason int `json:"cannotListenReason"` + PlayReason interface{} `json:"playReason"` + } `json:"freeTrialPrivilege"` + FreeTimeTrialPrivilege struct { + ResConsumable bool `json:"resConsumable"` + UserConsumable bool `json:"userConsumable"` + Type int `json:"type"` + RemainTime int `json:"remainTime"` + } `json:"freeTimeTrialPrivilege"` + URLSource int `json:"urlSource"` + RightSource int `json:"rightSource"` + PodcastCtrp interface{} `json:"podcastCtrp"` + EffectTypes interface{} `json:"effectTypes"` + Time int `json:"time"` + } `json:"data"` + Code int `json:"code"` +} + // 获取歌单信息 type listInfoOfAPI struct { Code int `json:"code"`