From abd4374941081dc4e25a54937460430a95214c2d Mon Sep 17 00:00:00 2001 From: shudorcl <1985366171@qq.com> Date: Mon, 14 Oct 2024 20:40:19 +0800 Subject: [PATCH] =?UTF-8?q?tarot:=20=E5=B0=86=E6=95=B0=E6=8D=AE=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=88=B0data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data | 2 +- plugin/tarot/tarot.go | 81 +++++++++---------------------------------- 2 files changed, 17 insertions(+), 66 deletions(-) diff --git a/data b/data index f1f8cd107c..8d31381a08 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit f1f8cd107cacaf8d8e358d7468f7c9b25adf637a +Subproject commit 8d31381a08234d86b72ca05424b8bb513428fba7 diff --git a/plugin/tarot/tarot.go b/plugin/tarot/tarot.go index 45cc944364..ee54ba02fa 100644 --- a/plugin/tarot/tarot.go +++ b/plugin/tarot/tarot.go @@ -10,21 +10,15 @@ import ( "github.com/FloatTech/floatbox/binary" 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" "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" - "github.com/FloatTech/zbputils/img/pool" "github.com/FloatTech/zbputils/img/text" "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" ) -const bed = "https://gitcode.net/shudorcl/zbp-tarot/-/raw/master/" - type cardInfo struct { Description string `json:"description"` ReverseDescription string `json:"reverseDescription"` @@ -140,31 +134,14 @@ func init() { if p == 1 { description = card.ReverseDescription } - imgurl := bed + reverse[p] + card.ImgURL - imgname := "" - 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", "gitcode.net", 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)) + imgurl := reverse[p] + card.ImgURL + logrus.Infof("尝试获取图片: " + imgurl) + data, err := engine.GetLazyData(imgurl, true) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return } - process.SleepAbout1sTo2s() + ctx.SendChain(message.ImageBytes(data)) ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description)) return } @@ -185,15 +162,13 @@ func init() { if p == 1 { 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")} var imgmsg message.MessageSegment var err error - if p == 1 { - imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache) - } else { - imgmsg, err = poolimg(imgurl, name, cache) - } + logrus.Infof("尝试获取图片: " + imgurl) + data, err := engine.GetLazyData(imgurl, true) + imgmsg = message.ImageBytes(data) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return @@ -211,9 +186,11 @@ func init() { match := ctx.State["regex_matched"].([]string)[1] info, ok := infoMap[match] if ok { - imgurl := bed + info.ImgURL + imgurl := info.ImgURL var tarotmsg message.Message - imgmsg, err := poolimg(imgurl, match, cache) + logrus.Infof("尝试获取图片: " + imgurl) + data, err := engine.GetLazyData(imgurl, true) + imgmsg := message.ImageBytes(data) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return @@ -280,14 +257,12 @@ func init() { description = card.ReverseDescription } var tarotmsg message.Message - imgurl := bed + reverse[p] + card.ImgURL + imgurl := reverse[p] + card.ImgURL var imgmsg message.MessageSegment var err error - if p == 1 { - imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache) - } else { - imgmsg, err = poolimg(imgurl, name, cache) - } + logrus.Infof("尝试获取图片: " + imgurl) + data, err := engine.GetLazyData(imgurl, true) + imgmsg = message.ImageBytes(data) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return @@ -318,27 +293,3 @@ func init() { } }) } - -func poolimg(imgurl, imgname, cache string) (msg message.MessageSegment, 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", "gitcode.net", 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 -}