Skip to content

Commit 480523f

Browse files
committed
🎨 🔥 ⚡️ feat: pack pooled img sending
1 parent 7ec7ab8 commit 480523f

File tree

8 files changed

+62
-113
lines changed

8 files changed

+62
-113
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module github.com/FloatTech/ZeroBot-Plugin
33
go 1.17
44

55
require (
6-
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385
6+
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00
77
github.com/FloatTech/sqlite v0.2.0
8-
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220223090830-a5253f2f5260
8+
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224114458-4310f8e13ad3
99
github.com/antchfx/htmlquery v1.2.4
1010
github.com/corona10/goimagehash v1.0.3
1111
github.com/fogleman/gg v1.3.0

go.sum

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385 h1:MdGiGCZJh/Q9Z9pmZw0xkU7CFfbptmsJECRnT/aUNq4=
2-
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385/go.mod h1:zYjxb/MpWOjBgyiMli6356fegX2tJ21+u2TNStzVrHg=
1+
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00 h1:idUMQfvkooUapF1B2WDrm84QQqz7t+/bVnSTIrs4Khg=
2+
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00/go.mod h1:WGkFlN/5QbdptbQuvGrS7KQl5nAdQbvZLFsbrM7rWCU=
33
github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
44
github.com/FloatTech/sqlite v0.2.0 h1:x3uls/hExXH1+bbaNLkvilce6ATtWlDx4IqoxBW/bv8=
55
github.com/FloatTech/sqlite v0.2.0/go.mod h1:xIDWIvpOFl8AXmZm0FC8t3PZjiR6ZutytCpBv2EWCns=
6-
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220222140356-fceefeb900dc/go.mod h1:tUXu86+Hoi6vcLHfcVwjZa9ZYF4JRoNaCC510EELjfs=
7-
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220223090830-a5253f2f5260 h1:vpw6jSQQL71rYBzzYK6NjdtTYH7aVaz6WmzAIDbcxd0=
8-
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220223090830-a5253f2f5260/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg=
6+
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224051618-597cdb58b3fe/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg=
7+
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224114458-4310f8e13ad3 h1:rIi9+wMFfiYxEG6pCp5Mr6ZiBsstCGHrIt7moGkMEXM=
8+
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224114458-4310f8e13ad3/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg=
99
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
1010
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
1111
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0=
@@ -157,7 +157,6 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM
157157
github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0=
158158
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
159159
github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw=
160-
github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220222135901-74f9f7b24555/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog=
161160
github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220223090418-8c5d8eccaf3a h1:Mw7850zyEgIr8VO9e8iofo78maed53FclPzRiMa7aeY=
162161
github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220223090418-8c5d8eccaf3a/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog=
163162
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

plugin_acgimage/classify.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,10 @@ func init() { // 插件主体
113113
u = apihead + dhash
114114
}
115115

116-
m, hassent, err := pool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u)
117-
if err == nil && !hassent {
118-
// 发送图片
119-
id := ctx.SendChain(message.Image(m.String()))
120-
if id.ID() == 0 {
121-
id = ctx.SendChain(message.Image(m.String()).Add("cache", "0"))
122-
if id.ID() == 0 {
123-
ctx.SendChain(message.Text("图片发送失败,可能被风控了~"))
124-
}
125-
}
116+
err := pool.SendRemoteImageFromPool(dhash, u, ctxext.Send(ctx), ctxext.GetMessage(ctx))
117+
if err != nil {
118+
ctx.SendChain(message.Text("ERROR:", err))
119+
return
126120
}
127121
}
128122
})
@@ -159,11 +153,5 @@ func reply(ctx *zero.Ctx, class int, dhash string, comment string) error {
159153
}
160154
}
161155

162-
m, hassent, err := pool.NewImage(send, ctxext.GetMessage(ctx), b14, u)
163-
if err == nil && !hassent {
164-
if send(message.Message{message.Image(m.String())}) == 0 {
165-
send(message.Message{message.Image(m.String()).Add("cache", "0")})
166-
}
167-
}
168-
return err
156+
return pool.SendRemoteImageFromPool(b14, u, send, ctxext.GetMessage(ctx))
169157
}

plugin_fortune/fortune.go

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,39 +146,18 @@ func init() {
146146
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
147147
cachefile := cache + hex.EncodeToString(digest[:])
148148

149-
m, err := pool.GetImage(cachefile)
150-
if err != nil {
151-
logrus.Debugln("[fortune]", err)
152-
if file.IsNotExist(cachefile) {
153-
f, err := os.Create(cachefile)
154-
if err != nil {
155-
ctx.SendChain(message.Text("ERROR: ", err))
156-
return
157-
}
158-
_, err = draw(background, title, text, f)
159-
_ = f.Close()
160-
if err != nil {
161-
ctx.SendChain(message.Text("ERROR: ", err))
162-
return
163-
}
164-
}
165-
m.SetFile(file.BOTPATH + "/" + cachefile)
166-
hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx))
167-
if hassent {
168-
return
169-
}
149+
err = pool.SendImageFromPool(cachefile, cachefile, func() error {
150+
f, err := os.Create(cachefile)
170151
if err != nil {
171-
ctx.SendChain(message.Text("ERROR: ", err))
172-
return
173-
}
174-
}
175-
// 发送图片
176-
id := ctx.SendChain(message.Image(m.String()))
177-
if id.ID() == 0 {
178-
id = ctx.SendChain(message.Image(m.String()).Add("cache", "0"))
179-
if id.ID() == 0 {
180-
ctx.SendChain(message.Text("图片发送失败,可能被风控了~"))
152+
return err
181153
}
154+
_, err = draw(background, title, text, f)
155+
_ = f.Close()
156+
return err
157+
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
158+
if err != nil {
159+
ctx.SendChain(message.Text("ERROR: ", err))
160+
return
182161
}
183162
})
184163
}

plugin_image_finder/keyword.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/FloatTech/zbputils/control"
1616
"github.com/FloatTech/zbputils/control/order"
1717
"github.com/FloatTech/zbputils/ctxext"
18-
"github.com/FloatTech/zbputils/file"
1918
"github.com/FloatTech/zbputils/img/pool"
2019
"github.com/FloatTech/zbputils/web"
2120
)
@@ -55,31 +54,15 @@ func init() {
5554
}
5655
u := illust.ImageUrls[0]
5756
n := u[strings.LastIndex(u, "/")+1 : len(u)-4]
58-
m, err := pool.GetImage(n)
59-
if err != nil {
57+
f := illust.Path(0)
58+
59+
err = pool.SendImageFromPool(n, f, func() error {
6060
// 下载图片
61-
f := ""
62-
if f, err = illust.DownloadToCache(0, n); err != nil {
63-
ctx.SendChain(message.Text("ERROR: ", err))
64-
return
65-
}
66-
m.SetFile(file.BOTPATH + "/" + f)
67-
hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx))
68-
if hassent {
69-
return
70-
}
71-
if err != nil {
72-
ctx.SendChain(message.Text("ERROR: ", err))
73-
return
74-
}
75-
}
76-
// 发送图片
77-
id := ctx.SendChain(message.Image(m.String()))
78-
if id.ID() == 0 {
79-
id = ctx.SendChain(message.Image(m.String()).Add("cache", "0"))
80-
if id.ID() == 0 {
81-
ctx.SendChain(message.Text("图片发送失败,可能被风控了~"))
82-
}
61+
return illust.DownloadToCache(0)
62+
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
63+
if err != nil {
64+
ctx.SendChain(message.Text("ERROR: ", err))
65+
return
8366
}
8467
})
8568
}

plugin_lolicon/lolicon.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
)
2727

2828
var (
29-
queue = make(chan string, capacity)
29+
queue = make(chan [2]string, capacity)
3030
)
3131

3232
func init() {
@@ -64,23 +64,22 @@ func init() {
6464
process.SleepAbout1sTo2s()
6565
}
6666
if err == nil {
67-
queue <- m.String()
67+
queue <- [2]string{name, m.String()}
6868
} else {
69-
queue <- url
69+
queue <- [2]string{name, url}
7070
}
7171
}
7272
}()
7373
select {
7474
case <-time.After(time.Minute):
7575
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
76-
case url := <-queue:
77-
// 发送图片
78-
id := ctx.SendChain(message.Image(url))
79-
if id.ID() == 0 {
80-
id = ctx.SendChain(message.Image(url).Add("cache", "0"))
81-
if id.ID() == 0 {
82-
ctx.SendChain(message.Text("图片发送失败,可能被风控了~"))
83-
}
76+
case o := <-queue:
77+
name := o[0]
78+
url := o[1]
79+
err := pool.SendRemoteImageFromPool(name, url, ctxext.Send(ctx), ctxext.GetMessage(ctx))
80+
if err != nil {
81+
ctx.SendChain(message.Text("ERROR:", err))
82+
return
8483
}
8584
}
8685
})

plugin_saucenao/searcher.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,27 @@ func init() { // 插件主体
4343
name := strconv.FormatInt(illust.Pid, 10)
4444
var imgs message.Message
4545
for i := range illust.ImageUrls {
46+
f := file.BOTPATH + "/" + illust.Path(i)
4647
n := name + "_p" + strconv.Itoa(i)
47-
filepath := file.BOTPATH + "/" + pixiv.CacheDir + n
48-
f := ""
4948
var m *pool.Image
50-
switch {
51-
case file.IsExist(filepath + ".jpg"):
52-
f = filepath + ".jpg"
53-
case file.IsExist(filepath + ".png"):
54-
f = filepath + ".png"
55-
case file.IsExist(filepath + ".gif"):
56-
f = filepath + ".gif"
57-
default:
49+
if file.IsNotExist(f) {
5850
m, err = pool.GetImage(n)
5951
if err == nil {
60-
imgs = append(imgs, message.Image(m.String()).Add("cache", "0"))
61-
continue
52+
err = file.DownloadTo(m.String(), f, true)
53+
if err != nil {
54+
ctx.SendChain(message.Text("ERROR: ", err))
55+
return
56+
}
57+
break
6258
}
6359
logrus.Debugln("[sausenao]开始下载", n)
64-
filepath, err = illust.DownloadToCache(i, n)
60+
err = illust.DownloadToCache(i)
6561
if err == nil {
66-
f = file.BOTPATH + "/" + filepath
6762
m.SetFile(f)
6863
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
6964
}
7065
}
71-
if f != "" {
72-
imgs = append(imgs, message.Image("file:///"+f))
73-
}
66+
imgs = append(imgs, message.Image("file:///"+f))
7467
}
7568
txt := message.Text(
7669
"标题:", illust.Title, "\n",

plugin_setutime/setu_geter.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
sql "github.com/FloatTech/sqlite"
1414
control "github.com/FloatTech/zbputils/control"
1515
"github.com/FloatTech/zbputils/ctxext"
16+
"github.com/FloatTech/zbputils/file"
1617
fileutil "github.com/FloatTech/zbputils/file"
1718
imagepool "github.com/FloatTech/zbputils/img/pool"
1819
"github.com/FloatTech/zbputils/math"
@@ -156,21 +157,28 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
156157
n := u[strings.LastIndex(u, "/")+1 : len(u)-4]
157158
m, err := imagepool.GetImage(n)
158159
var msg message.MessageSegment
160+
f := fileutil.BOTPATH + "/" + illust.Path(0)
159161
if err != nil {
160162
// 下载图片
161-
f := ""
162-
if f, err = illust.DownloadToCache(0, n); err != nil {
163+
if err = illust.DownloadToCache(0); err != nil {
163164
ctx.SendChain(message.Text("ERROR: ", err))
164165
return
165166
}
166-
f = fileutil.BOTPATH + "/" + f
167167
m.SetFile(f)
168168
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
169169
msg = message.Image("file:///" + f)
170170
} else {
171171
msg = message.Image(m.String())
172172
if ctxext.SendToSelf(ctx)(msg) == 0 {
173173
msg = msg.Add("cache", "0")
174+
if ctxext.SendToSelf(ctx)(msg) == 0 {
175+
err = file.DownloadTo(m.String(), f, true)
176+
if err != nil {
177+
ctx.SendChain(message.Text("ERROR: ", err))
178+
return
179+
}
180+
msg = message.Image("file:///" + f)
181+
}
174182
}
175183
}
176184
p.poolmu.Lock()
@@ -235,7 +243,7 @@ func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error {
235243
return err
236244
}
237245
// 下载插画
238-
if _, err := illust.DownloadToCache(0, strconv.FormatInt(id, 10)+"_p0"); err != nil {
246+
if err := illust.DownloadToCache(0); err != nil {
239247
return err
240248
}
241249
// 发送到发送者

0 commit comments

Comments
 (0)