60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package workers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"sort"
|
||
"strconv"
|
||
|
||
"github.com/parnurzeal/gorequest"
|
||
)
|
||
|
||
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]
|
||
url := "https://aur.archlinux.org/rpc/v5/search/" + user + "?by=maintainer"
|
||
request := gorequest.New()
|
||
_, body, errs := request.Get(url).End()
|
||
if len(errs) > 0 {
|
||
return "服务器网络错误!"
|
||
}
|
||
var result map[string]interface{}
|
||
err := json.Unmarshal([]byte(body), &result)
|
||
if err != nil {
|
||
return err.Error()
|
||
}
|
||
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
|
||
}
|