From 002f9718826315bf14fd732f50aee2bc7ec1ce55 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=8E=AB=E6=80=9D=E6=BD=8B?=
<55676105+shudorcl@users.noreply.github.com>
Date: Mon, 12 Sep 2022 20:04:35 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=8F=E9=B8=A1=E8=AF=8D=E5=85=B8?=
=?UTF-8?q?=E6=9F=A5=E6=A2=97=20(#408)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 8 +++
main.go | 1 +
plugin/jikipedia/main.go | 113 +++++++++++++++++++++++++++++++++++++++
3 files changed, 122 insertions(+)
create mode 100644 plugin/jikipedia/main.go
diff --git a/README.md b/README.md
index 962413f6..eb868d5d 100644
--- a/README.md
+++ b/README.md
@@ -709,6 +709,14 @@ print("run[CQ:image,file="+j["img"]+"]")
- [x] 更新[屌|弔|吊]图
+
+
+ 小鸡词典
+
+ `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/jikipedia"`
+
+ - [x] [查梗|小鸡词典][梗]
+
绝绝子
diff --git a/main.go b/main.go
index 44b3c2f0..a5c6447c 100644
--- a/main.go
+++ b/main.go
@@ -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音乐制作
diff --git a/plugin/jikipedia/main.go b/plugin/jikipedia/main.go
new file mode 100644
index 00000000..9ecb79e4
--- /dev/null
+++ b/plugin/jikipedia/main.go
@@ -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
+}