Skip to content

Commit 8855440

Browse files
authored
fix(tarot): 使用Getlazydata获取数据 (#1097)
* fix(tarot): use GetLazyData * fix(tarot): add cache folder & improve error handle * refactor(tarot): no need for clean cache
1 parent fcb01c2 commit 8855440

File tree

1 file changed

+52
-82
lines changed

1 file changed

+52
-82
lines changed

plugin/tarot/tarot.go

Lines changed: 52 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@ import (
55
"encoding/json"
66
"math/rand"
77
"os"
8+
"path/filepath"
89
"strconv"
910
"strings"
1011

1112
"github.com/FloatTech/floatbox/binary"
1213
fcext "github.com/FloatTech/floatbox/ctxext"
13-
"github.com/FloatTech/floatbox/file"
14-
"github.com/FloatTech/floatbox/process"
15-
"github.com/FloatTech/floatbox/web"
1614
ctrl "github.com/FloatTech/zbpctrl"
1715
"github.com/FloatTech/zbputils/control"
1816
"github.com/FloatTech/zbputils/ctxext"
19-
"github.com/FloatTech/zbputils/img/pool"
2017
"github.com/FloatTech/zbputils/img/text"
2118
"github.com/sirupsen/logrus"
2219
zero "github.com/wdvxdr1123/ZeroBot"
2320
"github.com/wdvxdr1123/ZeroBot/message"
2421
)
2522

26-
const bed = "https://github.moeyy.xyz/https://raw.githubusercontent.com/FloatTech/zbpdata/main/Tarot/"
27-
2823
type cardInfo struct {
2924
Description string `json:"description"`
3025
ReverseDescription string `json:"reverseDescription"`
@@ -48,6 +43,9 @@ var (
4843
formationMap = make(map[string]formation, 10)
4944
majorArcanaName = make([]string, 0, 80)
5045
formationName = make([]string, 0, 10)
46+
reverse = [...]string{"", "Reverse/"}
47+
arcanaType = [...]string{"MajorArcana", "MinorArcana"}
48+
minorArcanaType = [...]string{"Cups", "Pentacles", "Swords", "Wands"}
5149
)
5250

5351
func init() {
@@ -61,13 +59,25 @@ func init() {
6159
PublicDataFolder: "Tarot",
6260
}).ApplySingle(ctxext.DefaultSingle)
6361

64-
cache := engine.DataFolder() + "cache"
65-
_ = os.RemoveAll(cache)
66-
err := os.MkdirAll(cache, 0755)
67-
if err != nil {
68-
panic(err)
62+
for _, r := range reverse {
63+
for _, at := range arcanaType {
64+
if at == "MinorArcana" {
65+
for _, mat := range minorArcanaType {
66+
cachePath := filepath.Join(engine.DataFolder(), r, at, mat)
67+
err := os.MkdirAll(cachePath, 0755)
68+
if err != nil {
69+
panic(err)
70+
}
71+
}
72+
} else {
73+
cachePath := filepath.Join(engine.DataFolder(), r, at)
74+
err := os.MkdirAll(cachePath, 0755)
75+
if err != nil {
76+
panic(err)
77+
}
78+
}
79+
}
6980
}
70-
7181
getTarot := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
7282
data, err := engine.GetLazyData("tarots.json", true)
7383
if err != nil {
@@ -108,7 +118,6 @@ func init() {
108118
n := 1
109119
reasons := [...]string{"您抽到的是~\n", "锵锵锵,塔罗牌的预言是~\n", "诶,让我看看您抽到了~\n"}
110120
position := [...]string{"『正位』", "『逆位』"}
111-
reverse := [...]string{"", "Reverse/"}
112121
start := 0
113122
length := 22
114123
if match != "" {
@@ -140,31 +149,15 @@ func init() {
140149
if p == 1 {
141150
description = card.ReverseDescription
142151
}
143-
imgurl := bed + reverse[p] + card.ImgURL
144-
imgname := ""
145-
if p == 1 {
146-
imgname = reverse[p][:len(reverse[p])-1] + name
147-
} else {
148-
imgname = name
149-
}
150-
imgpath := cache + "/" + imgname + ".png"
151-
err := pool.SendImageFromPool(imgpath, func(pth string) error {
152-
data, err := web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "github.moeyy.xyz", web.RandUA(), nil)
153-
if err != nil {
154-
return err
155-
}
156-
f, err := os.Create(pth)
157-
if err != nil {
158-
return err
159-
}
160-
defer f.Close()
161-
return os.WriteFile(f.Name(), data, 0755)
162-
}, ctxext.Send(ctx))
152+
imgurl := reverse[p] + card.ImgURL
153+
data, err := engine.GetLazyData(imgurl, true)
163154
if err != nil {
164-
ctx.SendChain(message.Text("ERROR: ", err))
155+
// ctx.SendChain(message.Text("ERROR: ", err))
156+
logrus.Infof("[tarot]获取图片失败: %v", err)
157+
ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description))
165158
return
166159
}
167-
process.SleepAbout1sTo2s()
160+
ctx.SendChain(message.ImageBytes(data))
168161
ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description))
169162
return
170163
}
@@ -185,20 +178,19 @@ func init() {
185178
if p == 1 {
186179
description = card.ReverseDescription
187180
}
188-
imgurl := bed + reverse[p] + card.ImgURL
181+
imgurl := reverse[p] + card.ImgURL
189182
tarotmsg := message.Message{message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n")}
190183
var imgmsg message.Segment
191184
var err error
192-
if p == 1 {
193-
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
194-
} else {
195-
imgmsg, err = poolimg(imgurl, name, cache)
196-
}
185+
data, err := engine.GetLazyData(imgurl, true)
197186
if err != nil {
198-
ctx.SendChain(message.Text("ERROR: ", err))
199-
return
187+
// ctx.SendChain(message.Text("ERROR: ", err))
188+
logrus.Infof("[tarot]获取图片失败: %v", err)
189+
// return
190+
} else {
191+
imgmsg = message.ImageBytes(data)
192+
tarotmsg = append(tarotmsg, imgmsg)
200193
}
201-
tarotmsg = append(tarotmsg, imgmsg)
202194
tarotmsg = append(tarotmsg, message.Text("\n其释义为: ", description))
203195
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotmsg...)
204196
}
@@ -211,14 +203,17 @@ func init() {
211203
match := ctx.State["regex_matched"].([]string)[1]
212204
info, ok := infoMap[match]
213205
if ok {
214-
imgurl := bed + info.ImgURL
206+
imgurl := info.ImgURL
215207
var tarotmsg message.Message
216-
imgmsg, err := poolimg(imgurl, match, cache)
208+
data, err := engine.GetLazyData(imgurl, true)
217209
if err != nil {
218-
ctx.SendChain(message.Text("ERROR: ", err))
219-
return
210+
// ctx.SendChain(message.Text("ERROR: ", err))
211+
logrus.Infof("[tarot]获取图片失败: %v", err)
212+
// return
213+
} else {
214+
imgmsg := message.ImageBytes(data)
215+
tarotmsg = append(tarotmsg, imgmsg)
220216
}
221-
tarotmsg = append(tarotmsg, imgmsg)
222217
tarotmsg = append(tarotmsg, message.Text("\n", match, "的含义是~\n『正位』:", info.Description, "\n『逆位』:", info.ReverseDescription))
223218
if id := ctx.Send(tarotmsg).ID(); id == 0 {
224219
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
@@ -280,19 +275,18 @@ func init() {
280275
description = card.ReverseDescription
281276
}
282277
var tarotmsg message.Message
283-
imgurl := bed + reverse[p] + card.ImgURL
278+
imgurl := reverse[p] + card.ImgURL
284279
var imgmsg message.Segment
285280
var err error
286-
if p == 1 {
287-
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
288-
} else {
289-
imgmsg, err = poolimg(imgurl, name, cache)
290-
}
281+
data, err := engine.GetLazyData(imgurl, true)
291282
if err != nil {
292-
ctx.SendChain(message.Text("ERROR: ", err))
293-
return
283+
// ctx.SendChain(message.Text("ERROR: ", err))
284+
logrus.Infof("[tarot]获取图片失败: %v", err)
285+
// return
286+
} else {
287+
imgmsg = message.ImageBytes(data)
288+
tarotmsg = append(tarotmsg, imgmsg)
294289
}
295-
tarotmsg = append(tarotmsg, imgmsg)
296290
build.WriteString(info.Represent[0][i])
297291
build.WriteString(":")
298292
build.WriteString(position[p])
@@ -318,27 +312,3 @@ func init() {
318312
}
319313
})
320314
}
321-
322-
func poolimg(imgurl, imgname, cache string) (msg message.Segment, err error) {
323-
imgfile := cache + "/" + imgname + ".png"
324-
aimgfile := file.BOTPATH + "/" + imgfile
325-
if file.IsNotExist(aimgfile) {
326-
var data []byte
327-
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "github.moeyy.xyz", web.RandUA(), nil)
328-
if err != nil {
329-
return
330-
}
331-
var f *os.File
332-
f, err = os.Create(imgfile)
333-
if err != nil {
334-
return
335-
}
336-
defer f.Close()
337-
err = os.WriteFile(f.Name(), data, 0755)
338-
if err != nil {
339-
return
340-
}
341-
}
342-
msg = message.Image("file:///" + aimgfile)
343-
return
344-
}

0 commit comments

Comments
 (0)