mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
✨ 优化代码结构
This commit is contained in:
parent
a862e5be7c
commit
d730fd9cab
@ -2,15 +2,14 @@
|
|||||||
package zaobao
|
package zaobao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
|
|
||||||
|
"github.com/FloatTech/zbputils/binary"
|
||||||
control "github.com/FloatTech/zbputils/control"
|
control "github.com/FloatTech/zbputils/control"
|
||||||
"github.com/FloatTech/zbputils/file"
|
|
||||||
"github.com/FloatTech/zbputils/web"
|
"github.com/FloatTech/zbputils/web"
|
||||||
|
|
||||||
"github.com/FloatTech/zbputils/control/order"
|
"github.com/FloatTech/zbputils/control/order"
|
||||||
@ -22,6 +21,11 @@ const (
|
|||||||
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66"
|
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
picdata []byte
|
||||||
|
mu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
func init() { // 插件主体
|
func init() { // 插件主体
|
||||||
engine := control.Register("zaobao", order.AcquirePrio(), &control.Options{
|
engine := control.Register("zaobao", order.AcquirePrio(), &control.Options{
|
||||||
DisableOnDefault: true,
|
DisableOnDefault: true,
|
||||||
@ -32,41 +36,34 @@ func init() { // 插件主体
|
|||||||
"每天早上九点定时发送\n" +
|
"每天早上九点定时发送\n" +
|
||||||
"记录在'00 9 * * *'触发的指令\n" +
|
"记录在'00 9 * * *'触发的指令\n" +
|
||||||
"今日早报",
|
"今日早报",
|
||||||
PrivateDataFolder: "zaobao",
|
|
||||||
})
|
})
|
||||||
cachePath := engine.DataFolder()
|
|
||||||
os.RemoveAll(cachePath)
|
|
||||||
err := os.MkdirAll(cachePath, 0755)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
zaobaoFile := cachePath + "zaobao_" + time.Now().Format("2006-01-02") + ".jpg"
|
|
||||||
engine.OnFullMatch("今日早报", zero.OnlyGroup).SetBlock(false).
|
engine.OnFullMatch("今日早报", zero.OnlyGroup).SetBlock(false).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
err := download(zaobaoFile)
|
err := getdata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR:", err))
|
ctx.SendChain(message.Text("ERROR:", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + zaobaoFile))
|
ctx.SendChain(message.ImageBytes(picdata))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func download(zaobaoFile string) error { // 获取图片链接并且下载
|
func getdata() error { // 获取图片链接并且下载
|
||||||
if file.IsNotExist(zaobaoFile) {
|
if picdata != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
if picdata != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
data, err := web.GetDataWith(web.NewDefaultClient(), api, "GET", "", ua)
|
data, err := web.GetDataWith(web.NewDefaultClient(), api, "GET", "", ua)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
zaobaoURL := gjson.Get(string(data), "url").String()
|
picdata, err = web.GetDataWith(web.NewDefaultClient(), gjson.Get(binary.BytesToString(data), "url").String(), "GET", referer, ua)
|
||||||
data, err = web.GetDataWith(web.NewDefaultClient(), zaobaoURL, "GET", referer, ua)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = os.WriteFile(zaobaoFile, data, 0666)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user