🔥 use new imgpool

This commit is contained in:
fumiama 2022-01-26 23:52:37 +08:00
parent e0317d1fe1
commit da5aab3ee6
9 changed files with 188 additions and 195 deletions

4
go.mod
View File

@ -3,8 +3,8 @@ module github.com/FloatTech/ZeroBot-Plugin
go 1.17 go 1.17
require ( require (
github.com/FloatTech/AnimeAPI v1.2.4-fix3 github.com/FloatTech/AnimeAPI v1.2.4-fix10
github.com/FloatTech/zbputils v1.2.4-fix2 github.com/FloatTech/zbputils v1.2.4-fix6
github.com/antchfx/htmlquery v1.2.4 github.com/antchfx/htmlquery v1.2.4
github.com/corona10/goimagehash v1.0.3 github.com/corona10/goimagehash v1.0.3
github.com/fogleman/gg v1.3.0 github.com/fogleman/gg v1.3.0

8
go.sum
View File

@ -1,8 +1,8 @@
github.com/FloatTech/AnimeAPI v1.2.4-fix3 h1:WlaEPjpNzmVgHriR7V5RDxRPNYX8gN1xqaJP9rQBul8= github.com/FloatTech/AnimeAPI v1.2.4-fix10 h1:+wZ/9V+VuQtai47jrIGH6MEZfp096123RiYA+J8Er3g=
github.com/FloatTech/AnimeAPI v1.2.4-fix3/go.mod h1:h4Re10eTkkhCVrrftL8PbJm6OHlVxdGSdjvXQ7/opoM= github.com/FloatTech/AnimeAPI v1.2.4-fix10/go.mod h1:V+cIUIRxFsKniFqC2zfaT+2rhZQ0fIE0K+fWo+0qEpk=
github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
github.com/FloatTech/zbputils v1.2.4-fix2 h1:CfiJ/owSgB4UOm4esGh0ZKxARW/Pw9zHrV0QLUwSX0k= github.com/FloatTech/zbputils v1.2.4-fix6 h1:dEoOyYdCg05XhRivLU1CWNBzvzqv2hrJkv+eHXdOO0A=
github.com/FloatTech/zbputils v1.2.4-fix2/go.mod h1:ZXKT80QiMNZ2EP9Ga69hzjo3PV+NVrS9zZdJ9njNqWE= github.com/FloatTech/zbputils v1.2.4-fix6/go.mod h1:ZXKT80QiMNZ2EP9Ga69hzjo3PV+NVrS9zZdJ9njNqWE=
github.com/Mrs4s/MiraiGo v0.0.0-20211120033824-43b23f4e6fcb h1:Rkj28fqIwGx/EgBzRYtpmJRfH6wqVn7cNdc7aJ0QE4M= github.com/Mrs4s/MiraiGo v0.0.0-20211120033824-43b23f4e6fcb h1:Rkj28fqIwGx/EgBzRYtpmJRfH6wqVn7cNdc7aJ0QE4M=
github.com/Mrs4s/MiraiGo v0.0.0-20211120033824-43b23f4e6fcb/go.mod h1:imVKbfKqqeit+C/eaWGb4MKQ3z3gN6pRpBU5RMtp5so= github.com/Mrs4s/MiraiGo v0.0.0-20211120033824-43b23f4e6fcb/go.mod h1:imVKbfKqqeit+C/eaWGb4MKQ3z3gN6pRpBU5RMtp5so=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=

View File

@ -71,8 +71,6 @@ import (
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_vtb_quotation" // vtb语录 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_vtb_quotation" // vtb语录
// 以下为内置依赖,勿动 // 以下为内置依赖,勿动
"github.com/FloatTech/AnimeAPI/imgpool"
"github.com/FloatTech/zbputils/control"
"github.com/fumiama/go-registry" "github.com/fumiama/go-registry"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
@ -93,7 +91,6 @@ var (
url *string url *string
adana *string adana *string
prefix *string prefix *string
poolkey *string
reg = registry.NewRegReader("reilia.fumiama.top:32664", "fumiama") reg = registry.NewRegReader("reilia.fumiama.top:32664", "fumiama")
) )
@ -112,7 +109,6 @@ func init() {
// 默认昵称 // 默认昵称
adana = flag.String("n", "椛椛", "Set default nickname.") adana = flag.String("n", "椛椛", "Set default nickname.")
prefix = flag.String("p", "/", "Set command prefix.") prefix = flag.String("p", "/", "Set command prefix.")
poolkey = flag.String("pk", "", "Set imgpool key and enable listening.")
flag.Parse() flag.Parse()
if *h { if *h {
@ -129,10 +125,6 @@ func init() {
} }
} }
if *poolkey != "" {
imgpool.RegisterListener(*poolkey, control.Register("imgpool", 1, &control.Options{}))
}
// 启用 gui // 启用 gui
// webctrl.InitGui(*g) // webctrl.InitGui(*g)
} }

View File

@ -8,15 +8,15 @@ import (
"github.com/FloatTech/AnimeAPI/classify" "github.com/FloatTech/AnimeAPI/classify"
"github.com/FloatTech/AnimeAPI/imgpool" "github.com/FloatTech/AnimeAPI/imgpool"
control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/web"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/order" "github.com/FloatTech/ZeroBot-Plugin/order"
control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/web"
) )
const ( const (
@ -54,7 +54,7 @@ func init() { // 插件主体
} }
}) })
// 有保护的随机图片 // 有保护的随机图片
engine.OnFullMatch("随机图片", zero.OnlyPublic).SetBlock(true). engine.OnFullMatch("随机图片", zero.OnlyGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
if limit.Load(ctx.Event.UserID).Acquire() { if limit.Load(ctx.Event.UserID).Acquire() {
class, dhash, comment, _ := classify.Classify(randapi, true) class, dhash, comment, _ := classify.Classify(randapi, true)
@ -110,15 +110,10 @@ func init() { // 插件主体
u = apihead + dhash u = apihead + dhash
} }
m, err := imgpool.NewImage(ctx, dhash, u) _, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u)
var img message.MessageSegment if err != nil && err.Error() == "send image error" {
if err != nil { ctx.SendChain(message.Image(u))
img = message.Image(u)
} else {
img = message.Image(m.String())
} }
ctx.SendChain(img)
} }
}) })
} }
@ -128,6 +123,11 @@ func setLastMsg(id int64, msg message.MessageID) {
} }
func replyClass(ctx *zero.Ctx, class int, dhash string, comment string, isupload bool) { func replyClass(ctx *zero.Ctx, class int, dhash string, comment string, isupload bool) {
if isupload {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(comment))
return
}
b14, err := url.QueryUnescape(dhash) b14, err := url.QueryUnescape(dhash)
if err != nil { if err != nil {
return return
@ -140,28 +140,27 @@ func replyClass(ctx *zero.Ctx, class int, dhash string, comment string, isupload
u = apihead + dhash u = apihead + dhash
} }
m, err := imgpool.NewImage(ctx, b14, u) var send ctxext.NoCtxSendMsg
var img message.MessageSegment if class > 5 {
if err != nil { send = ctxext.SendTo(ctx, ctx.Event.UserID)
img = message.Image(u)
} else { } else {
img = message.Image(m.String()) send = func(msg interface{}) int64 {
return ctx.Send(append(msg.(message.Message), message.Text(comment))).ID()
}
} }
if class > 5 { if class > 5 {
if dhash != "" && !isupload { if dhash != "" {
ctx.SendChain(message.Text(comment + "\n给你点提示哦" + b14)) ctx.SendChain(message.Text(comment + "\n给你点提示哦" + b14))
ctx.Event.GroupID = 0
ctx.Event.DetailType = "private"
ctx.SendChain(img)
return return
} }
ctx.SendChain(message.Text(comment)) ctx.SendChain(message.Text(comment))
return
}
if isupload {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(comment))
return
} }
_, err = imgpool.NewImage(send, ctxext.GetMessage(ctx), b14, u)
if err != nil && err.Error() == "send image error" {
logrus.Debugln("[acgimage]", err)
img := message.Image(u)
ctx.SendChain(img, message.Text(comment)) ctx.SendChain(img, message.Text(comment))
} }
}

View File

@ -21,9 +21,9 @@ import (
"github.com/FloatTech/AnimeAPI/imgpool" "github.com/FloatTech/AnimeAPI/imgpool"
control "github.com/FloatTech/zbputils/control" control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/math" "github.com/FloatTech/zbputils/math"
"github.com/FloatTech/zbputils/process"
"github.com/FloatTech/zbputils/txt2img" "github.com/FloatTech/zbputils/txt2img"
"github.com/FloatTech/ZeroBot-Plugin/order" "github.com/FloatTech/ZeroBot-Plugin/order"
@ -147,7 +147,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[:])
m, err := imgpool.GetImage(ctx, cachefile) m, err := imgpool.GetImage(cachefile)
if err != nil { if err != nil {
logrus.Debugln("[fortune]", err) logrus.Debugln("[fortune]", err)
if file.IsNotExist(cachefile) { if file.IsNotExist(cachefile) {
@ -163,21 +163,16 @@ func init() {
return return
} }
} }
m, err = imgpool.NewImage(ctx, cachefile, file.BOTPATH+"/"+cachefile) m.SetFile(file.BOTPATH + "/" + cachefile)
process.SleepAbout1sTo2s() // 防止风控 err = m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx))
if err != nil { if err != nil && err.Error() == "send image error" {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + cachefile)) ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + cachefile))
return return
} }
} } else {
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
// 发送图片 // 发送图片
ctx.SendChain(message.Image(m.String())) ctx.SendChain(message.Image(m.String()))
}
}) })
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/FloatTech/AnimeAPI/imgpool" "github.com/FloatTech/AnimeAPI/imgpool"
control "github.com/FloatTech/zbputils/control" control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
@ -76,12 +77,10 @@ func init() {
rannum := randintn(len(soutujson.Illusts)) rannum := randintn(len(soutujson.Illusts))
pom2 := soutujson.Illusts[rannum].ImageUrls.Medium[19:] pom2 := soutujson.Illusts[rannum].ImageUrls.Medium[19:]
u := pom1 + pom2 u := pom1 + pom2
m, err := imgpool.NewImage(ctx, u[strings.LastIndex(u, "/")+1:], u) _, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), u[strings.LastIndex(u, "/")+1:], u)
if err != nil { if err != nil && err.Error() == "send image error" {
ctx.SendChain(message.Image(u)) ctx.SendChain(message.Image(u))
return
} }
ctx.SendChain(message.Image(m.String()))
}) })
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/FloatTech/AnimeAPI/imgpool" "github.com/FloatTech/AnimeAPI/imgpool"
control "github.com/FloatTech/zbputils/control" control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/math" "github.com/FloatTech/zbputils/math"
"github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/process"
@ -56,9 +57,10 @@ func init() {
url := json.Get("data.0.urls.original").Str url := json.Get("data.0.urls.original").Str
url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re") url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re")
name := url[strings.LastIndex(url, "/")+1 : len(url)-4] name := url[strings.LastIndex(url, "/")+1 : len(url)-4]
m, err := imgpool.GetImage(ctx, name) m, err := imgpool.GetImage(name)
if err != nil { if err != nil {
m, err = imgpool.NewImage(ctx, name, url) m.SetFile(url)
err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
process.SleepAbout1sTo2s() process.SleepAbout1sTo2s()
} }
if err == nil { if err == nil {
@ -69,7 +71,7 @@ func init() {
} }
}() }()
select { select {
case <-time.After(time.Second * 10): case <-time.After(time.Minute):
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......")) ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
case url := <-queue: case url := <-queue:
ctx.SendChain(message.Image(url)) ctx.SendChain(message.Image(url))

View File

@ -10,15 +10,15 @@ import (
"github.com/FloatTech/AnimeAPI/imgpool" "github.com/FloatTech/AnimeAPI/imgpool"
"github.com/FloatTech/AnimeAPI/pixiv" "github.com/FloatTech/AnimeAPI/pixiv"
"github.com/FloatTech/AnimeAPI/saucenao" "github.com/FloatTech/AnimeAPI/saucenao"
control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/process"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/order" "github.com/FloatTech/ZeroBot-Plugin/order"
control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/file"
) )
var ( var (
@ -70,10 +70,12 @@ func init() { // 插件主体
} }
} }
if f != "" { if f != "" {
m, err := imgpool.NewImage(ctx, n, f) m, err := imgpool.NewImage(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx), n, f)
if err == nil { if err == nil {
imgs = append(imgs, message.Image(m.String())) imgs = append(imgs, message.Image(m.String()))
process.SleepAbout1sTo2s()
} else { } else {
logrus.Debugln("[saucenao]", err)
imgs = append(imgs, message.Image("file:///"+f)) imgs = append(imgs, message.Image("file:///"+f))
} }
} }

View File

@ -9,19 +9,20 @@ import (
"sync" "sync"
"time" "time"
imagepool "github.com/FloatTech/AnimeAPI/imgpool"
"github.com/FloatTech/AnimeAPI/pixiv" "github.com/FloatTech/AnimeAPI/pixiv"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/order"
control "github.com/FloatTech/zbputils/control" control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
fileutil "github.com/FloatTech/zbputils/file" fileutil "github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/math" "github.com/FloatTech/zbputils/math"
"github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/process"
"github.com/FloatTech/zbputils/rule" "github.com/FloatTech/zbputils/rule"
"github.com/FloatTech/zbputils/sql" "github.com/FloatTech/zbputils/sql"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/order"
) )
// Pools 图片缓冲池 // Pools 图片缓冲池
@ -77,7 +78,6 @@ func init() { // 插件主体
"- 删除[涩图/二次元/风景/车万][P站图片ID]\n" + "- 删除[涩图/二次元/风景/车万][P站图片ID]\n" +
"- >setu status", "- >setu status",
}) })
go func() {
process.SleepAbout1sTo2s() process.SleepAbout1sTo2s()
pool = newPools() pool = newPools()
engine.OnRegex(`^来份(.*)$`, rule.FirstValueInList(pool.List)).SetBlock(true). engine.OnRegex(`^来份(.*)$`, rule.FirstValueInList(pool.List)).SetBlock(true).
@ -97,21 +97,15 @@ func init() { // 插件主体
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
continue continue
} }
// 下载图片
if err := download(illust, pool.Path); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
continue
}
ctx.SendGroupMessage(pool.Group, []message.MessageSegment{message.Image(file(illust))})
// 向缓冲池添加一张图片 // 向缓冲池添加一张图片
pool.push(imgtype, illust) pool.push(ctx, imgtype, illust)
time.Sleep(time.Second * 1) process.SleepAbout1sTo2s()
} }
}() }()
// 如果没有缓存阻塞5 // 如果没有缓存阻塞10秒
if pool.size(imgtype) == 0 { if pool.size(imgtype) == 0 {
ctx.SendChain(message.Text("INFO: 正在填充弹药......")) ctx.SendChain(message.Text("INFO: 正在填充弹药......"))
<-time.After(time.Second * 5) time.Sleep(time.Second * 10)
if pool.size(imgtype) == 0 { if pool.size(imgtype) == 0 {
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......")) ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
return return
@ -184,7 +178,6 @@ func init() { // 插件主体
} }
ctx.SendChain(message.Text(state)) ctx.SendChain(message.Text(state))
}) })
}()
} }
// size 返回缓冲池指定类型的现有大小 // size 返回缓冲池指定类型的现有大小
@ -199,10 +192,22 @@ func (p *imgpool) isFull(imgtype string) bool {
}*/ }*/
// push 向缓冲池插入一张图片 // push 向缓冲池插入一张图片
func (p *imgpool) push(imgtype string, illust *pixiv.Illust) { 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)
if err != nil {
// 下载图片
if err = download(illust, pool.Path); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
m.SetFile(strings.ReplaceAll(n, "_p0", ""))
_ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
}
p.Lock.Lock() p.Lock.Lock()
defer p.Lock.Unlock()
p.Pool[imgtype] = append(p.Pool[imgtype], illust) p.Pool[imgtype] = append(p.Pool[imgtype], illust)
p.Lock.Unlock()
} }
// Push 在缓冲池拿出一张图片 // Push 在缓冲池拿出一张图片
@ -218,31 +223,30 @@ func (p *imgpool) pop(imgtype string) (illust *pixiv.Illust) {
} }
func file(i *pixiv.Illust) string { func 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()
}
filename := fmt.Sprint(i.Pid) filename := fmt.Sprint(i.Pid)
filepath := fileutil.BOTPATH + `/` + pool.Path + filename filepath := fileutil.BOTPATH + `/` + pool.Path + filename
if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) { if fileutil.IsExist(filepath + ".jpg") {
return `file:///` + filepath + ".jpg" return `file:///` + filepath + ".jpg"
} }
if _, err := os.Stat(filepath + ".png"); err == nil || os.IsExist(err) { if fileutil.IsExist(filepath + ".png") {
return `file:///` + filepath + ".png" return `file:///` + filepath + ".png"
} }
if _, err := os.Stat(filepath + ".gif"); err == nil || os.IsExist(err) { if fileutil.IsExist(filepath + ".gif") {
return `file:///` + filepath + ".gif" return `file:///` + filepath + ".gif"
} }
return "" return ""
} }
func download(i *pixiv.Illust, filedir string) /*(string, */ error /*)*/ { func download(i *pixiv.Illust, filedir string) error {
filename := fmt.Sprint(i.Pid) filename := fmt.Sprint(i.Pid)
filepath := filedir + filename filepath := filedir + filename
if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) { if fileutil.IsExist(filepath+".jpg") || fileutil.IsExist(filepath+".png") || fileutil.IsExist(filepath+".gif") {
return /*filepath + ".jpg",*/ nil return nil
}
if _, err := os.Stat(filepath + ".png"); err == nil || os.IsExist(err) {
return /*filepath + ".png",*/ nil
}
if _, err := os.Stat(filepath + ".gif"); err == nil || os.IsExist(err) {
return /*filepath + ".gif",*/ nil
} }
// 下载最大分辨率为 1200 的图片 // 下载最大分辨率为 1200 的图片
link := i.ImageUrls[0] link := i.ImageUrls[0]