Skip to content

Commit

Permalink
fix: 缓存池有时无法发出图片
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Feb 19, 2022
1 parent 7c1ddc6 commit f1c9e23
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 52 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
8 changes: 7 additions & 1 deletion plugin_fortune/fortune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("图片发送失败,可能被风控了~"))
}
}
})
}

Expand Down
9 changes: 8 additions & 1 deletion plugin_image_finder/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("图片发送失败,可能被风控了~"))
}
}
})
}

Expand Down
9 changes: 8 additions & 1 deletion plugin_lolicon/lolicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("图片发送失败,可能被风控了~"))
}
}
}
})
}
26 changes: 9 additions & 17 deletions plugin_saucenao/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() { // 插件主体
Expand Down Expand Up @@ -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"
Expand All @@ -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(
Expand Down
62 changes: 36 additions & 26 deletions plugin_setutime/setu_geter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{"涩图", "二次元", "风景", "车万"}
Expand All @@ -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() { // 插件主体
Expand Down Expand Up @@ -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: 可能被风控了"))
}
})
Expand Down Expand Up @@ -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 {
Expand All @@ -150,38 +155,41 @@ 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 := ""
if f, err = illust.DownloadToCache(0, n); err != nil {
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") {
Expand All @@ -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{}
// 查询出一张图片
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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))
}

0 comments on commit f1c9e23

Please sign in to comment.