go-bot/workers/pkg.go

65 lines
1.6 KiB
Go
Raw Normal View History

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