From 637759c9673332d8f18ec99c2a1103ba5c43f097 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 1 Sep 2024 16:36:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(mner):=20=E5=A2=9E=E5=8A=A0mner=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=20fix(ai):=20=E4=BF=AE=E5=A4=8Dai=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=9B=BE=E7=89=87bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/ai.go | 2 +- workers/core.go | 4 ++-- workers/mner.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 workers/mner.go diff --git a/workers/ai.go b/workers/ai.go index 72c5086..0394a08 100644 --- a/workers/ai.go +++ b/workers/ai.go @@ -96,7 +96,7 @@ func (a *AI) GetMsg() string { return "未找到回复消息" } file, picUrl, fileSizeStr := a.GetHisMsg(msgId) - + println("file:", file, "picUrl:", picUrl, "fileSizeStr:", fileSizeStr) // 正则表达式匹配 file 和 file_size 的值 // re = regexp.MustCompile(`file=([^,]+),.*file_size=(\d+)`) // matches = re.FindStringSubmatch(message) diff --git a/workers/core.go b/workers/core.go index 717814c..1a2acec 100644 --- a/workers/core.go +++ b/workers/core.go @@ -150,8 +150,8 @@ func (s *StdAns) GetHisMsg(id string) (string, string, string) { // } // } // ], - message := data["data"].(map[string]interface{})["message"].(map[string]interface{})["data"].(map[string]interface{}) - return message["file"].(string), message["url"].(string), message["file_id"].(string) + message := data["data"].(map[string]interface{})["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{}) + return message["file"].(string), message["url"].(string), message["file_size"].(string) } else { // fmt.Println("响应返回:", body) return "", "", "" diff --git a/workers/mner.go b/workers/mner.go new file mode 100644 index 0000000..ca62dc4 --- /dev/null +++ b/workers/mner.go @@ -0,0 +1,60 @@ +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 +}