mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
optimize(img): drop remote image pool
This commit is contained in:
parent
ef3fa92de3
commit
8bcab9bf43
2
go.mod
2
go.mod
@ -12,7 +12,7 @@ require (
|
|||||||
github.com/FloatTech/sqlite v1.6.3
|
github.com/FloatTech/sqlite v1.6.3
|
||||||
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
|
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
|
||||||
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb
|
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb
|
||||||
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8
|
github.com/FloatTech/zbputils v1.7.2-0.20241013175349-67b3df244b08
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
|
||||||
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
|
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
|
||||||
github.com/antchfx/htmlquery v1.3.3
|
github.com/antchfx/htmlquery v1.3.3
|
||||||
|
|||||||
4
go.sum
4
go.sum
@ -17,8 +17,8 @@ github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ
|
|||||||
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
|
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
|
||||||
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb h1:sGqwCiMDyUD/znWEVVRVxbd6Kg1KLgGnnIuq5bCUWaQ=
|
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb h1:sGqwCiMDyUD/znWEVVRVxbd6Kg1KLgGnnIuq5bCUWaQ=
|
||||||
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo=
|
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo=
|
||||||
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8 h1:XlOrX7WJqIQVBziB+ZXXzp/OG0YQICO5IC6T9s4iohE=
|
github.com/FloatTech/zbputils v1.7.2-0.20241013175349-67b3df244b08 h1:fwVPkvJ5dR0/GisRomaDHEJ5whQBu6dSLQJpAP1gbwA=
|
||||||
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8/go.mod h1:2ZS6vZHn/imRIOhlDytd/FPVdBIY5KYdTPTIwBJU9Js=
|
github.com/FloatTech/zbputils v1.7.2-0.20241013175349-67b3df244b08/go.mod h1:J/uDSgIGr8exK1B49XebRKAKphgg/Gpfh1bpjb6t6GY=
|
||||||
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
|
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
|
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
|
||||||
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
|
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
|
||||||
|
|||||||
@ -146,7 +146,7 @@ func init() {
|
|||||||
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
|
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
|
||||||
cachefile := cache + hex.EncodeToString(digest[:])
|
cachefile := cache + hex.EncodeToString(digest[:])
|
||||||
|
|
||||||
err = pool.SendImageFromPool(cachefile, cachefile, func() error {
|
err = pool.SendImageFromPool(cachefile, func(cachefile string) error {
|
||||||
f, err := os.Create(cachefile)
|
f, err := os.Create(cachefile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -154,7 +154,7 @@ func init() {
|
|||||||
_, err = draw(background, fontdata, title, text, f)
|
_, err = draw(background, fontdata, title, text, f)
|
||||||
_ = f.Close()
|
_ = f.Close()
|
||||||
return err
|
return err
|
||||||
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
|
}, ctxext.Send(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
return
|
return
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import (
|
|||||||
ctrl "github.com/FloatTech/zbpctrl"
|
ctrl "github.com/FloatTech/zbpctrl"
|
||||||
"github.com/FloatTech/zbputils/control"
|
"github.com/FloatTech/zbputils/control"
|
||||||
"github.com/FloatTech/zbputils/ctxext"
|
"github.com/FloatTech/zbputils/ctxext"
|
||||||
imagepool "github.com/FloatTech/zbputils/img/pool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -68,18 +67,7 @@ func init() {
|
|||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
name := imageurl[strings.LastIndex(imageurl, "/")+1 : len(imageurl)-4]
|
queue <- imageurl
|
||||||
m, err := imagepool.GetImage(name)
|
|
||||||
if err != nil {
|
|
||||||
m.SetFile(imageurl)
|
|
||||||
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
|
||||||
process.SleepAbout1sTo2s()
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
queue <- m.String()
|
|
||||||
} else {
|
|
||||||
queue <- imageurl
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
select {
|
select {
|
||||||
|
|||||||
@ -22,7 +22,6 @@ import (
|
|||||||
ctrl "github.com/FloatTech/zbpctrl"
|
ctrl "github.com/FloatTech/zbpctrl"
|
||||||
"github.com/FloatTech/zbputils/control"
|
"github.com/FloatTech/zbputils/control"
|
||||||
"github.com/FloatTech/zbputils/ctxext"
|
"github.com/FloatTech/zbputils/ctxext"
|
||||||
"github.com/FloatTech/zbputils/img/pool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -73,20 +72,10 @@ func init() { // 插件主体
|
|||||||
for i := range illust.ImageUrls {
|
for i := range illust.ImageUrls {
|
||||||
f := file.BOTPATH + "/" + illust.Path(i)
|
f := file.BOTPATH + "/" + illust.Path(i)
|
||||||
n := name + "_p" + strconv.Itoa(i)
|
n := name + "_p" + strconv.Itoa(i)
|
||||||
var m *pool.Image
|
|
||||||
if file.IsNotExist(f) {
|
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]开始下载", n)
|
||||||
logrus.Debugln("[saucenao]urls:", illust.ImageUrls)
|
logrus.Debugln("[saucenao]urls:", illust.ImageUrls)
|
||||||
err1 := illust.DownloadToCache(i)
|
err1 := illust.DownloadToCache(i)
|
||||||
if err1 == nil {
|
|
||||||
m.SetFile(f)
|
|
||||||
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
|
||||||
}
|
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
logrus.Debugln("[saucenao]下载err:", err1)
|
logrus.Debugln("[saucenao]下载err:", err1)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -158,28 +157,16 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
|
|||||||
if len(illust.ImageUrls) == 0 {
|
if len(illust.ImageUrls) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
u := illust.ImageUrls[0]
|
|
||||||
n := u[strings.LastIndex(u, "/")+1 : len(u)-4]
|
|
||||||
m, err := imagepool.GetImage(n)
|
|
||||||
var msg message.MessageSegment
|
var msg message.MessageSegment
|
||||||
f := fileutil.BOTPATH + "/" + illust.Path(0)
|
f := fileutil.BOTPATH + "/" + illust.Path(0)
|
||||||
if err != nil {
|
if fileutil.IsNotExist(f) {
|
||||||
if fileutil.IsNotExist(f) {
|
// 下载图片
|
||||||
// 下载图片
|
if err := illust.DownloadToCache(0); err != nil {
|
||||||
if err := illust.DownloadToCache(0); err != nil {
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.SetFile(f)
|
|
||||||
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
|
||||||
msg = message.Image("file:///" + f)
|
|
||||||
} else {
|
|
||||||
msg = message.Image(m.String())
|
|
||||||
if ctxext.SendToSelf(ctx)(msg) == 0 {
|
|
||||||
msg = msg.Add("cache", "0")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
msg = message.Image("file:///" + f)
|
||||||
p.poolmu.Lock()
|
p.poolmu.Lock()
|
||||||
p.pool[imgtype] = append(p.pool[imgtype], &msg)
|
p.pool[imgtype] = append(p.pool[imgtype], &msg)
|
||||||
p.poolmu.Unlock()
|
p.poolmu.Unlock()
|
||||||
@ -229,9 +216,9 @@ func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error {
|
|||||||
if len(illust.ImageUrls) == 0 {
|
if len(illust.ImageUrls) == 0 {
|
||||||
return errors.New("nil image url")
|
return errors.New("nil image url")
|
||||||
}
|
}
|
||||||
err = imagepool.SendImageFromPool(strconv.FormatInt(illust.Pid, 10)+"_p0", illust.Path(0), func() error {
|
err = imagepool.SendImageFromPool(illust.Path(0), func(string) error {
|
||||||
return illust.DownloadToCache(0)
|
return illust.DownloadToCache(0)
|
||||||
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
|
}, ctxext.Send(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -148,18 +148,18 @@ func init() {
|
|||||||
imgname = name
|
imgname = name
|
||||||
}
|
}
|
||||||
imgpath := cache + "/" + imgname + ".png"
|
imgpath := cache + "/" + imgname + ".png"
|
||||||
err := pool.SendImageFromPool("pool"+imgname, imgpath, func() error {
|
err := pool.SendImageFromPool(imgpath, func(pth string) error {
|
||||||
data, err := web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
|
data, err := web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f, err := os.Create(imgpath)
|
f, err := os.Create(pth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
return os.WriteFile(f.Name(), data, 0755)
|
return os.WriteFile(f.Name(), data, 0755)
|
||||||
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
|
}, ctxext.Send(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
return
|
return
|
||||||
@ -190,9 +190,9 @@ func init() {
|
|||||||
var imgmsg message.MessageSegment
|
var imgmsg message.MessageSegment
|
||||||
var err error
|
var err error
|
||||||
if p == 1 {
|
if p == 1 {
|
||||||
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
|
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
|
||||||
} else {
|
} else {
|
||||||
imgmsg, err = poolimg(ctx, imgurl, name, cache)
|
imgmsg, err = poolimg(imgurl, name, cache)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
@ -213,7 +213,7 @@ func init() {
|
|||||||
if ok {
|
if ok {
|
||||||
imgurl := bed + info.ImgURL
|
imgurl := bed + info.ImgURL
|
||||||
var tarotmsg message.Message
|
var tarotmsg message.Message
|
||||||
imgmsg, err := poolimg(ctx, imgurl, match, cache)
|
imgmsg, err := poolimg(imgurl, match, cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
return
|
return
|
||||||
@ -284,9 +284,9 @@ func init() {
|
|||||||
var imgmsg message.MessageSegment
|
var imgmsg message.MessageSegment
|
||||||
var err error
|
var err error
|
||||||
if p == 1 {
|
if p == 1 {
|
||||||
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
|
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
|
||||||
} else {
|
} else {
|
||||||
imgmsg, err = poolimg(ctx, imgurl, name, cache)
|
imgmsg, err = poolimg(imgurl, name, cache)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
@ -319,17 +319,9 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func poolimg(ctx *zero.Ctx, imgurl, imgname, cache string) (msg message.MessageSegment, err error) {
|
func poolimg(imgurl, imgname, cache string) (msg message.MessageSegment, err error) {
|
||||||
imgfile := cache + "/" + imgname + ".png"
|
imgfile := cache + "/" + imgname + ".png"
|
||||||
aimgfile := file.BOTPATH + "/" + imgfile
|
aimgfile := file.BOTPATH + "/" + imgfile
|
||||||
m, err := pool.GetImage("pool" + imgname)
|
|
||||||
if err == nil {
|
|
||||||
msg = message.Image(m.String())
|
|
||||||
if ctxext.SendToSelf(ctx)(msg) == 0 {
|
|
||||||
msg = msg.Add("cache", "0")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if file.IsNotExist(aimgfile) {
|
if file.IsNotExist(aimgfile) {
|
||||||
var data []byte
|
var data []byte
|
||||||
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
|
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
|
||||||
@ -347,8 +339,6 @@ func poolimg(ctx *zero.Ctx, imgurl, imgname, cache string) (msg message.MessageS
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.SetFile(aimgfile)
|
|
||||||
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
|
||||||
msg = message.Image("file:///" + aimgfile)
|
msg = message.Image("file:///" + aimgfile)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user