go-bot/workers/pkg.go
2024-04-03 22:02:08 +08:00

119 lines
3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package workers
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
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[1]
if len(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)
if err != nil {
return err.Error()
}
defer req.Body.Close()
body, err := io.ReadAll(req.Body)
if err != nil {
return err.Error()
}
// 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()
}
// fmt.Println("pkg:", pkg)
resultSlipe := pkg["results"].([]interface{})
if len(resultSlipe) == 0 {
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)
}
if len(suggestions) > 0 {
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
} else {
return "没有找到相关软件"
}
}
result := resultSlipe[0].(map[string]interface{})
// 输出结果字符串
// fmt.Println("result:", result)
// if result != nil {
var msg string
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)
return msg
}