🎨 ️ pixiv 图片缓存集中到 data/pixiv

This commit is contained in:
fumiama 2022-01-27 14:32:13 +08:00
parent aa71fa7cb8
commit e245a8f124
9 changed files with 42 additions and 64 deletions

2
data

@ -1 +1 @@
Subproject commit abdfd110625a51263944d7de45b6c9c050d876ff Subproject commit f84539a9f20934d99d94d62cfb9ad6e193df3617

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/FloatTech/ZeroBot-Plugin
go 1.17 go 1.17
require ( 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/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

4
go.sum
View File

@ -1,5 +1,5 @@
github.com/FloatTech/AnimeAPI v1.2.4-fix10 h1:+wZ/9V+VuQtai47jrIGH6MEZfp096123RiYA+J8Er3g= github.com/FloatTech/AnimeAPI v1.2.4-fix12 h1:MtLPKhoSl6DSiR+nz1W1RD2XP1j2iZTZ4MBwijvfURw=
github.com/FloatTech/AnimeAPI v1.2.4-fix10/go.mod h1:V+cIUIRxFsKniFqC2zfaT+2rhZQ0fIE0K+fWo+0qEpk= 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/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 h1:dEoOyYdCg05XhRivLU1CWNBzvzqv2hrJkv+eHXdOO0A=
github.com/FloatTech/zbputils v1.2.4-fix6/go.mod h1:ZXKT80QiMNZ2EP9Ga69hzjo3PV+NVrS9zZdJ9njNqWE= github.com/FloatTech/zbputils v1.2.4-fix6/go.mod h1:ZXKT80QiMNZ2EP9Ga69hzjo3PV+NVrS9zZdJ9njNqWE=

View File

@ -109,9 +109,9 @@ func init() { // 插件主体
u = apihead + dhash u = apihead + dhash
} }
_, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u) m, hassent, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), dhash, u)
if err != nil && err.Error() == "send image error" { if err == nil && !hassent {
ctx.SendChain(message.Image(u)) 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) m, hassent, err := imgpool.NewImage(send, ctxext.GetMessage(ctx), b14, u)
if err != nil && err.Error() == "send image error" && class <= 5 { if err == nil && !hassent {
ctx.SendChain(message.Image(u), message.Text(comment)) send(message.Message{message.Image(m.String())})
} }
} }

View File

@ -164,15 +164,17 @@ func init() {
} }
} }
m.SetFile(file.BOTPATH + "/" + cachefile) m.SetFile(file.BOTPATH + "/" + cachefile)
err = m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx)) hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx))
if err != nil && err.Error() == "send image error" { if err != nil {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + cachefile)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
} else { if hassent {
return
}
}
// 发送图片 // 发送图片
ctx.SendChain(message.Image(m.String())) ctx.SendChain(message.Image(m.String()))
}
}) })
} }

View File

@ -77,9 +77,9 @@ 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
_, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), u[strings.LastIndex(u, "/")+1:], u) m, hassent, err := imgpool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), u[strings.LastIndex(u, "/")+1:], u)
if err != nil && err.Error() == "send image error" { if err == nil && !hassent {
ctx.SendChain(message.Image(u)) ctx.SendChain(message.Image(m.String()))
} }
}) })
} }

View File

@ -60,7 +60,7 @@ func init() {
m, err := imgpool.GetImage(name) m, err := imgpool.GetImage(name)
if err != nil { if err != nil {
m.SetFile(url) m.SetFile(url)
err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) _, err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
process.SleepAbout1sTo2s() process.SleepAbout1sTo2s()
} }
if err == nil { if err == nil {

View File

@ -3,7 +3,6 @@ package saucenao
import ( import (
"fmt" "fmt"
"os"
"strconv" "strconv"
"github.com/FloatTech/AnimeAPI/ascii2d" "github.com/FloatTech/AnimeAPI/ascii2d"
@ -21,16 +20,7 @@ import (
"github.com/FloatTech/ZeroBot-Plugin/order" "github.com/FloatTech/ZeroBot-Plugin/order"
) )
var (
datapath = file.BOTPATH + "/data/saucenao/"
)
func init() { // 插件主体 func init() { // 插件主体
_ = os.RemoveAll(datapath)
err := os.MkdirAll(datapath, 0755)
if err != nil {
panic(err)
}
engine := control.Register("saucenao", order.PrioSauceNao, &control.Options{ engine := control.Register("saucenao", order.PrioSauceNao, &control.Options{
DisableOnDefault: false, DisableOnDefault: false,
Help: "搜图\n" + Help: "搜图\n" +
@ -51,10 +41,15 @@ func init() { // 插件主体
if illust.Pid > 0 { if illust.Pid > 0 {
name := strconv.FormatInt(illust.Pid, 10) name := strconv.FormatInt(illust.Pid, 10)
var imgs message.Message var imgs message.Message
for i, u := range illust.ImageUrls { for i := range illust.ImageUrls {
n := name + "_p" + strconv.Itoa(i) n := name + "_p" + strconv.Itoa(i)
filepath := datapath + n filepath := file.BOTPATH + "/" + pixiv.CacheDir + n
f := "" f := ""
m, err := imgpool.GetImage(n)
if err == nil {
imgs = append(imgs, message.Image(m.String()))
continue
}
switch { switch {
case file.IsExist(filepath + ".jpg"): case file.IsExist(filepath + ".jpg"):
f = filepath + ".jpg" f = filepath + ".jpg"
@ -64,16 +59,19 @@ func init() { // 插件主体
f = filepath + ".gif" f = filepath + ".gif"
default: default:
logrus.Debugln("[sausenao]开始下载", n) logrus.Debugln("[sausenao]开始下载", n)
filepath, err = pixiv.Download(u, datapath, n) filepath, err = illust.DownloadToCache(i, n)
if err == nil { if err == nil {
f = filepath f = filepath
} }
} }
if f != "" { 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 { if err == nil {
imgs = append(imgs, message.Image(m.String())) imgs = append(imgs, message.Image(m.String()))
if hassent {
process.SleepAbout1sTo2s() process.SleepAbout1sTo2s()
}
} else { } else {
logrus.Debugln("[saucenao]", err) logrus.Debugln("[saucenao]", err)
imgs = append(imgs, message.Image("file:///"+f)) imgs = append(imgs, message.Image("file:///"+f))

View File

@ -3,7 +3,6 @@ package setutime
import ( import (
"fmt" "fmt"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -41,19 +40,13 @@ type imgpool struct {
func newPools() *imgpool { func newPools() *imgpool {
cache := &imgpool{ cache := &imgpool{
DB: &sql.Sqlite{DBPath: "data/SetuTime/SetuTime.db"}, DB: &sql.Sqlite{DBPath: "data/SetuTime/SetuTime.db"},
Path: "data/SetuTime/cache/", Path: pixiv.CacheDir,
Group: 0, Group: 0,
List: []string{"涩图", "二次元", "风景", "车万"}, // 可以自己加类别,得自己加图片进数据库 List: []string{"涩图", "二次元", "风景", "车万"}, // 可以自己加类别,得自己加图片进数据库
Max: 10, Max: 10,
Pool: map[string][]*pixiv.Illust{}, Pool: map[string][]*pixiv.Illust{},
Form: 0, Form: 0,
} }
// 每次启动清理缓存
os.RemoveAll(cache.Path)
err := os.MkdirAll(cache.Path, 0755)
if err != nil {
panic(err)
}
// 如果数据库不存在则下载 // 如果数据库不存在则下载
_, _ = fileutil.GetLazyData(cache.DB.DBPath, false, false) _, _ = fileutil.GetLazyData(cache.DB.DBPath, false, false)
for i := range cache.List { for i := range cache.List {
@ -131,7 +124,7 @@ func init() { // 插件主体
return 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)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
@ -198,12 +191,13 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
m, err := imagepool.GetImage(n) m, err := imagepool.GetImage(n)
if err != nil { 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)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
m.SetFile(strings.ReplaceAll(n, "_p0", "")) m.SetFile(fileutil.BOTPATH + "/" + f)
_ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx)) _, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
} }
p.Lock.Lock() p.Lock.Lock()
p.Pool[imgtype] = append(p.Pool[imgtype], illust) p.Pool[imgtype] = append(p.Pool[imgtype], illust)
@ -228,7 +222,7 @@ func file(i *pixiv.Illust) string {
if err == nil { if err == nil {
return m.String() return m.String()
} }
filename := fmt.Sprint(i.Pid) filename := fmt.Sprint(i.Pid) + "_p0"
filepath := fileutil.BOTPATH + `/` + pool.Path + filename filepath := fileutil.BOTPATH + `/` + pool.Path + filename
if fileutil.IsExist(filepath + ".jpg") { if fileutil.IsExist(filepath + ".jpg") {
return `file:///` + filepath + ".jpg" return `file:///` + filepath + ".jpg"
@ -241,19 +235,3 @@ func file(i *pixiv.Illust) string {
} }
return "" 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
}