From 383cdf1e0239633aa43e83395b131b32b0e57d7f Mon Sep 17 00:00:00 2001 From: liyp Date: Thu, 27 Jun 2024 13:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96pkg=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/pkg.go | 88 +++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 8280eab..2531e54 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -59,7 +59,7 @@ func (a *Pkg) GetMsg() string { } defer req.Body.Close() - fmt.Println("aur url:", url) + fmt.Println("aur suggest url:", url) re, err := io.ReadAll(req.Body) // fmt.Println(string(re)) if err != nil { @@ -70,26 +70,31 @@ func (a *Pkg) GetMsg() string { if err != nil { fmt.Println(err) } - if len(suggestions) > 0 { - searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" - searchReq, err := http.Get(searchUrl) - if err != nil { - fmt.Println(err) - } - defer searchReq.Body.Close() - searchRe, err := io.ReadAll(searchReq.Body) - if err != nil { - fmt.Println(err) - } - var searchMap map[string]interface{} - err = json.Unmarshal(searchRe, &searchMap) - if err != nil { - fmt.Println(err) - } - // 增加根据投票人数排序 - searchResults := searchMap["results"].([]interface{}) - maxVotes := 0.0 - var searchResult map[string]interface{} + // println(len(suggestions)) + if len(suggestions) == 0 { + return "没有找到相关软件" + } + searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" + searchReq, err := http.Get(searchUrl) + if err != nil { + fmt.Println(err) + } + defer searchReq.Body.Close() + searchRe, err := io.ReadAll(searchReq.Body) + if err != nil { + fmt.Println(err) + } + var searchMap map[string]interface{} + err = json.Unmarshal(searchRe, &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) @@ -98,26 +103,29 @@ func (a *Pkg) GetMsg() string { searchResult = resultMap } } - - maintainer, ok := searchResult["Maintainer"].(string) - if !ok { - maintainer = "孤儿包" - } - last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") - var msg string - msg += "仓库:AUR\n" - msg += "包名:" + searchResult["Name"].(string) + "\n" - msg += "版本:" + searchResult["Version"].(string) + "\n" - msg += "描述:" + searchResult["Description"].(string) + "\n" - msg += "维护者:" + maintainer + "\n" - msg += "链接:" + searchResult["URL"].(string) + "\n" - msg += "更新时间:" + last_update + "\n" - - fmt.Println(msg) - return msg - } else { - return "没有找到相关软件" + 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") + + var msg string + msg += "仓库:AUR\n" + msg += "包名:" + searchResult["Name"].(string) + "\n" + msg += "版本:" + searchResult["Version"].(string) + "\n" + msg += "描述:" + searchResult["Description"].(string) + "\n" + msg += "维护者:" + maintainer + "\n" + msg += "链接:" + searchResult["URL"].(string) + "\n" + msg += "更新时间:" + last_update + + fmt.Println(msg) + return msg } result := resultSlipe[0].(map[string]interface{})