From 480523ff275c9d61337ac600622e2edc9a91b208 Mon Sep 17 00:00:00 2001 From: fumiama Date: Thu, 24 Feb 2022 19:54:48 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=F0=9F=94=A5=20=E2=9A=A1?= =?UTF-8?q?=EF=B8=8F=20feat:=20pack=20pooled=20img=20sending?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 11 +++++---- plugin_acgimage/classify.go | 22 +++++------------- plugin_fortune/fortune.go | 41 +++++++++------------------------- plugin_image_finder/keyword.go | 33 +++++++-------------------- plugin_lolicon/lolicon.go | 21 +++++++++-------- plugin_saucenao/searcher.go | 27 +++++++++------------- plugin_setutime/setu_geter.go | 16 +++++++++---- 8 files changed, 62 insertions(+), 113 deletions(-) diff --git a/go.mod b/go.mod index 45e5f356..666ed211 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.17 require ( - github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385 + github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00 github.com/FloatTech/sqlite v0.2.0 - github.com/FloatTech/zbputils v1.3.0-beta8.0.20220223090830-a5253f2f5260 + github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224114458-4310f8e13ad3 github.com/antchfx/htmlquery v1.2.4 github.com/corona10/goimagehash v1.0.3 github.com/fogleman/gg v1.3.0 diff --git a/go.sum b/go.sum index 84d3135a..ae1b0df0 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,11 @@ -github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385 h1:MdGiGCZJh/Q9Z9pmZw0xkU7CFfbptmsJECRnT/aUNq4= -github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385/go.mod h1:zYjxb/MpWOjBgyiMli6356fegX2tJ21+u2TNStzVrHg= +github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00 h1:idUMQfvkooUapF1B2WDrm84QQqz7t+/bVnSTIrs4Khg= +github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00/go.mod h1:WGkFlN/5QbdptbQuvGrS7KQl5nAdQbvZLFsbrM7rWCU= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/sqlite v0.2.0 h1:x3uls/hExXH1+bbaNLkvilce6ATtWlDx4IqoxBW/bv8= github.com/FloatTech/sqlite v0.2.0/go.mod h1:xIDWIvpOFl8AXmZm0FC8t3PZjiR6ZutytCpBv2EWCns= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220222140356-fceefeb900dc/go.mod h1:tUXu86+Hoi6vcLHfcVwjZa9ZYF4JRoNaCC510EELjfs= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220223090830-a5253f2f5260 h1:vpw6jSQQL71rYBzzYK6NjdtTYH7aVaz6WmzAIDbcxd0= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220223090830-a5253f2f5260/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224051618-597cdb58b3fe/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224114458-4310f8e13ad3 h1:rIi9+wMFfiYxEG6pCp5Mr6ZiBsstCGHrIt7moGkMEXM= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224114458-4310f8e13ad3/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0= 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 github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw= -github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220222135901-74f9f7b24555/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog= github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220223090418-8c5d8eccaf3a h1:Mw7850zyEgIr8VO9e8iofo78maed53FclPzRiMa7aeY= github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220223090418-8c5d8eccaf3a/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/plugin_acgimage/classify.go b/plugin_acgimage/classify.go index ed63e487..6090aa74 100644 --- a/plugin_acgimage/classify.go +++ b/plugin_acgimage/classify.go @@ -113,16 +113,10 @@ func init() { // 插件主体 u = apihead + dhash } - m, hassent, err := pool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u) - if err == nil && !hassent { - // 发送图片 - id := ctx.SendChain(message.Image(m.String())) - if id.ID() == 0 { - id = ctx.SendChain(message.Image(m.String()).Add("cache", "0")) - if id.ID() == 0 { - ctx.SendChain(message.Text("图片发送失败,可能被风控了~")) - } - } + err := pool.SendRemoteImageFromPool(dhash, u, ctxext.Send(ctx), ctxext.GetMessage(ctx)) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return } } }) @@ -159,11 +153,5 @@ func reply(ctx *zero.Ctx, class int, dhash string, comment string) error { } } - m, hassent, err := pool.NewImage(send, ctxext.GetMessage(ctx), b14, u) - if err == nil && !hassent { - if send(message.Message{message.Image(m.String())}) == 0 { - send(message.Message{message.Image(m.String()).Add("cache", "0")}) - } - } - return err + return pool.SendRemoteImageFromPool(b14, u, send, ctxext.GetMessage(ctx)) } diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index fb83efbb..fe805d33 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -146,39 +146,18 @@ func init() { digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text)) cachefile := cache + hex.EncodeToString(digest[:]) - m, err := pool.GetImage(cachefile) - if err != nil { - logrus.Debugln("[fortune]", err) - if file.IsNotExist(cachefile) { - f, err := os.Create(cachefile) - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - _, err = draw(background, title, text, f) - _ = f.Close() - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - } - m.SetFile(file.BOTPATH + "/" + cachefile) - hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx)) - if hassent { - return - } + err = pool.SendImageFromPool(cachefile, cachefile, func() error { + f, err := os.Create(cachefile) if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - } - // 发送图片 - id := ctx.SendChain(message.Image(m.String())) - if id.ID() == 0 { - id = ctx.SendChain(message.Image(m.String()).Add("cache", "0")) - if id.ID() == 0 { - ctx.SendChain(message.Text("图片发送失败,可能被风控了~")) + return err } + _, err = draw(background, title, text, f) + _ = f.Close() + return err + }, ctxext.Send(ctx), ctxext.GetMessage(ctx)) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return } }) } diff --git a/plugin_image_finder/keyword.go b/plugin_image_finder/keyword.go index 6b858949..ff42aaa8 100644 --- a/plugin_image_finder/keyword.go +++ b/plugin_image_finder/keyword.go @@ -15,7 +15,6 @@ import ( "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control/order" "github.com/FloatTech/zbputils/ctxext" - "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/img/pool" "github.com/FloatTech/zbputils/web" ) @@ -55,31 +54,15 @@ func init() { } u := illust.ImageUrls[0] n := u[strings.LastIndex(u, "/")+1 : len(u)-4] - m, err := pool.GetImage(n) - if err != nil { + f := illust.Path(0) + + err = pool.SendImageFromPool(n, f, func() error { // 下载图片 - f := "" - if f, err = illust.DownloadToCache(0, n); err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - m.SetFile(file.BOTPATH + "/" + f) - hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx)) - if hassent { - return - } - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - } - // 发送图片 - id := ctx.SendChain(message.Image(m.String())) - if id.ID() == 0 { - id = ctx.SendChain(message.Image(m.String()).Add("cache", "0")) - if id.ID() == 0 { - ctx.SendChain(message.Text("图片发送失败,可能被风控了~")) - } + return illust.DownloadToCache(0) + }, ctxext.Send(ctx), ctxext.GetMessage(ctx)) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return } }) } diff --git a/plugin_lolicon/lolicon.go b/plugin_lolicon/lolicon.go index 155982c0..7fcabc7a 100644 --- a/plugin_lolicon/lolicon.go +++ b/plugin_lolicon/lolicon.go @@ -26,7 +26,7 @@ const ( ) var ( - queue = make(chan string, capacity) + queue = make(chan [2]string, capacity) ) func init() { @@ -64,23 +64,22 @@ func init() { process.SleepAbout1sTo2s() } if err == nil { - queue <- m.String() + queue <- [2]string{name, m.String()} } else { - queue <- url + queue <- [2]string{name, url} } } }() select { case <-time.After(time.Minute): ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......")) - case url := <-queue: - // 发送图片 - id := ctx.SendChain(message.Image(url)) - if id.ID() == 0 { - id = ctx.SendChain(message.Image(url).Add("cache", "0")) - if id.ID() == 0 { - ctx.SendChain(message.Text("图片发送失败,可能被风控了~")) - } + case o := <-queue: + name := o[0] + url := o[1] + err := pool.SendRemoteImageFromPool(name, url, ctxext.Send(ctx), ctxext.GetMessage(ctx)) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return } } }) diff --git a/plugin_saucenao/searcher.go b/plugin_saucenao/searcher.go index 5a66995f..6def900c 100644 --- a/plugin_saucenao/searcher.go +++ b/plugin_saucenao/searcher.go @@ -43,34 +43,27 @@ func init() { // 插件主体 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) - filepath := file.BOTPATH + "/" + pixiv.CacheDir + n - f := "" var m *pool.Image - switch { - case file.IsExist(filepath + ".jpg"): - f = filepath + ".jpg" - case file.IsExist(filepath + ".png"): - f = filepath + ".png" - case file.IsExist(filepath + ".gif"): - f = filepath + ".gif" - default: + if file.IsNotExist(f) { m, err = pool.GetImage(n) if err == nil { - imgs = append(imgs, message.Image(m.String()).Add("cache", "0")) - continue + err = file.DownloadTo(m.String(), f, true) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + break } logrus.Debugln("[sausenao]开始下载", n) - filepath, err = illust.DownloadToCache(i, n) + err = illust.DownloadToCache(i) if err == nil { - f = file.BOTPATH + "/" + filepath m.SetFile(f) _, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) } } - if f != "" { - imgs = append(imgs, message.Image("file:///"+f)) - } + imgs = append(imgs, message.Image("file:///"+f)) } txt := message.Text( "标题:", illust.Title, "\n", diff --git a/plugin_setutime/setu_geter.go b/plugin_setutime/setu_geter.go index 9ddd98b8..efa31421 100644 --- a/plugin_setutime/setu_geter.go +++ b/plugin_setutime/setu_geter.go @@ -13,6 +13,7 @@ import ( sql "github.com/FloatTech/sqlite" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" fileutil "github.com/FloatTech/zbputils/file" imagepool "github.com/FloatTech/zbputils/img/pool" "github.com/FloatTech/zbputils/math" @@ -156,14 +157,13 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) { n := u[strings.LastIndex(u, "/")+1 : len(u)-4] m, err := imagepool.GetImage(n) var msg message.MessageSegment + f := fileutil.BOTPATH + "/" + illust.Path(0) if err != nil { // 下载图片 - f := "" - if f, err = illust.DownloadToCache(0, n); err != nil { + if err = illust.DownloadToCache(0); err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return } - f = fileutil.BOTPATH + "/" + f m.SetFile(f) _, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) msg = message.Image("file:///" + f) @@ -171,6 +171,14 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) { msg = message.Image(m.String()) if ctxext.SendToSelf(ctx)(msg) == 0 { msg = msg.Add("cache", "0") + if ctxext.SendToSelf(ctx)(msg) == 0 { + err = file.DownloadTo(m.String(), f, true) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + msg = message.Image("file:///" + f) + } } } p.poolmu.Lock() @@ -235,7 +243,7 @@ func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error { return err } // 下载插画 - if _, err := illust.DownloadToCache(0, strconv.FormatInt(id, 10)+"_p0"); err != nil { + if err := illust.DownloadToCache(0); err != nil { return err } // 发送到发送者