搜索接口:使用昵称获取用户uid

This commit is contained in:
Suika 2021-06-25 23:02:49 +08:00 committed by GitHub
parent 792f866f93
commit 459683d3e8

57
fensi/search.go Normal file
View File

@ -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
}