feat: 优化最近成绩获取,并发下载歌曲封面并在消息中展示
This commit is contained in:
parent
861ee37da3
commit
fcc99efe7e
@ -2,13 +2,17 @@ package scoresaber
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"git.lxtend.com/qqbot/constants"
|
"git.lxtend.com/qqbot/constants"
|
||||||
"git.lxtend.com/qqbot/handler"
|
"git.lxtend.com/qqbot/handler"
|
||||||
|
"git.lxtend.com/qqbot/message"
|
||||||
"git.lxtend.com/qqbot/model"
|
"git.lxtend.com/qqbot/model"
|
||||||
"git.lxtend.com/qqbot/service/scoresaber"
|
"git.lxtend.com/qqbot/service/scoresaber"
|
||||||
|
"git.lxtend.com/qqbot/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -153,13 +157,40 @@ func getMyRecentScore(msg model.Message) (reply model.Reply) {
|
|||||||
FromMsg: msg,
|
FromMsg: msg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, v := range records {
|
coverImageMap := make(map[string]string)
|
||||||
scoreMsg += v.ToString() + "\n\n"
|
wg := sync.WaitGroup{}
|
||||||
userName = v.Name
|
for _, record := range records {
|
||||||
|
coverImageMap[record.SongHash] = record.CoverImage
|
||||||
|
wg.Add(1)
|
||||||
|
go func(songHash string) {
|
||||||
|
defer wg.Done()
|
||||||
|
//文件存在则跳过
|
||||||
|
if _, err := os.Stat(util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(songHash + ".jpeg"))); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
util.DownloadFile(coverImageMap[songHash], util.GenTempFilePath(songHash+".jpeg"))
|
||||||
|
newPath, err := util.ResizeImageByMaxHeight(util.GenTempFilePath(songHash+".jpeg"), 20)
|
||||||
|
os.Remove(util.GenTempFilePath(songHash + ".jpeg"))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("缩放图片失败: %v", err)
|
||||||
|
}
|
||||||
|
coverImageMap[songHash] = newPath
|
||||||
|
}(record.SongHash)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
for _, record := range records {
|
||||||
|
imageMsg := message.ImageMessage{
|
||||||
|
Type: "image",
|
||||||
|
Data: message.ImageMessageData{
|
||||||
|
File: util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(record.SongHash + ".jpeg")),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
scoreMsg += imageMsg.ToCQString() + record.ToString() + "\n"
|
||||||
|
userName = record.Name
|
||||||
recordCount++
|
recordCount++
|
||||||
}
|
}
|
||||||
if len(scoreMsg) > 0 {
|
if len(scoreMsg) > 0 {
|
||||||
scoreMsg = scoreMsg[:len(scoreMsg)-len("\n\n")] //去掉最后一个换行符
|
scoreMsg = scoreMsg[:len(scoreMsg)-len("\n")] //去掉最后一个换行符
|
||||||
} else {
|
} else {
|
||||||
return model.Reply{
|
return model.Reply{
|
||||||
ReplyMsg: "无最近游戏记录",
|
ReplyMsg: "无最近游戏记录",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user