refactor: 将歌曲哈希查询方法提取到公共工具包中
This commit is contained in:
parent
5dfc935f18
commit
7d5b69685b
@ -9,10 +9,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.lxtend.com/qqbot/sqlite3"
|
"git.lxtend.com/qqbot/sqlite3"
|
||||||
|
"git.lxtend.com/qqbot/util"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -356,7 +356,7 @@ func (bl *blQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er
|
|||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
hashs = append(hashs, record.SongHash)
|
hashs = append(hashs, record.SongHash)
|
||||||
}
|
}
|
||||||
hashToSongId, err := GetSongIdsByHash(hashs)
|
hashToSongId, err := util.GetSongIdsByHash(hashs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -366,52 +366,3 @@ func (bl *blQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er
|
|||||||
|
|
||||||
return records, nil
|
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
|
|
||||||
}
|
|
||||||
|
@ -9,10 +9,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.lxtend.com/qqbot/sqlite3"
|
"git.lxtend.com/qqbot/sqlite3"
|
||||||
|
"git.lxtend.com/qqbot/util"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -385,7 +385,7 @@ func (ss *ssQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er
|
|||||||
for _, record := range records {
|
for _, record := range records {
|
||||||
hashs = append(hashs, record.SongHash)
|
hashs = append(hashs, record.SongHash)
|
||||||
}
|
}
|
||||||
hashToSongId, err := GetSongIdsByHash(hashs)
|
hashToSongId, err := util.GetSongIdsByHash(hashs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -394,52 +394,3 @@ func (ss *ssQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er
|
|||||||
}
|
}
|
||||||
return records, nil
|
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
|
|
||||||
}
|
|
||||||
|
59
util/song_id.go
Normal file
59
util/song_id.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user