🎨 🔥 ️ feat: pack pooled img sending

This commit is contained in:
fumiama 2022-02-24 19:54:48 +08:00
parent 7ec7ab8682
commit 480523ff27
8 changed files with 62 additions and 113 deletions

4
go.mod
View File

@ -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

11
go.sum
View File

@ -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=

View File

@ -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))
}

View File

@ -146,40 +146,19 @@ 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) {
err = pool.SendImageFromPool(cachefile, cachefile, func() error {
f, err := os.Create(cachefile)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
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
}
}
m.SetFile(file.BOTPATH + "/" + cachefile)
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("图片发送失败,可能被风控了~"))
}
}
})
}

View File

@ -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,32 +54,16 @@ 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
}
return illust.DownloadToCache(0)
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
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("图片发送失败,可能被风控了~"))
}
}
})
}

View File

@ -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
}
}
})

View File

@ -43,35 +43,28 @@ 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))
}
}
txt := message.Text(
"标题:", illust.Title, "\n",
"插画ID", illust.Pid, "\n",

View File

@ -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
}
// 发送到发送者