go-bot/workers/pkg.go

86 lines
2.2 KiB
Go
Raw Normal View History

2024-04-01 09:42:29 +08:00
package workers
import (
"encoding/json"
"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
}
func (a *Pkg) GetMsg() string {
if len(a.Parms) < 2 {
return "请输入包名 如:/pkg linux 查询 linux 相关软件"
}
url := "https://archlinux.org/packages/search/json/?name=" + a.Parms[0]
if len(a.Parms) > 1 {
url += "&arch=" + a.Parms[1]
}
req, err := http.Get(url)
if err != nil {
return err.Error()
}
defer req.Body.Close()
body, err := io.ReadAll(req.Body)
if err != nil {
return err.Error()
}
var pkg []Package
// var data map[string]interface{}
err = json.Unmarshal(body, &pkg)
if err != nil {
return err.Error()
}
if pkg[0].Results != 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"
msg += "----------"
}
} else if pkg[0].Results == nil {
// req, _ := http.Get("https://aur.tuna.tsinghua.edu.cn/rpc/?v=5&type=info&arg=" + a.parms[1])
return "没有找到相关软件"
}
return ""
}