From e1f9c561550f3b92a5126d390ec72992f126282e Mon Sep 17 00:00:00 2001 From: liyp Date: Mon, 1 Apr 2024 12:33:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0aur=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/pkg.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 32553ea..7b1fe15 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -5,6 +5,8 @@ import ( "fmt" "io" "net/http" + "strings" + "time" ) type Pkg struct { @@ -17,7 +19,7 @@ func (a *Pkg) GetMsg() string { } url := "https://archlinux.org/packages/search/json/?name=" + a.Parms[1] if len(a.Parms) > 2 { - url += "&arch=" + a.Parms[2] + url += "&repo=" + strings.ToUpper(a.Parms[2][:1]) + a.Parms[2][1:] } fmt.Println("pkg url:", url) req, err := http.Get(url) @@ -39,7 +41,58 @@ func (a *Pkg) GetMsg() string { // fmt.Println("pkg:", pkg) resultSlipe := pkg["results"].([]interface{}) if len(resultSlipe) == 0 { - return "没有找到相关软件" + + url := "https://aur.archlinux.org/rpc/v5/suggest/" + a.Parms[1] + req, err := http.Get(url) + if err != nil { + fmt.Println(err) + } + defer req.Body.Close() + + re, err := io.ReadAll(req.Body) + fmt.Println(string(re)) + if err != nil { + fmt.Println(err) + } + var suggestions []string + err = json.Unmarshal(re, &suggestions) + if err != nil { + fmt.Println(err) + } + 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{}) + searchResult := searchResults[0].(map[string]interface{}) + 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 + } result := resultSlipe[0].(map[string]interface{})