From f1c9e2331c940eb7fdd4e612729068087498cf8f Mon Sep 17 00:00:00 2001 From: fumiama Date: Sat, 19 Feb 2022 23:41:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BC=93=E5=AD=98=E6=B1=A0=E6=9C=89?= =?UTF-8?q?=E6=97=B6=E6=97=A0=E6=B3=95=E5=8F=91=E5=87=BA=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 +-- go.sum | 8 ++--- plugin_fortune/fortune.go | 8 ++++- plugin_image_finder/keyword.go | 9 ++++- plugin_lolicon/lolicon.go | 9 ++++- plugin_saucenao/searcher.go | 26 +++++--------- plugin_setutime/setu_geter.go | 62 ++++++++++++++++++++-------------- 7 files changed, 74 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index 08ac06d8c5..e517962b75 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.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 diff --git a/go.sum b/go.sum index ade47d52e5..53c1cdf2d4 100644 --- a/go.sum +++ b/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= diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index 6f1b38953a..fb83efbb1c 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -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("图片发送失败,可能被风控了~")) + } + } }) } diff --git a/plugin_image_finder/keyword.go b/plugin_image_finder/keyword.go index 6a2df16503..6b8589490c 100644 --- a/plugin_image_finder/keyword.go +++ b/plugin_image_finder/keyword.go @@ -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("图片发送失败,可能被风控了~")) + } + } }) } diff --git a/plugin_lolicon/lolicon.go b/plugin_lolicon/lolicon.go index 16bfc98fd4..155982c0ed 100644 --- a/plugin_lolicon/lolicon.go +++ b/plugin_lolicon/lolicon.go @@ -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("图片发送失败,可能被风控了~")) + } + } } }) } diff --git a/plugin_saucenao/searcher.go b/plugin_saucenao/searcher.go index 62a76760cc..5a66995fe2 100644 --- a/plugin_saucenao/searcher.go +++ b/plugin_saucenao/searcher.go @@ -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( diff --git a/plugin_setutime/setu_geter.go b/plugin_setutime/setu_geter.go index a5f869934f..9ddd98b8a0 100644 --- a/plugin_setutime/setu_geter.go +++ b/plugin_setutime/setu_geter.go @@ -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)) }