2024-09-01 16:36:05 +08:00
|
|
|
|
package workers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
2024-09-08 16:46:05 +08:00
|
|
|
|
"time"
|
2024-09-01 16:36:05 +08:00
|
|
|
|
|
2024-09-08 16:46:05 +08:00
|
|
|
|
"github.com/imroc/req/v3"
|
2024-09-01 16:36:05 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
RegisterWorkerFactory("mner", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
|
|
|
|
|
return &Mner{
|
|
|
|
|
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Mner struct {
|
|
|
|
|
*StdAns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Mner) GetMsg() string {
|
|
|
|
|
if len(a.Parms) < 2 {
|
|
|
|
|
return "请输入AUR的维护者 如:/mner user 查询 user 用户维护的包"
|
|
|
|
|
}
|
|
|
|
|
user := a.Parms[1]
|
2024-09-08 16:46:05 +08:00
|
|
|
|
client := req.C().SetTimeout(time.Second * 10)
|
2024-09-01 16:36:05 +08:00
|
|
|
|
url := "https://aur.archlinux.org/rpc/v5/search/" + user + "?by=maintainer"
|
|
|
|
|
var result map[string]interface{}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
resp, err := client.R().
|
|
|
|
|
SetSuccessResult(&result).
|
|
|
|
|
Get(url)
|
2024-09-01 16:36:05 +08:00
|
|
|
|
if err != nil {
|
2024-09-08 16:46:05 +08:00
|
|
|
|
return "网络错误!"
|
2024-09-01 16:36:05 +08:00
|
|
|
|
}
|
2024-09-08 16:46:05 +08:00
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2024-09-01 16:36:05 +08:00
|
|
|
|
results := result["results"].([]interface{})
|
|
|
|
|
if len(results) == 0 {
|
|
|
|
|
return "未找到该维护者维护的包"
|
|
|
|
|
}
|
|
|
|
|
msg := "该维护者 " + a.Parms[1] + " 共维护了 " + strconv.FormatInt(int64(result["resultcount"].(float64)), 10) + " 个包:\n"
|
|
|
|
|
|
|
|
|
|
// 将结果按NumVotes排序
|
|
|
|
|
sort.Slice(results, func(i, j int) bool {
|
|
|
|
|
return results[i].(map[string]interface{})["NumVotes"].(float64) > results[j].(map[string]interface{})["NumVotes"].(float64)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 只显示NumVotes最大的5个包
|
|
|
|
|
for i, v := range results {
|
|
|
|
|
if i >= 5 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
pkg := v.(map[string]interface{})
|
|
|
|
|
msg += fmt.Sprintf("包名:%s\n版本:%s\n描述:%s\n票数:%d\n\n", pkg["Name"], pkg["Version"], pkg["Description"], int(pkg["NumVotes"].(float64)))
|
|
|
|
|
}
|
|
|
|
|
return msg
|
|
|
|
|
}
|