Skip to content

Commit

Permalink
perf: 早报插件图片存到本地,防止网络图片端限制拉取频率 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde authored Mar 2, 2023
1 parent c743086 commit 3de5d13
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
24 changes: 24 additions & 0 deletions engine/pkg/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/base64"
"io"
"net/http"
"os"
"strings"
)

// CheckFolderExists 检查文件夹是否存在,如果不存在则创建
Expand Down Expand Up @@ -53,3 +55,25 @@ func Base64ToImage(b64Str, dst string) error {
}
return nil
}

// IsImageFile 判断文件是否存在,如果存在是否为图片类型
func IsImageFile(path string) bool {
if !CheckPathExists(path) {
return false
}
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
buff := make([]byte, 512)
_, err = file.Read(buff)
if err != nil {
return false
}
fileType := http.DetectContentType(buff)
if strings.Contains(fileType, "image") {
return true
}
return false
}
8 changes: 7 additions & 1 deletion plugins/zaobao/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type zaoBaoResp struct {
LogId string `json:"log_id"`
}

func getZaoBao(token string) error {
func flushZaoBao(token, dstFile string) error {
var data zaoBaoResp
err := req.C().Get("https://v2.alapi.cn/api/zaobao").
SetQueryParams(map[string]string{"format": "json", "token": token}).
Expand All @@ -47,5 +47,11 @@ func getZaoBao(token string) error {
}
zaoBao.Date = data.Data.Date
zaoBao.Image = data.Data.Image

// 下载图片
if err := req.C().Get(data.Data.Image).SetOutputFile(dstFile).Do().Err; err != nil {
log.Errorf("[zaoBao]下载图片失败: %v", err)
return fmt.Errorf("获取数据失败")
}
return nil
}
24 changes: 14 additions & 10 deletions plugins/zaobao/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package zaobao

import (
"fmt"
"path/filepath"
"sync"
"time"

"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/pkg/sqlite"
"github.com/yqchilde/wxbot/engine/pkg/utils"
"github.com/yqchilde/wxbot/engine/robot"
)

Expand Down Expand Up @@ -61,13 +63,14 @@ func init() {
ctx.ReplyTextAndAt("请先私聊机器人配置token\n指令:set zaobao token __\n相关秘钥申请地址:https://admin.alapi.cn")
return
}
if zaoBao.Date != time.Now().Local().Format("2006-01-02") {
if err := getZaoBao(zaoBao.Token); err != nil {
ctx.ReplyTextAndAt(err.Error())
imgCache := filepath.Join(engine.GetCacheFolder(), time.Now().Local().Format("20060102")+".jpg")
if !utils.IsImageFile(imgCache) {
if err := flushZaoBao(zaoBao.Token, imgCache); err != nil {
ctx.ReplyTextAndAt("获取早报失败")
return
}
}
ctx.ReplyImage(zaoBao.Image)
ctx.ReplyImage("local://" + imgCache)
})

engine.OnRegex("set zaobao token ([0-9a-zA-Z]{16})", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
Expand Down Expand Up @@ -99,10 +102,10 @@ func pollingTask() {
timer.Stop()

// 任务
doSendImage := func() {
doSendImage := func(imgCache string) {
waitSendImage.Range(func(key, val interface{}) bool {
ctx := val.(*robot.Ctx)
ctx.SendImage(key.(string), zaoBao.Image)
ctx.SendImage(key.(string), "local://"+imgCache)
waitSendImage.Delete(key)
// 有时候连续发图片会有问题,所以延迟10s
time.Sleep(10 * time.Second)
Expand All @@ -124,12 +127,13 @@ func pollingTask() {
}

// 早报未更新
if zaoBao.Image == "" || zaoBao.Date != time.Now().Format("2006-01-02") {
if err := getZaoBao(zaoBao.Token); err != nil {
imgCache := filepath.Join("./data/plugins/zaobao/cache", time.Now().Local().Format("20060102")+".jpg")
if !utils.IsImageFile(imgCache) {
if err := flushZaoBao(zaoBao.Token, imgCache); err != nil {
continue
}
doSendImage()
doSendImage(imgCache)
}
doSendImage()
doSendImage(imgCache)
}
}

0 comments on commit 3de5d13

Please sign in to comment.