diff --git a/go.mod b/go.mod index a8306c11..ebe7f1e1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.16 require ( - github.com/FloatTech/AnimeAPI v1.1.9 + github.com/FloatTech/AnimeAPI v1.1.10 github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4 github.com/FloatTech/ZeroBot-Plugin-Timer v1.4.3 github.com/FloatTech/bot-manager v1.0.0 diff --git a/go.sum b/go.sum index ea1091f8..e13eeaf2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ -github.com/FloatTech/AnimeAPI v1.1.9 h1:H1hZmgwZPNHdx39K9JvY3awT8TTsCl9kKA1uVMyCjRg= github.com/FloatTech/AnimeAPI v1.1.9/go.mod h1:CC+vF30UGBlcIUxwFOcXIEHoJ4r7c5x2iLQsnUCVdDI= +github.com/FloatTech/AnimeAPI v1.1.10 h1:fYkv65P1HBukHi3GpzYulGx/xLshEL9635QXALDryf0= +github.com/FloatTech/AnimeAPI v1.1.10/go.mod h1:CC+vF30UGBlcIUxwFOcXIEHoJ4r7c5x2iLQsnUCVdDI= github.com/FloatTech/ZeroBot-Plugin v1.1.5/go.mod h1:kWuUARvU7gs4xLggi8Sy37ja2GRL6k0X6kewe5TiZRs= github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4 h1:WW0BmmLLqAg+m6qGkrKbsfSIm91fkj3/udt3R7Myodo= github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4/go.mod h1:W7ag6hml1pZTNzRXKU74OMr6rS8awQKSU+o2g7Gj4O0= diff --git a/plugin_diana/data/text.go b/plugin_diana/data/text.go index a36b797a..82ec18da 100644 --- a/plugin_diana/data/text.go +++ b/plugin_diana/data/text.go @@ -3,6 +3,7 @@ package data import ( "crypto/md5" + "errors" "io" "net/http" "os" @@ -11,6 +12,8 @@ import ( log "github.com/sirupsen/logrus" "github.com/wdvxdr1123/ZeroBot/utils/helper" + + "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) const ( @@ -53,7 +56,7 @@ func init() { // LoadText 加载小作文 func LoadText() error { - if _, err := os.Stat(pbfile); err == nil || os.IsExist(err) { + if file.IsExist(pbfile) { f, err := os.Open(pbfile) if err == nil { defer f.Close() @@ -116,7 +119,7 @@ func isin(sum *[16]byte) bool { func savecompo() error { data, err := compo.Marshal() if err == nil { - if _, err := os.Stat(datapath); err == nil || os.IsExist(err) { + if file.IsExist(datapath) { f, err1 := os.OpenFile(pbfile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err1 == nil { _, err2 := f.Write(data) @@ -125,6 +128,7 @@ func savecompo() error { } return err1 } + return errors.New("datapath is not exist") } return err } diff --git a/plugin_fortune/data.go b/plugin_fortune/data.go index 5b68078c..bc703016 100644 --- a/plugin_fortune/data.go +++ b/plugin_fortune/data.go @@ -1,9 +1,12 @@ package fortune import ( + "errors" io "io" "os" "sync" + + "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) var ( @@ -13,7 +16,7 @@ var ( func loadcfg(name string) error { name = base + name - if _, err := os.Stat(name); err == nil || os.IsExist(err) { + if file.IsExist(name) { f, err := os.Open(name) if err == nil { defer f.Close() @@ -35,7 +38,7 @@ func savecfg(name string) error { name = base + name data, err := conf.Marshal() if err == nil { - if _, err := os.Stat(base); err == nil || os.IsExist(err) { + if file.IsExist(base) { f, err1 := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err1 == nil { mu.Lock() @@ -46,6 +49,7 @@ func savecfg(name string) error { } return err1 } + return errors.New("base dir is not exist") } return err } diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index ecfc6557..c9c2a2a5 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -21,7 +21,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/utils/helper" "github.com/FloatTech/ZeroBot-Plugin/control" - "github.com/FloatTech/ZeroBot-Plugin/utils/dl" + "github.com/FloatTech/ZeroBot-Plugin/utils/file" "github.com/FloatTech/ZeroBot-Plugin/utils/math" ) @@ -76,9 +76,9 @@ func init() { Handle(func(ctx *zero.Ctx) { // 检查签文文件是否存在 mikuji := base + "运势签文.json" - if _, err := os.Stat(mikuji); err != nil && !os.IsExist(err) { + if file.IsNotExist(mikuji) { ctx.SendChain(message.Text("正在下载签文文件,请稍后...")) - err := dl.DownloadTo(site+"运势签文.json", mikuji) + err := file.DownloadTo(site+"运势签文.json", mikuji) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return @@ -87,9 +87,9 @@ func init() { } // 检查字体文件是否存在 ttf := base + "sakura.ttf" - if _, err := os.Stat(ttf); err != nil && !os.IsExist(err) { + if file.IsNotExist(ttf) { ctx.SendChain(message.Text("正在下载字体文件,请稍后...")) - err := dl.DownloadTo(site+"sakura.ttf", ttf) + err := file.DownloadTo(site+"sakura.ttf", ttf) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return @@ -109,11 +109,11 @@ func init() { } // 检查背景图片是否存在 folder := base + kind - if _, err := os.Stat(folder); err != nil && !os.IsExist(err) { + if file.IsNotExist(folder) { ctx.SendChain(message.Text("正在下载背景图片,请稍后...")) zipfile := kind + ".zip" zipcache := base + zipfile - err := dl.DownloadTo(site+zipfile, zipcache) + err := file.DownloadTo(site+zipfile, zipcache) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return @@ -160,7 +160,7 @@ func init() { // @return 错误信息 func unpack(tgt, dest string) error { // 路径目录不存在则创建目录 - if _, err := os.Stat(dest); err != nil && !os.IsExist(err) { + if file.IsNotExist(dest) { if err := os.MkdirAll(dest, 0755); err != nil { panic(err) } diff --git a/plugin_hs/run.go b/plugin_hs/run.go index 5baae174..9b82cd1b 100644 --- a/plugin_hs/run.go +++ b/plugin_hs/run.go @@ -14,6 +14,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) var botpath, _ = os.Getwd() @@ -53,7 +54,7 @@ func init() { cid := gjson.Get(g, `list.`+strconv.Itoa(i)+`.CardID`).String() cachefile := cachedir + cid imgcq := `[CQ:image,file=` + "file:///" + cachefile + `]` - if _, err := os.Stat(cachefile); err != nil { + if file.IsNotExist(cachefile) { im, err := req.Get(`https://res.fbigame.com/hs/v13/`+cid+ `.png?auth_key=`+gjson.Get(g, `list.`+strconv.Itoa(i)+`.auth_key`).String(), header, diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index 0eb5d92f..937b42e2 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -17,6 +17,7 @@ import ( timer "github.com/FloatTech/ZeroBot-Plugin-Timer" + "github.com/FloatTech/ZeroBot-Plugin/utils/file" "github.com/FloatTech/ZeroBot-Plugin/utils/math" ) @@ -427,7 +428,7 @@ func strToInt(str string) int64 { func loadConfig() { mkdirerr := os.MkdirAll(datapath, 0755) if mkdirerr == nil { - if _, err := os.Stat(confile); err == nil || os.IsExist(err) { + if file.IsExist(confile) { f, err := os.Open(confile) if err == nil { data, err1 := io.ReadAll(f) @@ -452,7 +453,7 @@ func saveConfig() error { data, err := config.Marshal() if err != nil { return err - } else if _, err := os.Stat(datapath); err == nil || os.IsExist(err) { + } else if file.IsExist(datapath) { f, err1 := os.OpenFile(confile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err1 != nil { return err1 diff --git a/plugin_reborn/load.go b/plugin_reborn/load.go index ffe24193..30545c4f 100644 --- a/plugin_reborn/load.go +++ b/plugin_reborn/load.go @@ -7,6 +7,7 @@ import ( "os" "time" + "github.com/FloatTech/ZeroBot-Plugin/utils/file" wr "github.com/mroth/weightedrand" log "github.com/sirupsen/logrus" ) @@ -53,7 +54,7 @@ func init() { // load 加载rate数据 func load(area *rate) error { - if _, err := os.Stat(jsonfile); err == nil || os.IsExist(err) { + if file.IsExist(jsonfile) { f, err := os.Open(jsonfile) if err == nil { defer f.Close() diff --git a/plugin_saucenao/searcher.go b/plugin_saucenao/searcher.go index 42178153..e90662fb 100644 --- a/plugin_saucenao/searcher.go +++ b/plugin_saucenao/searcher.go @@ -3,8 +3,8 @@ package saucenao import ( "fmt" + "os" "strconv" - "strings" "github.com/FloatTech/AnimeAPI/ascii2d" "github.com/FloatTech/AnimeAPI/picture" @@ -14,9 +14,20 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/file" +) + +var ( + botpath, _ = os.Getwd() + datapath = botpath + "/data/saucenao/" ) func init() { // 插件主体 + _ = os.RemoveAll(datapath) + err := os.MkdirAll(datapath, 0755) + if err != nil { + panic(err) + } engine := control.Register("saucenao", &control.Options{ DisableOnDefault: false, Help: "搜图\n" + @@ -35,21 +46,38 @@ func init() { // 插件主体 return } if illust.Pid > 0 { - // 改用 i.pixiv.cat 镜像站 - link := illust.ImageUrls - link = strings.ReplaceAll(link, "i.pximg.net", "i.pixiv.cat") - // 发送搜索结果 - ctx.SendChain( - message.Image(link), - message.Text( - "\n", - "标题:", illust.Title, "\n", - "插画ID:", illust.Pid, "\n", - "画师:", illust.UserName, "\n", - "画师ID:", illust.UserId, "\n", - "直链:", "https://pixivel.moe/detail?id=", illust.Pid, - ), + name := strconv.FormatInt(illust.Pid, 10) + filepath := datapath + name + switch { + case file.IsExist(filepath + ".jpg"): + filepath += ".jpg" + case file.IsExist(filepath + ".png"): + filepath += ".png" + case file.IsExist(filepath + ".gif"): + filepath += ".gif" + default: + filepath = "" + } + if filepath == "" { + filepath, err = pixiv.Download(illust.ImageUrls, datapath, name) + if err == nil { + filepath = "file:///" + filepath + } + } + txt := message.Text( + "\n", + "标题:", illust.Title, "\n", + "插画ID:", illust.Pid, "\n", + "画师:", illust.UserName, "\n", + "画师ID:", illust.UserId, "\n", + "直链:", "https://pixivel.moe/detail?id=", illust.Pid, ) + if filepath != "" { + // 发送搜索结果 + ctx.SendChain(message.Image(filepath), txt) + } + // 图片下载失败,仅发送文字结果 + ctx.SendChain(txt) } else { ctx.SendChain(message.Text("图片不存在!")) } diff --git a/utils/dl/dl.go b/utils/dl/dl.go deleted file mode 100644 index 414e5ef1..00000000 --- a/utils/dl/dl.go +++ /dev/null @@ -1,23 +0,0 @@ -// Package dl 下载实用工具 -package dl - -import ( - "io" - "net/http" - "os" -) - -// DownloadTo 下载到路径 -func DownloadTo(url, file string) error { - resp, err := http.Get(url) - if err == nil { - var f *os.File - f, err = os.Create(file) - if err == nil { - _, err = io.Copy(f, resp.Body) - resp.Body.Close() - f.Close() - } - } - return err -} diff --git a/utils/file/dl.go b/utils/file/dl.go new file mode 100644 index 00000000..8b204d1e --- /dev/null +++ b/utils/file/dl.go @@ -0,0 +1,35 @@ +// Package file 文件实用工具 +package file + +import ( + "io" + "net/http" + "os" +) + +// DownloadTo 下载到路径 +func DownloadTo(url, file string) error { + resp, err := http.Get(url) + if err == nil { + var f *os.File + f, err = os.Create(file) + if err == nil { + _, err = io.Copy(f, resp.Body) + resp.Body.Close() + f.Close() + } + } + return err +} + +// IsExist 文件/路径存在 +func IsExist(path string) bool { + _, err := os.Stat(path) + return err == nil || os.IsExist(err) +} + +// IsExist 文件/路径不存在 +func IsNotExist(path string) bool { + _, err := os.Stat(path) + return err != nil && os.IsNotExist(err) +}