From e245a8f1241c036d8d0feeb157a0f2708262655b Mon Sep 17 00:00:00 2001 From: fumiama Date: Thu, 27 Jan 2022 14:32:13 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=F0=9F=8E=A8=20=E2=9A=A1=EF=B8=8F?= =?UTF-8?q?=20pixiv=20=E5=9B=BE=E7=89=87=E7=BC=93=E5=AD=98=E9=9B=86?= =?UTF-8?q?=E4=B8=AD=E5=88=B0=20data/pixiv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data | 2 +- go.mod | 2 +- go.sum | 4 ++-- plugin_acgimage/classify.go | 12 ++++++------ plugin_fortune/fortune.go | 14 +++++++------ plugin_image_finder/keyword.go | 6 +++--- plugin_lolicon/lolicon.go | 2 +- plugin_saucenao/searcher.go | 28 ++++++++++++-------------- plugin_setutime/setu_geter.go | 36 +++++++--------------------------- 9 files changed, 42 insertions(+), 64 deletions(-) diff --git a/data b/data index abdfd110..f84539a9 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit abdfd110625a51263944d7de45b6c9c050d876ff +Subproject commit f84539a9f20934d99d94d62cfb9ad6e193df3617 diff --git a/go.mod b/go.mod index f79a7eab..1d02811c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.17 require ( - github.com/FloatTech/AnimeAPI v1.2.4-fix10 + github.com/FloatTech/AnimeAPI v1.2.4-fix12 github.com/FloatTech/zbputils v1.2.4-fix6 github.com/antchfx/htmlquery v1.2.4 github.com/corona10/goimagehash v1.0.3 diff --git a/go.sum b/go.sum index fa768ae4..e494822c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/FloatTech/AnimeAPI v1.2.4-fix10 h1:+wZ/9V+VuQtai47jrIGH6MEZfp096123RiYA+J8Er3g= -github.com/FloatTech/AnimeAPI v1.2.4-fix10/go.mod h1:V+cIUIRxFsKniFqC2zfaT+2rhZQ0fIE0K+fWo+0qEpk= +github.com/FloatTech/AnimeAPI v1.2.4-fix12 h1:MtLPKhoSl6DSiR+nz1W1RD2XP1j2iZTZ4MBwijvfURw= +github.com/FloatTech/AnimeAPI v1.2.4-fix12/go.mod h1:V+cIUIRxFsKniFqC2zfaT+2rhZQ0fIE0K+fWo+0qEpk= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/zbputils v1.2.4-fix6 h1:dEoOyYdCg05XhRivLU1CWNBzvzqv2hrJkv+eHXdOO0A= github.com/FloatTech/zbputils v1.2.4-fix6/go.mod h1:ZXKT80QiMNZ2EP9Ga69hzjo3PV+NVrS9zZdJ9njNqWE= diff --git a/plugin_acgimage/classify.go b/plugin_acgimage/classify.go index d96785d9..fc96f394 100644 --- a/plugin_acgimage/classify.go +++ b/plugin_acgimage/classify.go @@ -109,9 +109,9 @@ func init() { // 插件主体 u = apihead + dhash } - _, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u) - if err != nil && err.Error() == "send image error" { - ctx.SendChain(message.Image(u)) + m, hassent, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u) + if err == nil && !hassent { + ctx.SendChain(message.Image(m.String())) } } }) @@ -153,8 +153,8 @@ func replyClass(ctx *zero.Ctx, class int, dhash string, comment string, isupload } } - _, err = imgpool.NewImage(send, ctxext.GetMessage(ctx), b14, u) - if err != nil && err.Error() == "send image error" && class <= 5 { - ctx.SendChain(message.Image(u), message.Text(comment)) + m, hassent, err := imgpool.NewImage(send, ctxext.GetMessage(ctx), b14, u) + if err == nil && !hassent { + send(message.Message{message.Image(m.String())}) } } diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index ed6ad0cb..cddf14d8 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -164,15 +164,17 @@ func init() { } } m.SetFile(file.BOTPATH + "/" + cachefile) - err = m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx)) - if err != nil && err.Error() == "send image error" { - ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + cachefile)) + hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx)) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + if hassent { return } - } else { - // 发送图片 - ctx.SendChain(message.Image(m.String())) } + // 发送图片 + ctx.SendChain(message.Image(m.String())) }) } diff --git a/plugin_image_finder/keyword.go b/plugin_image_finder/keyword.go index 68f785ee..5f9afb15 100644 --- a/plugin_image_finder/keyword.go +++ b/plugin_image_finder/keyword.go @@ -77,9 +77,9 @@ func init() { rannum := randintn(len(soutujson.Illusts)) pom2 := soutujson.Illusts[rannum].ImageUrls.Medium[19:] u := pom1 + pom2 - _, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), u[strings.LastIndex(u, "/")+1:], u) - if err != nil && err.Error() == "send image error" { - ctx.SendChain(message.Image(u)) + m, hassent, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), u[strings.LastIndex(u, "/")+1:], u) + if err == nil && !hassent { + ctx.SendChain(message.Image(m.String())) } }) } diff --git a/plugin_lolicon/lolicon.go b/plugin_lolicon/lolicon.go index 4b40207d..fa043700 100644 --- a/plugin_lolicon/lolicon.go +++ b/plugin_lolicon/lolicon.go @@ -60,7 +60,7 @@ func init() { m, err := imgpool.GetImage(name) if err != nil { m.SetFile(url) - err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) + _, err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) process.SleepAbout1sTo2s() } if err == nil { diff --git a/plugin_saucenao/searcher.go b/plugin_saucenao/searcher.go index d33b2e03..12f19cdd 100644 --- a/plugin_saucenao/searcher.go +++ b/plugin_saucenao/searcher.go @@ -3,7 +3,6 @@ package saucenao import ( "fmt" - "os" "strconv" "github.com/FloatTech/AnimeAPI/ascii2d" @@ -21,16 +20,7 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/order" ) -var ( - datapath = file.BOTPATH + "/data/saucenao/" -) - func init() { // 插件主体 - _ = os.RemoveAll(datapath) - err := os.MkdirAll(datapath, 0755) - if err != nil { - panic(err) - } engine := control.Register("saucenao", order.PrioSauceNao, &control.Options{ DisableOnDefault: false, Help: "搜图\n" + @@ -51,10 +41,15 @@ func init() { // 插件主体 if illust.Pid > 0 { name := strconv.FormatInt(illust.Pid, 10) var imgs message.Message - for i, u := range illust.ImageUrls { + for i := range illust.ImageUrls { n := name + "_p" + strconv.Itoa(i) - filepath := datapath + n + filepath := file.BOTPATH + "/" + pixiv.CacheDir + n f := "" + m, err := imgpool.GetImage(n) + if err == nil { + imgs = append(imgs, message.Image(m.String())) + continue + } switch { case file.IsExist(filepath + ".jpg"): f = filepath + ".jpg" @@ -64,16 +59,19 @@ func init() { // 插件主体 f = filepath + ".gif" default: logrus.Debugln("[sausenao]开始下载", n) - filepath, err = pixiv.Download(u, datapath, n) + filepath, err = illust.DownloadToCache(i, n) if err == nil { f = filepath } } if f != "" { - m, err := imgpool.NewImage(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx), n, f) + m.SetFile(f) + hassent, err := m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) if err == nil { imgs = append(imgs, message.Image(m.String())) - process.SleepAbout1sTo2s() + if hassent { + process.SleepAbout1sTo2s() + } } else { logrus.Debugln("[saucenao]", err) imgs = append(imgs, message.Image("file:///"+f)) diff --git a/plugin_setutime/setu_geter.go b/plugin_setutime/setu_geter.go index f24ea2cc..2561d61d 100644 --- a/plugin_setutime/setu_geter.go +++ b/plugin_setutime/setu_geter.go @@ -3,7 +3,6 @@ package setutime import ( "fmt" - "os" "strconv" "strings" "sync" @@ -41,19 +40,13 @@ type imgpool struct { func newPools() *imgpool { cache := &imgpool{ DB: &sql.Sqlite{DBPath: "data/SetuTime/SetuTime.db"}, - Path: "data/SetuTime/cache/", + Path: pixiv.CacheDir, Group: 0, List: []string{"涩图", "二次元", "风景", "车万"}, // 可以自己加类别,得自己加图片进数据库 Max: 10, Pool: map[string][]*pixiv.Illust{}, Form: 0, } - // 每次启动清理缓存 - os.RemoveAll(cache.Path) - err := os.MkdirAll(cache.Path, 0755) - if err != nil { - panic(err) - } // 如果数据库不存在则下载 _, _ = fileutil.GetLazyData(cache.DB.DBPath, false, false) for i := range cache.List { @@ -131,7 +124,7 @@ func init() { // 插件主体 return } // 下载插画 - if err := download(illust, pool.Path); err != nil { + if _, err := illust.DownloadToCache(0, strconv.FormatInt(id, 10)+"_p0"); err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return } @@ -198,12 +191,13 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) { m, err := imagepool.GetImage(n) if err != nil { // 下载图片 - if err = download(illust, pool.Path); err != nil { + f := "" + if f, err = illust.DownloadToCache(0, n); err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return } - m.SetFile(strings.ReplaceAll(n, "_p0", "")) - _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) + m.SetFile(fileutil.BOTPATH + "/" + f) + _, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) } p.Lock.Lock() p.Pool[imgtype] = append(p.Pool[imgtype], illust) @@ -228,7 +222,7 @@ func file(i *pixiv.Illust) string { if err == nil { return m.String() } - filename := fmt.Sprint(i.Pid) + filename := fmt.Sprint(i.Pid) + "_p0" filepath := fileutil.BOTPATH + `/` + pool.Path + filename if fileutil.IsExist(filepath + ".jpg") { return `file:///` + filepath + ".jpg" @@ -241,19 +235,3 @@ func file(i *pixiv.Illust) string { } return "" } - -func download(i *pixiv.Illust, filedir string) error { - filename := fmt.Sprint(i.Pid) - filepath := filedir + filename - if fileutil.IsExist(filepath+".jpg") || fileutil.IsExist(filepath+".png") || fileutil.IsExist(filepath+".gif") { - return nil - } - // 下载最大分辨率为 1200 的图片 - link := i.ImageUrls[0] - link = strings.ReplaceAll(link, "img-original", "img-master") - link = strings.ReplaceAll(link, "_p0", "_p0_master1200") - link = strings.ReplaceAll(link, ".png", ".jpg") - // 下载 - _, err1 := pixiv.Download(link, filedir, filename) - return err1 -}