-
Notifications
You must be signed in to change notification settings - Fork 2k
/
searcher.go
251 lines (244 loc) · 7.61 KB
/
searcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Package saucenao P站ID/saucenao/ascii2d搜图
package saucenao
import (
"fmt"
"net/http"
"os"
"reflect"
"strconv"
"github.com/fumiama/terasu/http2"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/AnimeAPI/ascii2d"
"github.com/FloatTech/AnimeAPI/pixiv"
"github.com/jozsefsallai/gophersauce"
"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/pool"
)
const (
enableHex = 0x10
unableHex = 0x7fffffff_fffffffd
)
var (
saucenaocli *gophersauce.Client
)
func init() { // 插件主体
engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "以图搜图",
Help: "- 以图搜图 | 搜索图片 | 以图识图[图片]\n" +
"- 搜图[P站图片ID]",
PrivateDataFolder: "saucenao",
})
apikeyfile := engine.DataFolder() + "apikey.txt"
if file.IsExist(apikeyfile) {
key, err := os.ReadFile(apikeyfile)
if err != nil {
panic(err)
}
saucenaocli, err = gophersauce.NewClient(&gophersauce.Settings{
MaxResults: 1,
APIKey: binary.BytesToString(key),
})
if err != nil {
panic(err)
}
}
// 根据 PID 搜图
engine.OnRegex(`^搜图(\d+)$`).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
id, _ := strconv.ParseInt(ctx.State["regex_matched"].([]string)[1], 10, 64)
ctx.SendChain(message.Text("少女祈祷中......"))
// 获取P站插图信息
illust, err := pixiv.Works(id)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
if illust.Pid > 0 {
name := strconv.FormatInt(illust.Pid, 10)
var imgs message.Message
for i := range illust.ImageUrls {
f := file.BOTPATH + "/" + illust.Path(i)
n := name + "_p" + strconv.Itoa(i)
var m *pool.Image
if file.IsNotExist(f) {
m, err = pool.GetImage(n)
if err == nil {
imgs = append(imgs, message.Image(m.String()))
continue
}
logrus.Debugln("[saucenao]开始下载", n)
logrus.Debugln("[saucenao]urls:", illust.ImageUrls)
err1 := illust.DownloadToCache(i)
if err1 == nil {
m.SetFile(f)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
}
if err1 != nil {
logrus.Debugln("[saucenao]下载err:", err1)
}
}
imgs = append(imgs, message.Image("file:///"+f))
}
txt := message.Text(
"标题: ", illust.Title, "\n",
"插画ID: ", illust.Pid, "\n",
"画师: ", illust.UserName, "\n",
"画师ID: ", illust.UserID, "\n",
"直链: ", "https://pixivel.moe/detail?id=", illust.Pid,
)
if imgs != nil {
ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, txt),
ctxext.FakeSenderForwardNode(ctx, imgs...)})
} else {
// 图片下载失败,仅发送文字结果
ctx.SendChain(txt)
}
} else {
ctx.SendChain(message.Text("图片不存在!"))
}
})
// 以图搜图
engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, zero.MustProvidePicture).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
// 开始搜索图片
pics, ok := ctx.State["image_url"].([]string)
showPic := false
if !ok {
ctx.SendChain(message.Text("ERROR: 未获取到图片链接"))
return
}
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if ok && c.GetData(ctx.Event.GroupID)&enableHex == enableHex {
showPic = true
}
ctx.SendChain(message.Text("少女祈祷中..."))
for _, pic := range pics {
if saucenaocli != nil {
resp, err := saucenaocli.FromURL(pic)
if err == nil && resp.Count() > 0 {
result := resp.First()
s, err := strconv.ParseFloat(result.Header.Similarity, 64)
if err == nil {
rr := reflect.ValueOf(&result.Data).Elem()
b := binary.NewWriterF(func(w *binary.Writer) {
r := rr.Type()
for i := 0; i < r.NumField(); i++ {
if !rr.Field(i).IsZero() {
w.WriteString("\n")
w.WriteString(r.Field(i).Name)
w.WriteString(": ")
w.WriteString(fmt.Sprint(rr.Field(i).Interface()))
}
}
})
resp, err := http2.Head(result.Header.Thumbnail)
msg := make(message.Message, 0, 3)
if s > 80.0 {
msg = append(msg, message.Text("我有把握是这个!"))
} else {
msg = append(msg, message.Text("也许是这个?"))
}
if showPic {
if err == nil {
_ = resp.Body.Close()
if resp.StatusCode == http.StatusOK {
msg = append(msg, message.Image(result.Header.Thumbnail))
} else {
msg = append(msg, message.Image(pic))
}
} else {
msg = append(msg, message.Image(pic))
}
}
msg = append(msg, message.Text("\n图源: ", result.Header.IndexName, binary.BytesToString(b)))
ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, msg...)})
if s > 80.0 {
continue
}
}
}
} else {
ctx.SendChain(message.Text("请私聊发送 设置 saucenao api key [apikey] 以启用 saucenao 搜图 (方括号不需要输入), key 请前往 https://saucenao.com/user.php?page=search-api 获取"))
}
// ascii2d 搜索
result, err := ascii2d.ASCII2d(pic)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
continue
}
msg := message.Message{ctxext.FakeSenderForwardNode(ctx, message.Text("ascii2d搜图结果"))}
for i := 0; i < len(result) && i < 5; i++ {
var resultMsgs message.Message
if showPic {
resultMsgs = append(resultMsgs, message.Image(result[i].Thumb))
}
resultMsgs = append(resultMsgs, message.Text(fmt.Sprintf(
"标题: %s\n图源: %s\n画师: %s\n画师链接: %s\n图片链接: %s",
result[i].Name,
result[i].Type,
result[i].AuthNm,
result[i].Author,
result[i].Link,
)))
msg = append(msg, ctxext.FakeSenderForwardNode(ctx, resultMsgs...))
}
if id := ctx.Send(msg).ID(); id == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
}
}
})
engine.OnRegex(`^设置\s?saucenao\s?api\s?key\s?([0-9a-f]{40})$`, zero.SuperUserPermission, zero.OnlyPrivate).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var err error
saucenaocli, err = gophersauce.NewClient(&gophersauce.Settings{
MaxResults: 1,
APIKey: ctx.State["regex_matched"].([]string)[1],
})
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
err = os.WriteFile(apikeyfile, binary.StringToBytes(saucenaocli.APIKey), 0644)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("成功!"))
})
engine.OnRegex(`^(开启|打开|启用|关闭|关掉|禁用)搜图显示图片$`, zero.AdminPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
if gid <= 0 {
// 个人用户设为负数
gid = -ctx.Event.UserID
}
option := ctx.State["regex_matched"].([]string)[1]
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
if !ok {
ctx.SendChain(message.Text("找不到服务!"))
return
}
data := c.GetData(ctx.Event.GroupID)
switch option {
case "开启", "打开", "启用":
data |= enableHex
case "关闭", "关掉", "禁用":
data &= unableHex
default:
return
}
err := c.SetData(gid, data)
if err != nil {
ctx.SendChain(message.Text("出错啦: ", err))
return
}
ctx.SendChain(message.Text("已", option, "搜图显示图片"))
})
}