Skip to content

Commit a9e13d3

Browse files
committed
🎨 优化代码结构
1 parent 0698d8e commit a9e13d3

File tree

1 file changed

+60
-67
lines changed

1 file changed

+60
-67
lines changed

plugin/diana/zhiwang.go

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,87 @@ package diana
33

44
import (
55
"bytes"
6-
"io"
76
"math"
7+
"strings"
88
"time"
99

10+
"github.com/FloatTech/zbputils/binary"
11+
"github.com/FloatTech/zbputils/ctxext"
12+
"github.com/FloatTech/zbputils/web"
1013
"github.com/tidwall/gjson"
1114
"github.com/wdvxdr1123/ZeroBot/message"
1215

13-
"net/http"
14-
"strings"
15-
1616
zero "github.com/wdvxdr1123/ZeroBot"
1717
)
1818

1919
// 小作文查重: 回复要查的消息 查重
2020
func init() {
21-
engine.OnMessage(fullmatch("查重")).SetBlock(true).
22-
Handle(func(ctx *zero.Ctx) {
23-
msg := ctx.Event.Message
24-
if msg[0].Type == "reply" {
25-
msg := ctx.GetMessage(message.NewMessageID(msg[0].Data["id"])).Elements[0].Data["text"]
26-
zhiwangjson := zhiwangapi(msg)
27-
if zhiwangjson == nil || zhiwangjson.Get("code").Int() != 0 {
28-
ctx.SendChain(message.Text("api返回错误"))
29-
return
30-
}
31-
32-
if zhiwangjson.Get("data.related.#").Int() == 0 {
33-
ctx.SendChain(message.Text("枝网没搜到,查重率为0%,鉴定为原创"))
34-
return
35-
}
36-
related := zhiwangjson.Get("data.related.0.reply").Map()
37-
rate := zhiwangjson.Get("data.related.0.rate").Float()
38-
ctx.SendChain(message.Text(
39-
"枝网文本复制检测报告(简洁)", "\n",
40-
"查重时间: ", time.Now().Format("2006-01-02 15:04:05"), "\n",
41-
"总文字复制比: ", math.Floor(rate*100), "%", "\n",
42-
"相似小作文:", "\n",
43-
related["content"].String()[:102]+".....", "\n",
44-
"获赞数:", related["like_num"].String(), "\n",
45-
zhiwangjson.Get("data.related.0.reply_url").String(), "\n",
46-
"作者: ", related["m_name"].String(), "\n",
47-
"发表时间: ", time.Unix(int64(related["ctime"].Float()), 0).Format("2006-01-02 15:04:05"), "\n",
48-
"查重结果仅作参考,请注意辨别是否为原创", "\n",
49-
"数据来源: https://asoulcnki.asia/",
50-
))
51-
}
52-
})
53-
}
54-
55-
func zhiwangapi(text string) *gjson.Result {
56-
url := "https://asoulcnki.asia/v1/api/check"
57-
post := "{\n\"text\":\"" + text + "\"\n}"
58-
var jsonStr = []byte(post)
59-
60-
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
61-
req.Header.Set("Content-Type", "application/json")
62-
client := &http.Client{}
63-
64-
resp, err := client.Do(req)
65-
66-
if err != nil {
67-
return nil
68-
}
69-
bodyBytes, err := io.ReadAll(resp.Body)
70-
if err != nil {
71-
return nil
72-
}
73-
resp.Body.Close()
74-
result := gjson.ParseBytes(bodyBytes)
75-
return &result
76-
}
77-
78-
func fullmatch(src ...string) zero.Rule {
79-
return func(ctx *zero.Ctx) bool {
21+
engine.OnMessage(func(ctx *zero.Ctx) bool {
8022
msg := ctx.Event.Message
23+
if msg[0].Type != "reply" {
24+
return false
25+
}
8126
for _, elem := range msg {
8227
if elem.Type == "text" {
8328
text := elem.Data["text"]
8429
text = strings.ReplaceAll(text, " ", "")
8530
text = strings.ReplaceAll(text, "\r", "")
8631
text = strings.ReplaceAll(text, "\n", "")
87-
for _, s := range src {
88-
if text == s {
89-
return true
90-
}
32+
if text == "查重" {
33+
return true
9134
}
9235
}
9336
}
9437
return false
38+
}).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
39+
msg := ctx.GetMessage(message.NewMessageID(ctx.Event.Message[0].Data["id"])).Elements[0].Data["text"]
40+
result, err := zhiwangapi(msg)
41+
if err != nil {
42+
ctx.SendChain(message.Text("ERROR:", err))
43+
return
44+
}
45+
if result.Get("code").Int() != 0 {
46+
ctx.SendChain(message.Text("api返回错误:", result.Get("code").Int()))
47+
return
48+
}
49+
if result.Get("data.related.#").Int() == 0 {
50+
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("枝网没搜到,查重率为0%,鉴定为原创")))
51+
return
52+
}
53+
related := result.Get("data.related.0.reply").Map()
54+
rate := result.Get("data.related.0.rate").Float()
55+
relatedcontent := related["content"].String()
56+
if len(relatedcontent) > 102 {
57+
relatedcontent = relatedcontent[:102] + "....."
58+
}
59+
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text(
60+
"枝网文本复制检测报告(简洁)", "\n",
61+
"查重时间: ", time.Now().Format("2006-01-02 15:04:05"), "\n",
62+
"总文字复制比: ", math.Floor(rate*100), "%", "\n",
63+
"相似小作文:", "\n", relatedcontent, "\n",
64+
"获赞数:", related["like_num"].String(), "\n",
65+
result.Get("data.related.0.reply_url").String(), "\n",
66+
"作者: ", related["m_name"].String(), "\n",
67+
"发表时间: ", time.Unix(int64(related["ctime"].Float()), 0).Format("2006-01-02 15:04:05"), "\n",
68+
"查重结果仅作参考,请注意辨别是否为原创", "\n",
69+
"数据来源: https://asoulcnki.asia/",
70+
)))
71+
})
72+
}
73+
74+
func zhiwangapi(text string) (*gjson.Result, error) {
75+
b, cl := binary.OpenWriterF(func(w *binary.Writer) {
76+
w.WriteString("{\n\"text\":\"")
77+
w.WriteString(text)
78+
w.WriteString("\"\n}")
79+
})
80+
81+
data, err := web.PostData("https://asoulcnki.asia/v1/api/check", "application/json", bytes.NewReader(b))
82+
cl()
83+
if err != nil {
84+
return nil, err
9585
}
86+
87+
result := gjson.ParseBytes(data)
88+
return &result, nil
9689
}

0 commit comments

Comments
 (0)