From b91b18f29c602f690677e75ba3c129b1614d2609 Mon Sep 17 00:00:00 2001 From: liyp Date: Sat, 13 Jul 2024 16:10:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E8=BD=AF=E4=BB=B6?= =?UTF-8?q?=E5=8C=85=E6=90=9C=E7=B4=A2=E9=80=BB=E8=BE=91=E4=BB=A5=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9B=B4=E5=A4=9A=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/pkg.go | 80 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 92d18a8..2a7ab38 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -73,43 +73,63 @@ func (a *Pkg) GetMsg() string { if len(suggestions) == 0 { return "没有找到相关软件" } - searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" - _, body, errs = request.Get(searchUrl).End() - if len(errs) > 0 { - fmt.Println("searchUrl err:", errs) - } var searchMap map[string]interface{} - err = json.Unmarshal([]byte(body), &searchMap) - if err != nil { - fmt.Println(err) - } - - searchResults := searchMap["results"].([]interface{}) - // println("searchResults:", len(searchResults)) - maxVotes := 0.0 var searchResult map[string]interface{} - // 增加根据投票人数排序 - if len(searchResults) > 1 { - for _, result := range searchResults { - resultMap := result.(map[string]interface{}) - numVotes := resultMap["NumVotes"].(float64) - if numVotes > maxVotes { - maxVotes = numVotes - searchResult = resultMap - } + if len(suggestions) == 1 { + searchUrl := "https://aur.archlinux.org/rpc/v5/info/" + suggestions[0] + _, body, errs = request.Get(searchUrl).End() + + if len(errs) > 0 { + fmt.Println("searchUrl err:", errs) + + return "服务器网络错误!" + } + + err = json.Unmarshal([]byte(body), &searchMap) + + if err != nil { + fmt.Println("解析失败:", err) + } + searchResult = searchMap["results"].([]interface{})[0].(map[string]interface{}) + + } else { + searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" + _, body, errs = request.Get(searchUrl).End() + if len(errs) > 0 { + fmt.Println("searchUrl err:", errs) + } + + err = json.Unmarshal([]byte(body), &searchMap) + if err != nil { + fmt.Println(err) + } + + searchResults := searchMap["results"].([]interface{}) + // println("searchResults:", len(searchResults)) + maxVotes := 0.0 + + // 增加根据投票人数排序 + if len(searchResults) > 1 { + for _, result := range searchResults { + resultMap := result.(map[string]interface{}) + numVotes := resultMap["NumVotes"].(float64) + if numVotes > maxVotes { + maxVotes = numVotes + searchResult = resultMap + } + } + // fmt.Println(len(searchResult)) } - // fmt.Println(len(searchResult)) - } else if len(searchResults) == 1 { - searchResult = searchResults[0].(map[string]interface{}) } maintainer, ok := searchResult["Maintainer"].(string) if !ok { maintainer = "孤儿包" } - // println("maintainer:", maintainer) - // last_update := '' - last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") - + OutOfDate := "" + _, ok = searchResult["OutOfDate"].(float64) + if ok { + OutOfDate = fmt.Sprintf("过期时间:%s\n", time.Unix(int64(searchResult["OutOfDate"].(float64)), 0).Format("2006-01-02 15:04:05")) + } var msg string msg += "仓库:AUR\n" msg += "包名:" + searchResult["Name"].(string) + "\n" @@ -121,6 +141,8 @@ func (a *Pkg) GetMsg() string { upstream = "无" } msg += "上游:" + upstream + "\n" + msg += OutOfDate + last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") msg += "更新时间:" + last_update // fmt.Println(msg)