feat: 小鸡词典查梗 (#408)

This commit is contained in:
莫思潋 2022-09-12 20:04:35 +08:00 committed by GitHub
parent 99a8fcd73a
commit 002f971882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 0 deletions

View File

@ -709,6 +709,14 @@ print("run[CQ:image,file="+j["img"]+"]")
- [x] 更新[屌|弔|吊]图
</details>
<details>
<summary>小鸡词典</summary>
`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/jikipedia"`
- [x] [查梗|小鸡词典][梗]
</details>
<details>
<summary>绝绝子</summary>

View File

@ -90,6 +90,7 @@ import (
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/image_finder" // 关键字搜图
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/inject" // 注入指令
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jandan" // 煎蛋网无聊图
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jikipedia" // 小鸡词典
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/juejuezi" // 绝绝子生成器
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/lolicon" // lolicon 随机图片
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/midicreate" // 简易midi音乐制作

113
plugin/jikipedia/main.go Normal file
View File

@ -0,0 +1,113 @@
// Package jikipedia 小鸡词典
// 修改自https://github.com/TeamPGM/PagerMaid_Plugins_Pyro ,非常感谢!!
package jikipedia
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/FloatTech/floatbox/binary"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
const (
url = "https://api.jikipedia.com/go/search_entities"
)
type value struct {
Phrase string `json:"phrase"`
Page int `json:"page"`
Size int `json:"size"`
}
func init() {
// 初始化engine
engine := control.Register(
"jikipedia",
&ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Help: "小鸡词典\n -[查梗|小鸡词典][梗]",
},
)
engine.OnPrefix("小鸡词典").Limit(ctxext.LimitByGroup).SetBlock(true).Handle(
func(ctx *zero.Ctx) {
keyWord := strings.Trim(ctx.State["args"].(string), " ")
definition, err := parseKeyword(keyWord)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
if definition.String() == "" {
ctx.SendChain(message.Text("好像什么都没查到,换个关键词试一试?"))
return
}
ctx.SendChain(message.Text("【标题】:", definition.Get("term.title"),
"\n【释义】:", definition.Get("plaintext"),
"\n【原文】:https://jikipedia.com/definition/", definition.Get("id")))
},
)
}
func parseKeyword(keyWord string) (definition gjson.Result, err error) {
client := &http.Client{}
values := value{Phrase: keyWord, Page: 1, Size: 60}
jsonData, err := json.Marshal(values)
if err != nil {
return
}
var request *http.Request
request, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return
}
request.Header = http.Header{
"Accept": {"application/json, text/plain, */*"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"zh-CN,zh-TW;q=0.9,zh;q=0.8"},
"Client": {"web"},
"Client-Version": {"2.7.2g"},
"Connection": {"keep-alive"},
"Host": {"api.jikipedia.com"},
"Origin": {"https://jikipedia.com"},
"Referer": {"https://jikipedia.com/"},
"Sec-Fetch-Dest": {"empty"},
"Sec-Fetch-Mode": {"cors"},
"Sec-Fetch-Site": {"same-site"},
"Token": {""},
"User-Agent": {"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36"},
"XID": {"uNo5bL1nyNCp/Gm7lJAHQ91220HLbMT8jqk9IJYhtHA4ofP+zgxwM6lSDIKiYoppP2k1IW/1Vxc2vOVGxOOVReebsLmWPHhTs7NCRygfDkE="},
"sec-ch-ua": {`" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"`},
"sec-ch-ua-mobile": {"?1"},
"sec-ch-ua-platform": {`"Android"`},
}
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
var response *http.Response
response, err = client.Do(request)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
s := fmt.Sprintf("status code: %d", response.StatusCode)
err = errors.New(s)
return
}
data, err := io.ReadAll(response.Body)
if err != nil {
return
}
definition = gjson.Get(binary.BytesToString(data), "data.0.definitions.0")
return
}