fix: 修复单个哈希查询时的歌曲ID获取逻辑

This commit is contained in:
lixiangwuxian 2025-03-08 21:15:18 +08:00
parent e645ed5366
commit b22185e87f
2 changed files with 23 additions and 3 deletions

View File

@ -381,11 +381,15 @@ func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error
}
batchHashs := hashs[i:end]
queryUrl := "https://api.beatsaver.com/maps/hash/" + strings.Join(batchHashs, ",")
log.Default().Printf("获取歌曲IDurl:%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
@ -393,7 +397,15 @@ func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error
var response map[string]struct {
ID string `json:"id"`
}
err = json.Unmarshal(body, &response)
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
}

View File

@ -389,7 +389,7 @@ func (ss *ssQuery) GetRecentScores(count int, qqId string) ([]RecordDataLite, er
if err != nil {
return nil, err
}
for i := 0; i < len(records); i++ {
for i := range records {
records[i].SongId = hashToSongId[records[i].SongHash]
}
return records, nil
@ -425,7 +425,15 @@ func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error
var response map[string]struct {
ID string `json:"id"`
}
err = json.Unmarshal(body, &response)
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
}