This commit is contained in:
liyp 2024-04-01 11:05:57 +08:00
parent 19c4673d98
commit 822847adcb

View file

@ -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 ""
}