fix: 因为gitcode反爬导致tarot插件图片获取失败 (#465)

* fix tarot get img

* change: use sendtoslef check cache

但是好像过慢

* change: use imgpool

* fix

感觉改不好了
This commit is contained in:
DreamZero 2022-10-22 11:53:46 +08:00 committed by GitHub
parent 2034d91912
commit 494c1b33b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,14 +4,18 @@ package tarot
import ( import (
"encoding/json" "encoding/json"
"math/rand" "math/rand"
"os"
"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/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"
@ -55,6 +59,9 @@ func init() {
PublicDataFolder: "Tarot", PublicDataFolder: "Tarot",
}).ApplySingle(ctxext.DefaultSingle) }).ApplySingle(ctxext.DefaultSingle)
cache := engine.DataFolder() + "cache"
_ = os.MkdirAll(cache, 0755)
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 {
@ -131,10 +138,22 @@ func init() {
if p == 1 { if p == 1 {
description = card.ReverseDescription description = card.ReverseDescription
} }
if id := ctx.SendChain( imgurl := bed + reverse[p] + card.ImgURL
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")}
message.Image(bed+reverse[p]+card.ImgURL), var imgmsg message.MessageSegment
message.Text("\n其释义为: ", description)); id.ID() == 0 { var err error
if p == 1 {
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+card.Name, cache)
} else {
imgmsg, err = poolimg(ctx, imgurl, card.Name, cache)
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
tarotmsg = append(tarotmsg, imgmsg)
tarotmsg = append(tarotmsg, message.Text("\n其释义为: ", description))
if id := ctx.Send(tarotmsg).ID(); id == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控了")) ctx.SendChain(message.Text("ERROR: 可能被风控了"))
} }
return return
@ -156,24 +175,44 @@ func init() {
if p == 1 { if p == 1 {
description = card.ReverseDescription description = card.ReverseDescription
} }
tarotMsg := message.Message{ imgurl := bed + reverse[p] + card.ImgURL
message.Text(position[p], "的『", name, "』\n"), tarotmsg := message.Message{message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n")}
message.Image(bed + reverse[p] + card.ImgURL), var imgmsg message.MessageSegment
message.Text("\n其释义为: ", description)} var err error
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotMsg...) if p == 1 {
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+card.Name, cache)
} else {
imgmsg, err = poolimg(ctx, imgurl, card.Name, cache)
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
tarotmsg = append(tarotmsg, imgmsg)
tarotmsg = append(tarotmsg, message.Text("\n其释义为: ", description))
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotmsg...)
}
if id := ctx.Send(msg).ID(); id == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
} }
ctx.Send(msg)
}) })
engine.OnRegex(`^解塔罗牌\s?(.*)`, getTarot).SetBlock(true).Limit(ctxext.LimitByGroup).Handle(func(ctx *zero.Ctx) { engine.OnRegex(`^解塔罗牌\s?(.*)`, getTarot).SetBlock(true).Limit(ctxext.LimitByGroup).Handle(func(ctx *zero.Ctx) {
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 {
ctx.SendChain( imgurl := bed + info.ImgURL
message.Image(bed+info.ImgURL), var tarotmsg message.Message
message.Text("\n", match, "的含义是~"), imgmsg, err := poolimg(ctx, imgurl, match, cache)
message.Text("\n『正位』:", info.Description), if err != nil {
message.Text("\n『逆位』:", info.ReverseDescription)) ctx.SendChain(message.Text("ERROR: ", err))
return
}
tarotmsg = append(tarotmsg, imgmsg)
tarotmsg = append(tarotmsg, message.Text("\n", match, "的含义是~\n『正位』:", info.Description, "\n『逆位』:", info.ReverseDescription))
if id := ctx.Send(tarotmsg).ID(); id == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
}
} else { } else {
var build strings.Builder var build strings.Builder
build.WriteString("塔罗牌列表\n大阿尔卡纳:\n") build.WriteString("塔罗牌列表\n大阿尔卡纳:\n")
@ -186,8 +225,8 @@ func init() {
txt := build.String() txt := build.String()
cardList, err := text.RenderToBase64(txt, text.FontFile, 420, 20) cardList, err := text.RenderToBase64(txt, text.FontFile, 420, 20)
if err != nil { if err != nil {
ctx.SendChain(message.Text("没有找到", match, "噢~"))
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return
} }
ctx.SendChain(message.Text("没有找到", match, "噢~"), message.Image("base64://"+binary.BytesToString(cardList))) ctx.SendChain(message.Text("没有找到", match, "噢~"), message.Image("base64://"+binary.BytesToString(cardList)))
} }
@ -230,7 +269,20 @@ func init() {
if p == 1 { if p == 1 {
description = card.ReverseDescription description = card.ReverseDescription
} }
tarotMsg := message.Message{message.Image(bed + reverse[p] + card.ImgURL)} var tarotmsg message.Message
imgurl := bed + reverse[p] + card.ImgURL
var imgmsg message.MessageSegment
var err error
if p == 1 {
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+card.Name, cache)
} else {
imgmsg, err = poolimg(ctx, imgurl, card.Name, cache)
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
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])
@ -239,18 +291,54 @@ func init() {
build.WriteString("』\n其释义为: \n") build.WriteString("』\n其释义为: \n")
build.WriteString(description) build.WriteString(description)
build.WriteString("\n") build.WriteString("\n")
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotMsg...) msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotmsg...)
} }
txt := build.String() txt := build.String()
formation, err := text.RenderToBase64(txt, text.FontFile, 400, 20) formation, err := text.RenderToBase64(txt, text.FontFile, 420, 20)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
msg[info.CardsNum] = ctxext.FakeSenderForwardNode(ctx, message.Message{message.Image("base64://" + binary.BytesToString(formation))}...) msg[info.CardsNum] = ctxext.FakeSenderForwardNode(ctx, message.Message{message.Image("base64://" + binary.BytesToString(formation))}...)
ctx.Send(msg) if id := ctx.Send(msg).ID(); id == 0 {
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
}
} else { } else {
ctx.SendChain(message.Text("没有找到", match, "噢~\n现有牌阵列表: \n", strings.Join(formationName, "\n"))) ctx.SendChain(message.Text("没有找到", match, "噢~\n现有牌阵列表: \n", strings.Join(formationName, "\n")))
} }
}) })
} }
func poolimg(ctx *zero.Ctx, imgurl, imgname, cache string) (msg message.MessageSegment, err error) {
imgfile := cache + "/" + imgname + ".png"
aimgfile := file.BOTPATH + "/" + imgfile
m, err := pool.GetImage(imgname)
if err != nil {
if file.IsNotExist(aimgfile) {
var f *os.File
f, err = os.Create(imgfile)
if err != nil {
return
}
var data []byte
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA())
if err != nil {
return
}
_ = f.Close()
err = os.WriteFile(f.Name(), data, 0755)
if err != nil {
return
}
}
m.SetFile(aimgfile)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
msg = message.Image("file:///" + aimgfile)
} else {
msg = message.Image(m.String())
if ctxext.SendToSelf(ctx)(msg) == 0 {
msg = msg.Add("cache", "0")
}
}
return msg, nil
}