@@ -3,6 +3,7 @@ package saucenao
3
3
4
4
import (
5
5
"fmt"
6
+ "os"
6
7
"strconv"
7
8
8
9
"github.com/sirupsen/logrus"
@@ -11,21 +12,41 @@ import (
11
12
12
13
"github.com/FloatTech/AnimeAPI/ascii2d"
13
14
"github.com/FloatTech/AnimeAPI/pixiv"
14
- "github.com/FloatTech/AnimeAPI/saucenao "
15
+ "github.com/jozsefsallai/gophersauce "
15
16
17
+ "github.com/FloatTech/zbputils/binary"
16
18
"github.com/FloatTech/zbputils/control"
17
19
"github.com/FloatTech/zbputils/ctxext"
18
20
"github.com/FloatTech/zbputils/file"
19
21
"github.com/FloatTech/zbputils/img/pool"
20
22
)
21
23
24
+ var (
25
+ saucenaocli * gophersauce.Client
26
+ )
27
+
22
28
func init () { // 插件主体
23
29
engine := control .Register ("saucenao" , & control.Options {
24
30
DisableOnDefault : false ,
25
31
Help : "搜图\n " +
26
32
"- 以图搜图 | 搜索图片 | 以图识图[图片]\n " +
27
33
"- 搜图[P站图片ID]" ,
34
+ PrivateDataFolder : "saucenao" ,
28
35
})
36
+ apikeyfile := engine .DataFolder () + "apikey.txt"
37
+ if file .IsExist (apikeyfile ) {
38
+ key , err := os .ReadFile (apikeyfile )
39
+ if err != nil {
40
+ panic (err )
41
+ }
42
+ saucenaocli , err = gophersauce .NewClient (& gophersauce.Settings {
43
+ MaxResults : 1 ,
44
+ APIKey : binary .BytesToString (key ),
45
+ })
46
+ if err != nil {
47
+ panic (err )
48
+ }
49
+ }
29
50
// 根据 PID 搜图
30
51
engine .OnRegex (`^搜图(\d+)$` ).SetBlock (true ).
31
52
Handle (func (ctx * zero.Ctx ) {
@@ -64,11 +85,11 @@ func init() { // 插件主体
64
85
imgs = append (imgs , message .Image ("file:///" + f ))
65
86
}
66
87
txt := message .Text (
67
- "标题: " , illust .Title , "\n " ,
68
- "插画ID: " , illust .Pid , "\n " ,
69
- "画师: " , illust .UserName , "\n " ,
70
- "画师ID: " , illust .UserId , "\n " ,
71
- "直链: " , "https://pixivel.moe/detail?id=" , illust .Pid ,
88
+ "标题: " , illust .Title , "\n " ,
89
+ "插画ID: " , illust .Pid , "\n " ,
90
+ "画师: " , illust .UserName , "\n " ,
91
+ "画师ID: " , illust .UserId , "\n " ,
92
+ "直链: " , "https://pixivel.moe/detail?id=" , illust .Pid ,
72
93
)
73
94
if imgs != nil {
74
95
// 发送搜索结果
@@ -87,24 +108,81 @@ func init() { // 插件主体
87
108
// 开始搜索图片
88
109
ctx .SendChain (message .Text ("少女祈祷中......" ))
89
110
for _ , pic := range ctx .State ["image_url" ].([]string ) {
90
- if result , err := saucenao .SauceNAO (pic ); err != nil {
111
+ if saucenaocli != nil {
112
+ resp , err := saucenaocli .FromURL (pic )
113
+ if err == nil && resp .Count () > 0 {
114
+ result := resp .First ()
115
+ // 返回SauceNAO的结果
116
+ picid := 0
117
+ picidstr := ""
118
+ source := ""
119
+ switch {
120
+ case result .IsPixiv ():
121
+ picid = result .Data .PixivID
122
+ source = "Pixiv"
123
+ case result .IsAniDB ():
124
+ picid = result .Data .AniDBAID
125
+ source = "AniDB"
126
+ case result .IsBcy ():
127
+ picid = result .Data .BcyID
128
+ source = "Bcy"
129
+ case result .IsDanbooru ():
130
+ picid = result .Data .DanbooruID
131
+ source = "Danbooru"
132
+ case result .IsDeviantArt ():
133
+ picid = result .Data .DeviantArtID
134
+ source = "DeviantArt"
135
+ case result .IsIMDb ():
136
+ picidstr = result .Data .IMDbID
137
+ source = "IMDb"
138
+ case result .IsPawoo ():
139
+ picid = result .Data .PawooID
140
+ source = "Pawoo"
141
+ case result .IsSankaku ():
142
+ picid = result .Data .SankakuID
143
+ source = "Sankaku"
144
+ case result .IsSeiga ():
145
+ picid = result .Data .SeigaID
146
+ source = "Seiga"
147
+ }
148
+ if picid != 0 {
149
+ ctx .SendChain (
150
+ message .Text ("我有把握是这个!" ),
151
+ message .Image (result .Header .Thumbnail ),
152
+ message .Text (
153
+ "\n " ,
154
+ "图源: " , source , "\n " ,
155
+ "相似度: " , result .Header .Similarity , "\n " ,
156
+ "标题: " , result .Data .Title , "\n " ,
157
+ "插画ID: " , picid , "\n " ,
158
+ "画师: " , result .Data .MemberName , "\n " ,
159
+ "画师ID: " , result .Data .MemberID , "\n " ,
160
+ "直链: " , "https://pixivel.moe/detail?id=" , result .Data .PixivID ,
161
+ ),
162
+ )
163
+ continue
164
+ }
165
+ if picidstr != "" {
166
+ ctx .SendChain (
167
+ message .Text ("我有把握是这个!" ),
168
+ message .Image (result .Header .Thumbnail ),
169
+ message .Text (
170
+ "\n " ,
171
+ "图源: " , source , "\n " ,
172
+ "相似度: " , result .Header .Similarity , "\n " ,
173
+ "标题: " , result .Data .Title , "\n " ,
174
+ "插画ID: " , picidstr , "\n " ,
175
+ "画师: " , result .Data .MemberName , "\n " ,
176
+ "画师ID: " , result .Data .MemberID , "\n " ,
177
+ "直链: " , "https://pixivel.moe/detail?id=" , result .Data .PixivID ,
178
+ ),
179
+ )
180
+ continue
181
+ }
182
+ }
91
183
ctx .SendChain (message .Text ("ERROR:" , err ))
92
184
} else {
93
- // 返回SauceNAO的结果
94
- ctx .SendChain (
95
- message .Text ("我有把握是这个!" ),
96
- message .Image (result [0 ].Thumbnail ),
97
- message .Text (
98
- "\n " ,
99
- "相似度:" , result [0 ].Similarity , "\n " ,
100
- "标题:" , result [0 ].Title , "\n " ,
101
- "插画ID:" , result [0 ].PixivID , "\n " ,
102
- "画师:" , result [0 ].MemberName , "\n " ,
103
- "画师ID:" , result [0 ].MemberID , "\n " ,
104
- "直链:" , "https://pixivel.moe/detail?id=" , result [0 ].PixivID ,
105
- ),
106
- )
107
- continue
185
+ ctx .SendChain (message .Text ("请私聊发送 设置 saucenao api key [apikey] 以启用 saucenao 搜图, key 请前往 https://saucenao.com/user.php?page=search-api 获取" ))
108
186
}
109
187
// ascii2d 搜索
110
188
if result , err := ascii2d .Ascii2d (pic ); err != nil {
@@ -116,7 +194,7 @@ func init() { // 插件主体
116
194
msg = append (msg , ctxext .FakeSenderForwardNode (ctx ,
117
195
message .Image (result [i ].Thumb ),
118
196
message .Text (fmt .Sprintf (
119
- "标题: %s\n 图源: %s\n 画师: %s\n 画师链接: %s\n 图片链接: %s" ,
197
+ "标题: %s\n 图源: %s\n 画师: %s\n 画师链接: %s\n 图片链接: %s" ,
120
198
result [i ].Name ,
121
199
result [i ].Type ,
122
200
result [i ].AuthNm ,
@@ -134,4 +212,22 @@ func init() { // 插件主体
134
212
}
135
213
}
136
214
})
215
+ engine .OnRegex (`^设置\s?saucenao\s?api\s?key\s?([0-9a-f]{40})$` , zero .SuperUserPermission , zero .OnlyPrivate ).SetBlock (true ).
216
+ Handle (func (ctx * zero.Ctx ) {
217
+ var err error
218
+ saucenaocli , err = gophersauce .NewClient (& gophersauce.Settings {
219
+ MaxResults : 1 ,
220
+ APIKey : ctx .State ["regex_matched" ].([]string )[1 ],
221
+ })
222
+ if err != nil {
223
+ ctx .SendChain (message .Text ("ERROR:" , err ))
224
+ return
225
+ }
226
+ err = os .WriteFile (apikeyfile , binary .StringToBytes (saucenaocli .APIKey ), 0644 )
227
+ if err != nil {
228
+ ctx .SendChain (message .Text ("ERROR:" , err ))
229
+ return
230
+ }
231
+ ctx .SendChain (message .Text ("成功!" ))
232
+ })
137
233
}
0 commit comments