Skip to content

Commit

Permalink
✨ 🎨 ⚡️ pixiv 图片缓存集中到 data/pixiv
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Jan 27, 2022
1 parent aa71fa7 commit e245a8f
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 64 deletions.
2 changes: 1 addition & 1 deletion data
Submodule data updated 1 files
+1 −0 .gitignore
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
12 changes: 6 additions & 6 deletions plugin_acgimage/classify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
})
Expand Down Expand Up @@ -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())})
}
}
14 changes: 8 additions & 6 deletions plugin_fortune/fortune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
}

Expand Down
6 changes: 3 additions & 3 deletions plugin_image_finder/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion plugin_lolicon/lolicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 13 additions & 15 deletions plugin_saucenao/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package saucenao

import (
"fmt"
"os"
"strconv"

"github.com/FloatTech/AnimeAPI/ascii2d"
Expand All @@ -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" +
Expand All @@ -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"
Expand All @@ -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))
Expand Down
36 changes: 7 additions & 29 deletions plugin_setutime/setu_geter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package setutime

import (
"fmt"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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
}

0 comments on commit e245a8f

Please sign in to comment.