diff --git a/service/beatleader/bind_bl.go b/service/beatleader/bind_bl.go index 919a486..0f5d9fa 100644 --- a/service/beatleader/bind_bl.go +++ b/service/beatleader/bind_bl.go @@ -9,10 +9,10 @@ import ( "log" "net/http" "strconv" - "strings" "time" "git.lxtend.com/qqbot/sqlite3" + "git.lxtend.com/qqbot/util" _ "github.com/mattn/go-sqlite3" ) @@ -356,7 +356,7 @@ func (bl *blQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er for _, record := range records { hashs = append(hashs, record.SongHash) } - hashToSongId, err := GetSongIdsByHash(hashs) + hashToSongId, err := util.GetSongIdsByHash(hashs) if err != nil { return nil, err } @@ -366,52 +366,3 @@ func (bl *blQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er return records, nil } - -func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error) { - if len(hashs) == 0 { - return nil, nil - } - //每批最多49个 - hashToSongId = make(map[string]string) - 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, ",") - log.Default().Printf("获取歌曲ID,url:%s", queryUrl) - resp, err := http.Get(queryUrl) - if err != nil { - return nil, err - } - defer resp.Body.Close() - if resp.StatusCode != 200 { - return nil, fmt.Errorf("获取歌曲ID失败,状态码:%d,url:%s", resp.StatusCode, queryUrl) - } - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - var response = make(map[string]struct { - ID string `json:"id"` - }) - if len(batchHashs) == 1 { - var singleResponse struct { - ID string `json:"id"` - } - err = json.Unmarshal(body, &singleResponse) - response[batchHashs[0]] = singleResponse - } else { - err = json.Unmarshal(body, &response) - } - if err != nil { - return nil, err - } - for hash, data := range response { - hashToSongId[hash] = data.ID - } - } - return hashToSongId, nil -} diff --git a/service/scoresaber/bind_ss.go b/service/scoresaber/bind_ss.go index 74b3e88..1a2c135 100644 --- a/service/scoresaber/bind_ss.go +++ b/service/scoresaber/bind_ss.go @@ -9,10 +9,10 @@ import ( "log" "net/http" "strconv" - "strings" "time" "git.lxtend.com/qqbot/sqlite3" + "git.lxtend.com/qqbot/util" _ "github.com/mattn/go-sqlite3" ) @@ -385,7 +385,7 @@ func (ss *ssQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er for _, record := range records { hashs = append(hashs, record.SongHash) } - hashToSongId, err := GetSongIdsByHash(hashs) + hashToSongId, err := util.GetSongIdsByHash(hashs) if err != nil { return nil, err } @@ -394,52 +394,3 @@ func (ss *ssQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er } return records, nil } - -func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error) { - if len(hashs) == 0 { - return nil, nil - } - //每批最多49个 - hashToSongId = make(map[string]string) - 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, ",") - log.Default().Printf("获取歌曲ID,url:%s", queryUrl) - resp, err := http.Get(queryUrl) - if err != nil { - return nil, err - } - defer resp.Body.Close() - if resp.StatusCode != 200 { - return nil, fmt.Errorf("获取歌曲ID失败,状态码:%d,url:%s", resp.StatusCode, queryUrl) - } - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - var response = make(map[string]struct { - ID string `json:"id"` - }) - if len(batchHashs) == 1 { - var singleResponse struct { - ID string `json:"id"` - } - err = json.Unmarshal(body, &singleResponse) - response[batchHashs[0]] = singleResponse - } else { - err = json.Unmarshal(body, &response) - } - if err != nil { - return nil, err - } - for hash, data := range response { - hashToSongId[hash] = data.ID - } - } - return hashToSongId, nil -} diff --git a/util/song_id.go b/util/song_id.go new file mode 100644 index 0000000..eb3a24f --- /dev/null +++ b/util/song_id.go @@ -0,0 +1,59 @@ +package util + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "strings" +) + +func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error) { + if len(hashs) == 0 { + return nil, nil + } + //每批最多49个 + hashToSongId = make(map[string]string) + 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, ",") + log.Default().Printf("获取歌曲ID,url:%s", queryUrl) + resp, err := http.Get(queryUrl) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return nil, fmt.Errorf("获取歌曲ID失败,状态码:%d,url:%s", resp.StatusCode, queryUrl) + } + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response = make(map[string]struct { + ID string `json:"id"` + }) + if len(batchHashs) == 1 { + var singleResponse struct { + ID string `json:"id"` + } + err = json.Unmarshal(body, &singleResponse) + response[batchHashs[0]] = singleResponse + } else { + err = json.Unmarshal(body, &response) + } + if err != nil { + return nil, err + } + for hash, data := range response { + hashToSongId[hash] = data.ID + } + } + return hashToSongId, nil +}