🔥 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=

22
main.go
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"
@ -87,14 +85,13 @@ var (
"* Copyright © 2020 - 2021 FloatTech. All Rights Reserved.", "* Copyright © 2020 - 2021 FloatTech. All Rights Reserved.",
"* Project: https://github.com/FloatTech/ZeroBot-Plugin", "* Project: https://github.com/FloatTech/ZeroBot-Plugin",
} }
nicks = []string{"ATRI", "atri", "亚托莉", "アトリ"} nicks = []string{"ATRI", "atri", "亚托莉", "アトリ"}
banner = strings.Join(contents, "\n") banner = strings.Join(contents, "\n")
token *string token *string
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")
) )
func init() { func init() {
@ -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)) _, err = imgpool.NewImage(send, ctxext.GetMessage(ctx), b14, u)
return 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 {
// 发送图片
ctx.SendChain(message.Image(m.String()))
} }
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
// 发送图片
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,114 +78,106 @@ 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). Handle(func(ctx *zero.Ctx) {
Handle(func(ctx *zero.Ctx) { if !limit.Load(ctx.Event.UserID).Acquire() {
if !limit.Load(ctx.Event.UserID).Acquire() { ctx.SendChain(message.Text("请稍后重试0x0..."))
ctx.SendChain(message.Text("请稍后重试0x0...")) return
return }
} var imgtype = ctx.State["regex_matched"].([]string)[1]
var imgtype = ctx.State["regex_matched"].([]string)[1] // 补充池子
// 补充池子 go func() {
go func() { times := math.Min(pool.Max-pool.size(imgtype), 2)
times := math.Min(pool.Max-pool.size(imgtype), 2) for i := 0; i < times; i++ {
for i := 0; i < times; i++ { illust := &pixiv.Illust{}
illust := &pixiv.Illust{} // 查询出一张图片
// 查询出一张图片 if err := pool.DB.Pick(imgtype, illust); err != nil {
if err := pool.DB.Pick(imgtype, illust); err != nil { 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)
time.Sleep(time.Second * 1)
} }
}() // 向缓冲池添加一张图片
// 如果没有缓存阻塞5秒 pool.push(ctx, imgtype, illust)
process.SleepAbout1sTo2s()
}
}()
// 如果没有缓存阻塞10秒
if pool.size(imgtype) == 0 {
ctx.SendChain(message.Text("INFO: 正在填充弹药......"))
time.Sleep(time.Second * 10)
if pool.size(imgtype) == 0 { if pool.size(imgtype) == 0 {
ctx.SendChain(message.Text("INFO: 正在填充弹药......")) ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
<-time.After(time.Second * 5) return
if pool.size(imgtype) == 0 {
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
return
}
} }
// 从缓冲池里抽一张 }
if id := ctx.SendChain(message.Image(file(pool.pop(imgtype)))); id.ID() == 0 { // 从缓冲池里抽一张
ctx.SendChain(message.Text("ERROR: 可能被风控了")) if id := ctx.SendChain(message.Image(file(pool.pop(imgtype)))); id.ID() == 0 {
} ctx.SendChain(message.Text("ERROR: 可能被风控了"))
}) }
})
engine.OnRegex(`^添加(.*?)(\d+)$`, rule.FirstValueInList(pool.List), zero.SuperUserPermission).SetBlock(true). engine.OnRegex(`^添加(.*?)(\d+)$`, rule.FirstValueInList(pool.List), zero.SuperUserPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
var ( var (
imgtype = ctx.State["regex_matched"].([]string)[1] imgtype = ctx.State["regex_matched"].([]string)[1]
id, _ = strconv.ParseInt(ctx.State["regex_matched"].([]string)[2], 10, 64) id, _ = strconv.ParseInt(ctx.State["regex_matched"].([]string)[2], 10, 64)
) )
ctx.SendChain(message.Text("少女祈祷中......")) ctx.SendChain(message.Text("少女祈祷中......"))
// 查询P站插图信息 // 查询P站插图信息
illust, err := pixiv.Works(id) illust, err := pixiv.Works(id)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
// 下载插画
if err := download(illust, pool.Path); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
// 发送到发送者
if id := ctx.SendChain(message.Image(file(illust))); id.ID() == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控,发送失败"))
return
}
// 添加插画到对应的数据库table
if err := pool.DB.Insert(imgtype, illust); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("添加成功"))
})
engine.OnRegex(`^删除(.*?)(\d+)$`, rule.FirstValueInList(pool.List), zero.SuperUserPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var (
imgtype = ctx.State["regex_matched"].([]string)[1]
id, _ = strconv.ParseInt(ctx.State["regex_matched"].([]string)[2], 10, 64)
)
// 查询数据库
if err := pool.DB.Del(imgtype, fmt.Sprintf("WHERE pid=%d", id)); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("删除成功"))
})
// 查询数据库涩图数量
engine.OnFullMatchGroup([]string{">setu status"}).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
state := []string{"[SetuTime]"}
for i := range pool.List {
num, err := pool.DB.Count(pool.List[i])
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) num = 0
return
} }
// 下载插画 state = append(state, "\n")
if err := download(illust, pool.Path); err != nil { state = append(state, pool.List[i])
ctx.SendChain(message.Text("ERROR: ", err)) state = append(state, ": ")
return state = append(state, fmt.Sprintf("%d", num))
} }
// 发送到发送者 ctx.SendChain(message.Text(state))
if id := ctx.SendChain(message.Image(file(illust))); id.ID() == 0 { })
ctx.SendChain(message.Text("ERROR: 可能被风控,发送失败"))
return
}
// 添加插画到对应的数据库table
if err := pool.DB.Insert(imgtype, illust); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("添加成功"))
})
engine.OnRegex(`^删除(.*?)(\d+)$`, rule.FirstValueInList(pool.List), zero.SuperUserPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var (
imgtype = ctx.State["regex_matched"].([]string)[1]
id, _ = strconv.ParseInt(ctx.State["regex_matched"].([]string)[2], 10, 64)
)
// 查询数据库
if err := pool.DB.Del(imgtype, fmt.Sprintf("WHERE pid=%d", id)); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("删除成功"))
})
// 查询数据库涩图数量
engine.OnFullMatchGroup([]string{">setu status"}).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
state := []string{"[SetuTime]"}
for i := range pool.List {
num, err := pool.DB.Count(pool.List[i])
if err != nil {
num = 0
}
state = append(state, "\n")
state = append(state, pool.List[i])
state = append(state, ": ")
state = append(state, fmt.Sprintf("%d", num))
}
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]