feat: 为 BeatLeader 和 ScoreSaber 添加通用歌曲 ID 获取方法
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.lxtend.com/qqbot/sqlite3"
|
||||
@@ -286,38 +287,11 @@ func (bl *blQuery) SaveRecord(scoreData ScoreData) {
|
||||
}
|
||||
|
||||
func (bl *blQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, error) {
|
||||
db := sqlite3.GetDB() // 假设 sqlite3.GetDB() 返回 *sqlx.DB
|
||||
tx, err := db.Beginx()
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil, errors.New("数据库连接失败,请稍后重试")
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// 查询绑定的 blId
|
||||
blId, err := getBLID(qqId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// // 查询记录
|
||||
// var records []RecordDataLite
|
||||
// err = tx.Select(&records, "SELECT * FROM blRecordData WHERE bl_id = ? ORDER BY generated_time DESC LIMIT ?", blId, count)
|
||||
// if err != nil {
|
||||
// if err == sql.ErrNoRows {
|
||||
// return nil, errors.New("未查询到数据")
|
||||
// }
|
||||
// log.Println("查询数据出错:", err)
|
||||
// return nil, errors.New("查询记录失败")
|
||||
// }
|
||||
|
||||
// // 提交事务
|
||||
// err = tx.Commit()
|
||||
// if err != nil {
|
||||
// log.Print(err)
|
||||
// return nil, errors.New("提交事务失败")
|
||||
// }
|
||||
|
||||
// return records, nil
|
||||
playerData, err := FetchPlayerData(blId)
|
||||
if err != nil {
|
||||
@@ -372,15 +346,56 @@ func (bl *blQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er
|
||||
DeviceControllerRight: GetControllerStr(score.Controller),
|
||||
GeneratedTime: time.Unix(score.Timepost, 0).Format("2006-01-02 15:04:05.999999999-07:00"),
|
||||
}
|
||||
if score.Leaderboard.Song.ID != "" && len(score.Leaderboard.Song.ID) > 5 {
|
||||
dataLite.SongId = score.Leaderboard.Song.ID[0:5]
|
||||
} else if score.Leaderboard.Song.ID != "" {
|
||||
dataLite.SongId = score.Leaderboard.Song.ID
|
||||
}
|
||||
if score.Leaderboard.Difficulty.Stars != nil {
|
||||
dataLite.Stars = *score.Leaderboard.Difficulty.Stars
|
||||
}
|
||||
records = append(records, dataLite)
|
||||
}
|
||||
// 获取歌曲ID
|
||||
hashs := make([]string, 0)
|
||||
for _, record := range records {
|
||||
hashs = append(hashs, record.SongHash)
|
||||
}
|
||||
hashToSongId, err := GetSongIdsByHash(hashs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < len(records); i++ {
|
||||
records[i].SongId = hashToSongId[records[i].SongHash]
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error) {
|
||||
//每批最多49个
|
||||
batchSize := 49
|
||||
for i := 0; i < len(hashs); i += batchSize {
|
||||
end := i + batchSize
|
||||
if end > len(hashs) {
|
||||
end = len(hashs)
|
||||
}
|
||||
batchHashs := hashs[i:end]
|
||||
queryUrl := "https://api.beatsaver.com/maps/hash/" + strings.Join(batchHashs, ",")
|
||||
resp, err := http.Get(queryUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var response map[string]struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for hash, data := range response {
|
||||
hashToSongId[hash] = data.ID
|
||||
}
|
||||
}
|
||||
return hashToSongId, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user