mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-24 18:51:02 +08:00
fix: 缓存池有时无法发出图片
This commit is contained in:
parent
7c1ddc6ab1
commit
f1c9e2331c
4
go.mod
4
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.20220219081142-278ad435f9f4
|
||||
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220219143823-2a02ca18b80f
|
||||
github.com/FloatTech/sqlite v0.2.0
|
||||
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220219081025-8f85202a3b0f
|
||||
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220219143652-6a7c76858a09
|
||||
github.com/antchfx/htmlquery v1.2.4
|
||||
github.com/corona10/goimagehash v1.0.3
|
||||
github.com/fogleman/gg v1.3.0
|
||||
|
||||
8
go.sum
8
go.sum
@ -1,10 +1,10 @@
|
||||
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220219081142-278ad435f9f4 h1:rYjVtKCsoDvmiRA93RDa2T8/jQdKa/I4SOb/pPVr1e8=
|
||||
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220219081142-278ad435f9f4/go.mod h1:b0DZdhMhqRQgjp8fPs+t0VUduR2XMLReankBsDRyBVg=
|
||||
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220219143823-2a02ca18b80f h1:e2SSjsD06q1eT1l+b9gLi9A+9g5B23TT/XuZbJrckec=
|
||||
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220219143823-2a02ca18b80f/go.mod h1:o0XA9RdADgR84fL43pvnBb1uwnq/b5Tw49LEnZZXY2E=
|
||||
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.20220219081025-8f85202a3b0f h1:wz08zPeX02LWawjj+tn5fbcXDr7RZB14BO1eFkYLXk4=
|
||||
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220219081025-8f85202a3b0f/go.mod h1:lNDjXvvNHlo4LHiAMyP/FVWcvw6kWB5onuQ024QvHfw=
|
||||
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220219143652-6a7c76858a09 h1:kv2t4YsK+jLuCL9LIAvoVffbCUjpmiQwWf8fMbYF2YI=
|
||||
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220219143652-6a7c76858a09/go.mod h1:lNDjXvvNHlo4LHiAMyP/FVWcvw6kWB5onuQ024QvHfw=
|
||||
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=
|
||||
|
||||
@ -173,7 +173,13 @@ func init() {
|
||||
}
|
||||
}
|
||||
// 发送图片
|
||||
ctx.SendChain(message.Image(m.String()))
|
||||
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("图片发送失败,可能被风控了~"))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -73,7 +73,14 @@ func init() {
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.SendChain(message.Image(m.String()))
|
||||
// 发送图片
|
||||
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("图片发送失败,可能被风控了~"))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -74,7 +74,14 @@ func init() {
|
||||
case <-time.After(time.Minute):
|
||||
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
|
||||
case url := <-queue:
|
||||
ctx.SendChain(message.Image(url))
|
||||
// 发送图片
|
||||
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("图片发送失败,可能被风控了~"))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -19,7 +19,6 @@ import (
|
||||
"github.com/FloatTech/zbputils/ctxext"
|
||||
"github.com/FloatTech/zbputils/file"
|
||||
"github.com/FloatTech/zbputils/img/pool"
|
||||
"github.com/FloatTech/zbputils/process"
|
||||
)
|
||||
|
||||
func init() { // 插件主体
|
||||
@ -47,11 +46,7 @@ func init() { // 插件主体
|
||||
n := name + "_p" + strconv.Itoa(i)
|
||||
filepath := file.BOTPATH + "/" + pixiv.CacheDir + n
|
||||
f := ""
|
||||
m, err := pool.GetImage(n)
|
||||
if err == nil {
|
||||
imgs = append(imgs, message.Image(m.String()))
|
||||
continue
|
||||
}
|
||||
var m *pool.Image
|
||||
switch {
|
||||
case file.IsExist(filepath + ".jpg"):
|
||||
f = filepath + ".jpg"
|
||||
@ -60,24 +55,21 @@ func init() { // 插件主体
|
||||
case file.IsExist(filepath + ".gif"):
|
||||
f = filepath + ".gif"
|
||||
default:
|
||||
m, err = pool.GetImage(n)
|
||||
if err == nil {
|
||||
imgs = append(imgs, message.Image(m.String()).Add("cache", "0"))
|
||||
continue
|
||||
}
|
||||
logrus.Debugln("[sausenao]开始下载", n)
|
||||
filepath, err = illust.DownloadToCache(i, n)
|
||||
if err == nil {
|
||||
f = file.BOTPATH + "/" + filepath
|
||||
m.SetFile(f)
|
||||
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
||||
}
|
||||
}
|
||||
if f != "" {
|
||||
m.SetFile(f)
|
||||
hassent, err := m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
||||
if err == nil {
|
||||
imgs = append(imgs, message.Image(m.String()))
|
||||
if hassent {
|
||||
process.SleepAbout1sTo2s()
|
||||
}
|
||||
} else {
|
||||
logrus.Debugln("[saucenao]", err)
|
||||
imgs = append(imgs, message.Image("file:///"+f))
|
||||
}
|
||||
imgs = append(imgs, message.Image("file:///"+f))
|
||||
}
|
||||
}
|
||||
txt := message.Text(
|
||||
|
||||
@ -25,15 +25,18 @@ import (
|
||||
|
||||
// Pools 图片缓冲池
|
||||
type imgpool struct {
|
||||
lock sync.Mutex
|
||||
db *sql.Sqlite
|
||||
path string
|
||||
max int
|
||||
pool map[string][]*pixiv.Illust
|
||||
db *sql.Sqlite
|
||||
dbmu sync.RWMutex
|
||||
path string
|
||||
max int
|
||||
pool map[string][]*message.MessageSegment
|
||||
poolmu sync.Mutex
|
||||
}
|
||||
|
||||
func (p *imgpool) List() (l []string) {
|
||||
var err error
|
||||
p.dbmu.RLock()
|
||||
defer p.dbmu.RUnlock()
|
||||
l, err = p.db.ListTables()
|
||||
if err != nil {
|
||||
l = []string{"涩图", "二次元", "风景", "车万"}
|
||||
@ -45,7 +48,7 @@ var pool = &imgpool{
|
||||
db: &sql.Sqlite{},
|
||||
path: pixiv.CacheDir,
|
||||
max: 10,
|
||||
pool: map[string][]*pixiv.Illust{},
|
||||
pool: make(map[string][]*message.MessageSegment),
|
||||
}
|
||||
|
||||
func init() { // 插件主体
|
||||
@ -90,7 +93,7 @@ func init() { // 插件主体
|
||||
}
|
||||
}
|
||||
// 从缓冲池里抽一张
|
||||
if id := ctx.SendChain(message.Image(pool.popfile(imgtype))); id.ID() == 0 {
|
||||
if id := ctx.SendChain(*pool.pop(imgtype)); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
||||
}
|
||||
})
|
||||
@ -127,6 +130,8 @@ func init() { // 插件主体
|
||||
engine.OnFullMatchGroup([]string{">setu status"}).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
state := []string{"[SetuTime]"}
|
||||
pool.dbmu.RLock()
|
||||
defer pool.dbmu.RUnlock()
|
||||
for _, imgtype := range pool.List() {
|
||||
num, err := pool.db.Count(imgtype)
|
||||
if err != nil {
|
||||
@ -150,6 +155,7 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
|
||||
u := illust.ImageUrls[0]
|
||||
n := u[strings.LastIndex(u, "/")+1 : len(u)-4]
|
||||
m, err := imagepool.GetImage(n)
|
||||
var msg message.MessageSegment
|
||||
if err != nil {
|
||||
// 下载图片
|
||||
f := ""
|
||||
@ -157,31 +163,33 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
m.SetFile(fileutil.BOTPATH + "/" + f)
|
||||
f = fileutil.BOTPATH + "/" + f
|
||||
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")
|
||||
}
|
||||
}
|
||||
p.lock.Lock()
|
||||
p.pool[imgtype] = append(p.pool[imgtype], illust)
|
||||
p.lock.Unlock()
|
||||
p.poolmu.Lock()
|
||||
p.pool[imgtype] = append(p.pool[imgtype], &msg)
|
||||
p.poolmu.Unlock()
|
||||
}
|
||||
|
||||
func (p *imgpool) pop(imgtype string) (illust *pixiv.Illust) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
func (p *imgpool) pop(imgtype string) (msg *message.MessageSegment) {
|
||||
p.poolmu.Lock()
|
||||
defer p.poolmu.Unlock()
|
||||
if p.size(imgtype) == 0 {
|
||||
return
|
||||
}
|
||||
illust = p.pool[imgtype][0]
|
||||
msg = p.pool[imgtype][0]
|
||||
p.pool[imgtype] = p.pool[imgtype][1:]
|
||||
return
|
||||
}
|
||||
|
||||
func (p *imgpool) file(i *pixiv.Illust) string {
|
||||
u := i.ImageUrls[0]
|
||||
m, err := imagepool.GetImage(u[strings.LastIndex(u, "/")+1 : len(u)-4])
|
||||
if err == nil {
|
||||
return m.String()
|
||||
}
|
||||
func (p *imgpool) cachefile(i *pixiv.Illust) string {
|
||||
filename := fmt.Sprint(i.Pid) + "_p0"
|
||||
filepath := fileutil.BOTPATH + `/` + p.path + filename
|
||||
if fileutil.IsExist(filepath + ".jpg") {
|
||||
@ -196,13 +204,11 @@ func (p *imgpool) file(i *pixiv.Illust) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (p *imgpool) popfile(imgtype string) string {
|
||||
return p.file(p.pop(imgtype))
|
||||
}
|
||||
|
||||
// fill 补充池子
|
||||
func (p *imgpool) fill(ctx *zero.Ctx, imgtype string) {
|
||||
times := math.Min(p.max-p.size(imgtype), 2)
|
||||
p.dbmu.RLock()
|
||||
defer p.dbmu.RUnlock()
|
||||
for i := 0; i < times; i++ {
|
||||
illust := &pixiv.Illust{}
|
||||
// 查询出一张图片
|
||||
@ -217,6 +223,8 @@ func (p *imgpool) fill(ctx *zero.Ctx, imgtype string) {
|
||||
}
|
||||
|
||||
func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error {
|
||||
p.dbmu.Lock()
|
||||
defer p.dbmu.Unlock()
|
||||
if err := p.db.Create(imgtype, &pixiv.Illust{}); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -231,7 +239,7 @@ func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error {
|
||||
return err
|
||||
}
|
||||
// 发送到发送者
|
||||
if id := ctx.SendChain(message.Image(p.file(illust))); id.ID() == 0 {
|
||||
if id := ctx.SendChain(message.Image(p.cachefile(illust))); id.ID() == 0 {
|
||||
return errors.New("可能被风控,发送失败")
|
||||
}
|
||||
// 添加插画到对应的数据库table
|
||||
@ -242,5 +250,7 @@ func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error {
|
||||
}
|
||||
|
||||
func (p *imgpool) remove(imgtype string, id int64) error {
|
||||
p.dbmu.Lock()
|
||||
defer p.dbmu.Unlock()
|
||||
return p.db.Del(imgtype, fmt.Sprintf("WHERE pid=%d", id))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user