fix(tarot): 使用Getlazydata获取数据 (#1097)

* fix(tarot): use GetLazyData

* fix(tarot): add cache folder & improve error handle

* refactor(tarot): no need for clean cache
This commit is contained in:
莫思潋 2025-01-07 21:02:49 +08:00 committed by GitHub
parent fcb01c2c18
commit 885544077b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,26 +5,21 @@ import (
"encoding/json" "encoding/json"
"math/rand" "math/rand"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/FloatTech/floatbox/binary" "github.com/FloatTech/floatbox/binary"
fcext "github.com/FloatTech/floatbox/ctxext" fcext "github.com/FloatTech/floatbox/ctxext"
"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/floatbox/process"
"github.com/FloatTech/floatbox/web"
ctrl "github.com/FloatTech/zbpctrl" ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/pool"
"github.com/FloatTech/zbputils/img/text" "github.com/FloatTech/zbputils/img/text"
"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"
) )
const bed = "https://github.moeyy.xyz/https://raw.githubusercontent.com/FloatTech/zbpdata/main/Tarot/"
type cardInfo struct { type cardInfo struct {
Description string `json:"description"` Description string `json:"description"`
ReverseDescription string `json:"reverseDescription"` ReverseDescription string `json:"reverseDescription"`
@ -48,6 +43,9 @@ var (
formationMap = make(map[string]formation, 10) formationMap = make(map[string]formation, 10)
majorArcanaName = make([]string, 0, 80) majorArcanaName = make([]string, 0, 80)
formationName = make([]string, 0, 10) formationName = make([]string, 0, 10)
reverse = [...]string{"", "Reverse/"}
arcanaType = [...]string{"MajorArcana", "MinorArcana"}
minorArcanaType = [...]string{"Cups", "Pentacles", "Swords", "Wands"}
) )
func init() { func init() {
@ -61,13 +59,25 @@ func init() {
PublicDataFolder: "Tarot", PublicDataFolder: "Tarot",
}).ApplySingle(ctxext.DefaultSingle) }).ApplySingle(ctxext.DefaultSingle)
cache := engine.DataFolder() + "cache" for _, r := range reverse {
_ = os.RemoveAll(cache) for _, at := range arcanaType {
err := os.MkdirAll(cache, 0755) if at == "MinorArcana" {
if err != nil { for _, mat := range minorArcanaType {
panic(err) cachePath := filepath.Join(engine.DataFolder(), r, at, mat)
err := os.MkdirAll(cachePath, 0755)
if err != nil {
panic(err)
}
}
} else {
cachePath := filepath.Join(engine.DataFolder(), r, at)
err := os.MkdirAll(cachePath, 0755)
if err != nil {
panic(err)
}
}
}
} }
getTarot := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool { getTarot := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
data, err := engine.GetLazyData("tarots.json", true) data, err := engine.GetLazyData("tarots.json", true)
if err != nil { if err != nil {
@ -108,7 +118,6 @@ func init() {
n := 1 n := 1
reasons := [...]string{"您抽到的是~\n", "锵锵锵,塔罗牌的预言是~\n", "诶,让我看看您抽到了~\n"} reasons := [...]string{"您抽到的是~\n", "锵锵锵,塔罗牌的预言是~\n", "诶,让我看看您抽到了~\n"}
position := [...]string{"『正位』", "『逆位』"} position := [...]string{"『正位』", "『逆位』"}
reverse := [...]string{"", "Reverse/"}
start := 0 start := 0
length := 22 length := 22
if match != "" { if match != "" {
@ -140,31 +149,15 @@ func init() {
if p == 1 { if p == 1 {
description = card.ReverseDescription description = card.ReverseDescription
} }
imgurl := bed + reverse[p] + card.ImgURL imgurl := reverse[p] + card.ImgURL
imgname := "" data, err := engine.GetLazyData(imgurl, true)
if p == 1 {
imgname = reverse[p][:len(reverse[p])-1] + name
} else {
imgname = name
}
imgpath := cache + "/" + imgname + ".png"
err := pool.SendImageFromPool(imgpath, func(pth string) error {
data, err := web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "github.moeyy.xyz", web.RandUA(), nil)
if err != nil {
return err
}
f, err := os.Create(pth)
if err != nil {
return err
}
defer f.Close()
return os.WriteFile(f.Name(), data, 0755)
}, ctxext.Send(ctx))
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) // ctx.SendChain(message.Text("ERROR: ", err))
logrus.Infof("[tarot]获取图片失败: %v", err)
ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description))
return return
} }
process.SleepAbout1sTo2s() ctx.SendChain(message.ImageBytes(data))
ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description)) ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description))
return return
} }
@ -185,20 +178,19 @@ func init() {
if p == 1 { if p == 1 {
description = card.ReverseDescription description = card.ReverseDescription
} }
imgurl := bed + reverse[p] + card.ImgURL imgurl := reverse[p] + card.ImgURL
tarotmsg := message.Message{message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n")} tarotmsg := message.Message{message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n")}
var imgmsg message.Segment var imgmsg message.Segment
var err error var err error
if p == 1 { data, err := engine.GetLazyData(imgurl, true)
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
} else {
imgmsg, err = poolimg(imgurl, name, cache)
}
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) // ctx.SendChain(message.Text("ERROR: ", err))
return logrus.Infof("[tarot]获取图片失败: %v", err)
// return
} else {
imgmsg = message.ImageBytes(data)
tarotmsg = append(tarotmsg, imgmsg)
} }
tarotmsg = append(tarotmsg, imgmsg)
tarotmsg = append(tarotmsg, message.Text("\n其释义为: ", description)) tarotmsg = append(tarotmsg, message.Text("\n其释义为: ", description))
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotmsg...) msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotmsg...)
} }
@ -211,14 +203,17 @@ func init() {
match := ctx.State["regex_matched"].([]string)[1] match := ctx.State["regex_matched"].([]string)[1]
info, ok := infoMap[match] info, ok := infoMap[match]
if ok { if ok {
imgurl := bed + info.ImgURL imgurl := info.ImgURL
var tarotmsg message.Message var tarotmsg message.Message
imgmsg, err := poolimg(imgurl, match, cache) data, err := engine.GetLazyData(imgurl, true)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) // ctx.SendChain(message.Text("ERROR: ", err))
return logrus.Infof("[tarot]获取图片失败: %v", err)
// return
} else {
imgmsg := message.ImageBytes(data)
tarotmsg = append(tarotmsg, imgmsg)
} }
tarotmsg = append(tarotmsg, imgmsg)
tarotmsg = append(tarotmsg, message.Text("\n", match, "的含义是~\n『正位』:", info.Description, "\n『逆位』:", info.ReverseDescription)) tarotmsg = append(tarotmsg, message.Text("\n", match, "的含义是~\n『正位』:", info.Description, "\n『逆位』:", info.ReverseDescription))
if id := ctx.Send(tarotmsg).ID(); id == 0 { if id := ctx.Send(tarotmsg).ID(); id == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控了")) ctx.SendChain(message.Text("ERROR: 可能被风控了"))
@ -280,19 +275,18 @@ func init() {
description = card.ReverseDescription description = card.ReverseDescription
} }
var tarotmsg message.Message var tarotmsg message.Message
imgurl := bed + reverse[p] + card.ImgURL imgurl := reverse[p] + card.ImgURL
var imgmsg message.Segment var imgmsg message.Segment
var err error var err error
if p == 1 { data, err := engine.GetLazyData(imgurl, true)
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
} else {
imgmsg, err = poolimg(imgurl, name, cache)
}
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) // ctx.SendChain(message.Text("ERROR: ", err))
return logrus.Infof("[tarot]获取图片失败: %v", err)
// return
} else {
imgmsg = message.ImageBytes(data)
tarotmsg = append(tarotmsg, imgmsg)
} }
tarotmsg = append(tarotmsg, imgmsg)
build.WriteString(info.Represent[0][i]) build.WriteString(info.Represent[0][i])
build.WriteString(":") build.WriteString(":")
build.WriteString(position[p]) build.WriteString(position[p])
@ -318,27 +312,3 @@ func init() {
} }
}) })
} }
func poolimg(imgurl, imgname, cache string) (msg message.Segment, err error) {
imgfile := cache + "/" + imgname + ".png"
aimgfile := file.BOTPATH + "/" + imgfile
if file.IsNotExist(aimgfile) {
var data []byte
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "github.moeyy.xyz", web.RandUA(), nil)
if err != nil {
return
}
var f *os.File
f, err = os.Create(imgfile)
if err != nil {
return
}
defer f.Close()
err = os.WriteFile(f.Name(), data, 0755)
if err != nil {
return
}
}
msg = message.Image("file:///" + aimgfile)
return
}