From 459683d3e8cd29c3e14c4897ea80244028a2ce08 Mon Sep 17 00:00:00 2001 From: Suika <290760339@qq.com> Date: Fri, 25 Jun 2021 23:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=8E=A5=E5=8F=A3:=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=98=B5=E7=A7=B0=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?uid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fensi/search.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 fensi/search.go diff --git a/fensi/search.go b/fensi/search.go new file mode 100644 index 00000000..d1a6c4a2 --- /dev/null +++ b/fensi/search.go @@ -0,0 +1,57 @@ +package fensi + +import ( + "encoding/json" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" + "net/http" +) + +func init() { + zero.OnRegex(`^/搜索 (.*)$`). + Handle(func(ctx *zero.Ctx) { + keyword := ctx.State["regex_matched"].([]string)[1] + searchJson := searchapi(keyword) + + if searchapi(keyword).Data.NumResults == 0 { + ctx.Send("名字没搜到") + return + } + + if searchapi(keyword).Data.NumResults < 5 { + ctx.SendChain(message.Text( + "uid: ", searchJson.Data.Result[0].Mid, "\n", + "name: ", searchJson.Data.Result[0].Uname, "\n", + )) + return + } + + ctx.SendChain(message.Text( + "搜索结果很多,请尽量准确关键字,以下为你返回前5条结果", "\n\n", + "uid1: ", searchJson.Data.Result[0].Mid, "\n", + "name1: ", searchJson.Data.Result[0].Uname, "\n\n", + "uid2: ", searchJson.Data.Result[1].Mid, "\n", + "name2: ", searchJson.Data.Result[1].Uname, "\n\n", + "uid3: ", searchJson.Data.Result[2].Mid, "\n", + "name3: ", searchJson.Data.Result[2].Uname, "\n\n", + "uid4: ", searchJson.Data.Result[3].Mid, "\n", + "name4: ", searchJson.Data.Result[3].Uname, "\n\n", + "uid5: ", searchJson.Data.Result[4].Mid, "\n", + "name5: ", searchJson.Data.Result[4].Uname, + )) + }) +} + +func searchapi(keyword string) *search { + url := "http://api.bilibili.com/x/web-interface/search/type?search_type=bili_user&&user_type=1&keyword=" + keyword + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + result := &search{} + if err := json.NewDecoder(resp.Body).Decode(result); err != nil { + panic(err) + } + return result +}