From 822847adcbe4497e6cdc44fff41971a62d55884c Mon Sep 17 00:00:00 2001 From: liyp Date: Mon, 1 Apr 2024 11:05:57 +0800 Subject: [PATCH] fix pkg --- workers/pkg.go | 67 ++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 43466a0..6477dc5 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -2,40 +2,11 @@ package workers import ( "encoding/json" + "fmt" "io" "net/http" - "time" ) -type Package struct { - Version int `json:"version"` - Limit int `json:"limit"` - Valid bool `json:"valid"` - Results []Result `json:"results"` -} - -type Result struct { - Pkgname string `json:"pkgname"` - Pkgbase string `json:"pkgbase"` - Repo string `json:"repo"` - Arch string `json:"arch"` - Pkgver string `json:"pkgver"` - Pkgrel string `json:"pkgrel"` - Epoch int `json:"epoch"` - Pkgdesc string `json:"pkgdesc"` - Url string `json:"url"` - Filename string `json:"filename"` - CompressedSize int `json:"compressed_size"` - InstalledSize int `json:"installed_size"` - BuildDate time.Time `json:"build_date"` - LastUpdate time.Time `json:"last_update"` - FlagDate time.Time `json:"flag_date"` - Maintainers []string `json:"maintainers"` - Packager string `json:"packager"` - Groups []string `json:"groups"` - Licenses []string `json:"licenses"` -} - type Pkg struct { *StdAns } @@ -45,9 +16,10 @@ func (a *Pkg) GetMsg() string { return "请输入包名 如:/pkg linux 查询 linux 相关软件" } url := "https://archlinux.org/packages/search/json/?name=" + a.Parms[0] - if len(a.Parms) > 1 { + if len(a.Parms) > 2 { url += "&arch=" + a.Parms[1] } + // fmt.Println("pkg url:", url) req, err := http.Get(url) if err != nil { return err.Error() @@ -57,29 +29,32 @@ func (a *Pkg) GetMsg() string { if err != nil { return err.Error() } - var pkg []Package - // var data map[string]interface{} + fmt.Println("pkg body:", string(body)) + // var pkg []Package + var pkg map[string]interface{} err = json.Unmarshal(body, &pkg) if err != nil { return err.Error() } - if pkg[0].Results != nil { + // fmt.Println("pkg:", pkg) + result := pkg["results"].([]interface{})[0].(map[string]interface{}) + + fmt.Println("result:", result) + if result != nil { var msg string - for _, v := range pkg[0].Results { - msg += "仓库:" + v.Repo + "\n" - msg += "包名:" + v.Pkgname + "\n" - msg += "版本:" + v.Pkgver + "\n" - msg += "描述:" + v.Pkgdesc + "\n" - msg += "打包:" + v.Packager + "\n" - msg += "链接:" + v.Url + "\n" - msg += "更新日期:" + v.LastUpdate.String() + "\n" + // for _, v := range result[0] { + msg += "仓库:" + result["repo"].(string) + "\n" + msg += "包名:" + result["pkgname"].(string) + "\n" + msg += "版本:" + result["pkgver"].(string) + "\n" + msg += "描述:" + result["pkgdesc"].(string) + "\n" + msg += "打包:" + result["packager"].(string) + "\n" + msg += "链接:" + result["url"].(string) + "\n" + msg += "更新日期:" + result["last_update"].(string) + "\n" - msg += "----------" - } + return msg - } else if pkg[0].Results == nil { + } else { // req, _ := http.Get("https://aur.tuna.tsinghua.edu.cn/rpc/?v=5&type=info&arg=" + a.parms[1]) return "没有找到相关软件" } - return "" }