From 6d8a3476a871c00b75ffc0e0866b9b9801c2e0e1 Mon Sep 17 00:00:00 2001 From: liyp Date: Sat, 11 May 2024 19:38:19 +0800 Subject: [PATCH] =?UTF-8?q?pkg=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E6=8A=95=E7=A5=A8=E4=BA=BA=E6=95=B0=E6=8E=92=E5=BA=8F=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/pkg.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 437111a..8280eab 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -21,6 +21,8 @@ func (a *Pkg) GetMsg() string { } // 去除换行符 raw_msg := strings.TrimRight(a.RawMsg, "\n") + raw_msg = strings.TrimRight(raw_msg, "\r") + fmt.Println("raw_msg:", raw_msg) parms := strings.Split(raw_msg, " ") @@ -32,12 +34,12 @@ func (a *Pkg) GetMsg() string { fmt.Println("pkg url:", url) req, err := http.Get(url) if err != nil { - return err.Error() + return "服务器网络错误!" } defer req.Body.Close() body, err := io.ReadAll(req.Body) if err != nil { - return err.Error() + return "数据解析错误!" } // fmt.Println("pkg body:", string(body)) // var pkg []Package @@ -84,8 +86,19 @@ func (a *Pkg) GetMsg() string { if err != nil { fmt.Println(err) } + // 增加根据投票人数排序 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) if !ok { maintainer = "孤儿包"