pkg增加根据投票人数排序搜索结果

This commit is contained in:
liyp 2024-05-11 19:38:19 +08:00
parent 46dde0cd18
commit 6d8a3476a8

View file

@ -21,6 +21,8 @@ func (a *Pkg) GetMsg() string {
} }
// 去除换行符 // 去除换行符
raw_msg := strings.TrimRight(a.RawMsg, "\n") raw_msg := strings.TrimRight(a.RawMsg, "\n")
raw_msg = strings.TrimRight(raw_msg, "\r")
fmt.Println("raw_msg:", raw_msg) fmt.Println("raw_msg:", raw_msg)
parms := strings.Split(raw_msg, " ") parms := strings.Split(raw_msg, " ")
@ -32,12 +34,12 @@ func (a *Pkg) GetMsg() string {
fmt.Println("pkg url:", url) fmt.Println("pkg url:", url)
req, err := http.Get(url) req, err := http.Get(url)
if err != nil { if err != nil {
return err.Error() return "服务器网络错误!"
} }
defer req.Body.Close() defer req.Body.Close()
body, err := io.ReadAll(req.Body) body, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
return err.Error() return "数据解析错误!"
} }
// fmt.Println("pkg body:", string(body)) // fmt.Println("pkg body:", string(body))
// var pkg []Package // var pkg []Package
@ -84,8 +86,19 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
// 增加根据投票人数排序
searchResults := searchMap["results"].([]interface{}) searchResults := searchMap["results"].([]interface{})
searchResult := searchResults[0].(map[string]interface{}) maxVotes := 0.0
var searchResult map[string]interface{}
for _, result := range searchResults {
resultMap := result.(map[string]interface{})
numVotes := resultMap["NumVotes"].(float64)
if numVotes > maxVotes {
maxVotes = numVotes
searchResult = resultMap
}
}
maintainer, ok := searchResult["Maintainer"].(string) maintainer, ok := searchResult["Maintainer"].(string)
if !ok { if !ok {
maintainer = "孤儿包" maintainer = "孤儿包"