2024-04-01 09:42:29 +08:00
|
|
|
|
package workers
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-01 11:05:57 +08:00
|
|
|
|
"fmt"
|
2024-10-25 23:15:20 +08:00
|
|
|
|
"go-bot/config"
|
|
|
|
|
"slices"
|
2024-04-01 12:33:13 +08:00
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2024-04-21 17:13:20 +08:00
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
"github.com/imroc/req/v3"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-07-05 22:33:55 +08:00
|
|
|
|
func init() {
|
2024-10-25 23:15:20 +08:00
|
|
|
|
plugins := config.GetConfig()["PLUGINS"].([]interface{})
|
|
|
|
|
if slices.Contains(plugins, "pkg") {
|
|
|
|
|
RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
|
|
|
|
return &Pkg{
|
|
|
|
|
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-07-05 22:33:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 09:42:29 +08:00
|
|
|
|
type Pkg struct {
|
|
|
|
|
*StdAns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Pkg) GetMsg() string {
|
2024-04-02 20:29:25 +08:00
|
|
|
|
|
2024-04-01 09:42:29 +08:00
|
|
|
|
if len(a.Parms) < 2 {
|
|
|
|
|
return "请输入包名 如:/pkg linux 查询 linux 相关软件"
|
|
|
|
|
}
|
2024-04-14 15:18:23 +08:00
|
|
|
|
// 去除换行符
|
2024-04-14 15:24:35 +08:00
|
|
|
|
raw_msg := strings.TrimRight(a.RawMsg, "\n")
|
2024-05-11 19:38:19 +08:00
|
|
|
|
raw_msg = strings.TrimRight(raw_msg, "\r")
|
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
// fmt.Println("raw_msg:", raw_msg)
|
2024-04-14 15:18:23 +08:00
|
|
|
|
parms := strings.Split(raw_msg, " ")
|
|
|
|
|
|
|
|
|
|
url := "https://archlinux.org/packages/search/json/?name=" + parms[1]
|
2024-04-23 20:38:37 +08:00
|
|
|
|
if len(parms) > 2 && parms[1] != "" {
|
2024-04-14 15:18:23 +08:00
|
|
|
|
url += "&repo=" + strings.ToUpper(parms[2][:1]) + parms[2][1:]
|
2024-04-01 09:42:29 +08:00
|
|
|
|
}
|
2024-04-02 20:29:25 +08:00
|
|
|
|
// 输出请求地址
|
2024-07-06 21:57:14 +08:00
|
|
|
|
// fmt.Println("pkg url:", url)
|
2024-09-08 16:46:05 +08:00
|
|
|
|
|
|
|
|
|
client := req.C().SetTimeout(time.Second * 10)
|
|
|
|
|
|
|
|
|
|
var pkg map[string]interface{}
|
|
|
|
|
resp, err := client.R().
|
|
|
|
|
SetSuccessResult(&pkg).
|
|
|
|
|
Get(url)
|
|
|
|
|
if err != nil {
|
2024-10-20 20:58:25 +08:00
|
|
|
|
return "网络错误!"
|
2024-04-01 09:42:29 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2024-04-02 20:29:25 +08:00
|
|
|
|
// fmt.Println("pkg body:", string(body))
|
2024-04-01 11:05:57 +08:00
|
|
|
|
// var pkg []Package
|
2024-09-08 16:46:05 +08:00
|
|
|
|
|
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 {
|
2024-04-01 12:33:13 +08:00
|
|
|
|
|
2024-04-14 15:18:23 +08:00
|
|
|
|
url := "https://aur.archlinux.org/rpc/v5/suggest/" + parms[1]
|
2024-04-01 12:33:13 +08:00
|
|
|
|
var suggestions []string
|
2024-09-08 16:46:05 +08:00
|
|
|
|
resp, err := client.R().
|
|
|
|
|
SetSuccessResult(&suggestions).Get(url)
|
2024-04-01 12:33:13 +08:00
|
|
|
|
if err != nil {
|
2024-09-08 16:46:05 +08:00
|
|
|
|
return "服务器网络错误!"
|
2024-04-01 12:33:13 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2024-06-27 13:16:14 +08:00
|
|
|
|
// println(len(suggestions))
|
|
|
|
|
if len(suggestions) == 0 {
|
|
|
|
|
return "没有找到相关软件"
|
|
|
|
|
}
|
|
|
|
|
var searchMap map[string]interface{}
|
|
|
|
|
var searchResult map[string]interface{}
|
2024-09-01 20:37:45 +08:00
|
|
|
|
if len(suggestions) == 1 || suggestions[0] == parms[1] {
|
2024-07-13 16:10:34 +08:00
|
|
|
|
searchUrl := "https://aur.archlinux.org/rpc/v5/info/" + suggestions[0]
|
2024-09-08 16:46:05 +08:00
|
|
|
|
resp, err := client.R().SetSuccessResult(&searchMap).Get(searchUrl)
|
|
|
|
|
if err != nil {
|
2024-07-13 16:10:34 +08:00
|
|
|
|
return "服务器网络错误!"
|
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
defer resp.Body.Close()
|
2024-07-13 16:10:34 +08:00
|
|
|
|
|
|
|
|
|
searchResult = searchMap["results"].([]interface{})[0].(map[string]interface{})
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
maxVotes := 0.0
|
2024-07-13 18:00:03 +08:00
|
|
|
|
var searchResults []map[string]interface{}
|
|
|
|
|
for _, suggestion := range suggestions {
|
|
|
|
|
searchUrl := "https://aur.archlinux.org/rpc/v5/info/" + suggestion
|
2024-09-08 16:46:05 +08:00
|
|
|
|
resp, err := client.R().SetSuccessResult(&searchMap).Get(searchUrl)
|
2024-07-13 18:00:03 +08:00
|
|
|
|
if err != nil {
|
2024-09-08 16:46:05 +08:00
|
|
|
|
return "服务器网络错误!"
|
2024-07-13 18:00:03 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
defer resp.Body.Close()
|
2024-07-13 18:00:03 +08:00
|
|
|
|
results := searchMap["results"].([]interface{})
|
|
|
|
|
|
|
|
|
|
searchResults = append(searchResults, results[0].(map[string]interface{}))
|
|
|
|
|
// searchResults = append(searchResults, searchMap["results"].([]interface{})[0].(map[string]interface{}))
|
|
|
|
|
// println("searchResults:", len(searchResults))
|
|
|
|
|
}
|
2024-07-13 16:10:34 +08:00
|
|
|
|
|
|
|
|
|
// 增加根据投票人数排序
|
2024-07-13 18:00:03 +08:00
|
|
|
|
for _, result := range searchResults {
|
|
|
|
|
// resultMap := result.(map[string]interface{})
|
|
|
|
|
numVotes := result["NumVotes"].(float64)
|
|
|
|
|
if numVotes > maxVotes {
|
|
|
|
|
maxVotes = numVotes
|
|
|
|
|
searchResult = result
|
2024-05-11 19:38:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-13 18:00:03 +08:00
|
|
|
|
|
2024-06-27 13:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
maintainer, ok := searchResult["Maintainer"].(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
maintainer = "孤儿包"
|
|
|
|
|
}
|
2024-07-13 16:10:34 +08:00
|
|
|
|
OutOfDate := ""
|
|
|
|
|
_, ok = searchResult["OutOfDate"].(float64)
|
|
|
|
|
if ok {
|
|
|
|
|
OutOfDate = fmt.Sprintf("过期时间:%s\n", time.Unix(int64(searchResult["OutOfDate"].(float64)), 0).Format("2006-01-02 15:04:05"))
|
|
|
|
|
}
|
2024-06-27 13:16:14 +08:00
|
|
|
|
var msg string
|
|
|
|
|
msg += "仓库:AUR\n"
|
|
|
|
|
msg += "包名:" + searchResult["Name"].(string) + "\n"
|
|
|
|
|
msg += "版本:" + searchResult["Version"].(string) + "\n"
|
|
|
|
|
msg += "描述:" + searchResult["Description"].(string) + "\n"
|
2024-09-01 20:54:37 +08:00
|
|
|
|
msg += "维护者:" + maintainer
|
2024-07-06 19:18:59 +08:00
|
|
|
|
upstream, ok := searchResult["URL"].(string)
|
|
|
|
|
if !ok || upstream == "" {
|
|
|
|
|
upstream = "无"
|
|
|
|
|
}
|
2024-09-01 20:54:37 +08:00
|
|
|
|
coMaintainers, ok := searchResult["CoMaintainers"].([]interface{})
|
|
|
|
|
if ok {
|
|
|
|
|
msg += " ( "
|
|
|
|
|
for _, coMaintainer := range coMaintainers {
|
|
|
|
|
msg += coMaintainer.(string) + " "
|
|
|
|
|
}
|
|
|
|
|
msg += ")"
|
|
|
|
|
}
|
|
|
|
|
msg += "\n"
|
2024-07-06 19:18:59 +08:00
|
|
|
|
msg += "上游:" + upstream + "\n"
|
2024-07-13 16:10:34 +08:00
|
|
|
|
msg += OutOfDate
|
|
|
|
|
last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05")
|
2024-09-01 20:54:37 +08:00
|
|
|
|
msg += "更新时间:" + last_update + "\n"
|
2024-09-01 20:37:45 +08:00
|
|
|
|
msg += "投票:" + fmt.Sprintf("%.0f", searchResult["NumVotes"].(float64)) + "\n"
|
|
|
|
|
msg += "AUR 链接:https://aur.archlinux.org/packages/" + searchResult["Name"].(string) + "\n"
|
2024-04-01 12:33:13 +08:00
|
|
|
|
|
2024-07-06 21:57:14 +08:00
|
|
|
|
// fmt.Println(msg)
|
2024-06-27 13:16:14 +08:00
|
|
|
|
return msg
|
2024-04-01 12:33:13 +08:00
|
|
|
|
|
2024-04-01 11:37:03 +08:00
|
|
|
|
}
|
|
|
|
|
result := resultSlipe[0].(map[string]interface{})
|
2024-07-13 18:00:03 +08:00
|
|
|
|
|
2024-04-02 20:29:25 +08:00
|
|
|
|
var msg string
|
2024-04-18 21:13:15 +08:00
|
|
|
|
last_update := result["last_update"].(string)
|
|
|
|
|
last_update = strings.Replace(last_update, "T", " ", 1)
|
|
|
|
|
last_update = strings.Replace(last_update, "Z", "", 1)
|
2024-04-02 20:29:25 +08:00
|
|
|
|
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"
|
2024-07-06 19:18:59 +08:00
|
|
|
|
msg += "上游:" + result["url"].(string) + "\n"
|
2024-04-18 21:13:15 +08:00
|
|
|
|
msg += "更新日期:" + last_update
|
2024-04-02 20:29:25 +08:00
|
|
|
|
return msg
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
|
|
}
|