|
1 | 1 | package memepicture |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/base64" |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
5 | 10 | "strings" |
| 11 | + "time" |
6 | 12 |
|
7 | | - "github.com/imroc/req/v3" |
| 13 | + "github.com/gabriel-vasile/mimetype" |
8 | 14 |
|
9 | 15 | "github.com/yqchilde/wxbot/engine/control" |
| 16 | + "github.com/yqchilde/wxbot/engine/pkg/log" |
| 17 | + "github.com/yqchilde/wxbot/engine/pkg/utils" |
10 | 18 | "github.com/yqchilde/wxbot/engine/robot" |
11 | 19 | ) |
12 | 20 |
|
13 | 21 | func init() { |
14 | 22 | engine := control.Register("memepicture", &control.Options{ |
15 | | - Alias: "表情原图", |
16 | | - Help: "输入 {表情原图} => 30s内发送表情获取表情原图", |
| 23 | + Alias: "表情原图", |
| 24 | + Help: "输入 {表情原图} => 30s内发送表情获取表情原图", |
| 25 | + DataFolder: "memepicture", |
17 | 26 | }) |
18 | 27 |
|
19 | 28 | engine.OnFullMatch("表情原图", robot.MustMemePicture).SetBlock(true).Handle(func(ctx *robot.Ctx) { |
20 | | - url := ctx.State["image_url"].(string) |
21 | | - if strings.HasPrefix(url[:20], "http") { |
22 | | - url = base64.StdEncoding.EncodeToString([]byte(url)) |
23 | | - var resp apiResponse |
24 | | - if err := req.C().Post("https://bot.yqqy.top/api/thumbnail"). |
25 | | - SetFormData(map[string]string{ |
26 | | - "type": "url", |
27 | | - "image": url, |
28 | | - }).Do().Into(&resp); err == nil { |
29 | | - ctx.ReplyShareLink("快来下载你要的表情原图", "打开后长按图片可保存到本地哦", "https://bot.yqqy.top/api/thumbnail?image="+resp.Data.Thumbnail, "https://bot.yqqy.top/api/direct?image="+resp.Data.Original) |
| 29 | + imageUrl := ctx.State["image_url"].(string) |
| 30 | + host := ctx.Bot.GetConfig().ServerAddress |
| 31 | + if host == "" { |
| 32 | + ctx.ReplyTextAndAt("请先配置在配置文件中配置serverAddress") |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + switch ctx.Bot.GetConfig().Framework.Name { |
| 37 | + case "千寻", "qianxun": |
| 38 | + original, thumbnail, err := featureImage(imageUrl, "", engine.GetCacheFolder()) |
| 39 | + if err != nil { |
| 40 | + log.Errorf("获取表情原图失败: %v", err) |
| 41 | + ctx.ReplyTextAndAt("获取表情原图失败") |
| 42 | + return |
30 | 43 | } |
31 | | - } else { |
32 | | - var resp apiResponse |
33 | | - if err := req.C().Post("https://bot.yqqy.top/api/thumbnail"). |
34 | | - SetFormData(map[string]string{ |
35 | | - "type": "base64", |
36 | | - "image": url, |
37 | | - }).Do().Into(&resp); err == nil { |
38 | | - ctx.ReplyShareLink("快来下载你要的表情原图", "打开后长按图片可保存到本地哦", "https://bot.yqqy.top/api/thumbnail?image="+resp.Data.Thumbnail, "https://bot.yqqy.top/api/direct?image="+resp.Data.Original) |
| 44 | + thumbnail = fmt.Sprintf("%s/wxbot/static?path=%s", host, url.QueryEscape(thumbnail)) |
| 45 | + original = fmt.Sprintf("%s/wxbot/static?path=%s", host, url.QueryEscape(original)) |
| 46 | + jumpUrl := fmt.Sprintf("%s/memepicture?img=%s", host, original) |
| 47 | + ctx.ReplyShareLink("快来下载你要的表情原图", "打开后长按图片可保存到本地哦", thumbnail, jumpUrl) |
| 48 | + case "VLW", "vlw": |
| 49 | + original, thumbnail, err := featureImage("", imageUrl, engine.GetCacheFolder()) |
| 50 | + if err != nil { |
| 51 | + log.Errorf("获取表情原图失败: %v", err) |
| 52 | + ctx.ReplyTextAndAt("获取表情原图失败") |
| 53 | + return |
39 | 54 | } |
| 55 | + thumbnail = fmt.Sprintf("%s/wxbot/static?path=%s", host, url.QueryEscape(thumbnail)) |
| 56 | + original = fmt.Sprintf("%s/wxbot/static?path=%s", host, url.QueryEscape(original)) |
| 57 | + jumpUrl := fmt.Sprintf("%s/memepicture?img=%s", host, original) |
| 58 | + ctx.ReplyShareLink("快来下载你要的表情原图", "打开后长按图片可保存到本地哦", thumbnail, jumpUrl) |
| 59 | + default: |
| 60 | + ctx.ReplyTextAndAt("暂不支持该框架,请联系管理员") |
40 | 61 | } |
41 | 62 | }) |
42 | 63 | } |
43 | 64 |
|
44 | | -type apiResponse struct { |
45 | | - Code int `json:"code"` |
46 | | - Data struct { |
47 | | - Thumbnail string `json:"thumbnail"` |
48 | | - Original string `json:"original"` |
49 | | - } `json:"data"` |
| 65 | +func featureImage(url, b64, cacheDir string) (original, thumbnail string, err error) { |
| 66 | + tmpFile := "/tmp/" + time.Now().Local().Format("20060102150405") + ".png" |
| 67 | + if url == "" && b64 == "" { |
| 68 | + return "", "", errors.New("url和b64不能同时为空") |
| 69 | + } |
| 70 | + if url != "" && b64 != "" { |
| 71 | + return "", "", errors.New("url和b64不能同时存在") |
| 72 | + } |
| 73 | + if url != "" { |
| 74 | + // 下载图片 |
| 75 | + resp, err := http.Get(url) |
| 76 | + if err != nil { |
| 77 | + return |
| 78 | + } |
| 79 | + defer resp.Body.Close() |
| 80 | + |
| 81 | + // 保存图片 |
| 82 | + file, err := os.Create(tmpFile) |
| 83 | + if err != nil { |
| 84 | + os.Remove(tmpFile) |
| 85 | + return |
| 86 | + } |
| 87 | + defer file.Close() |
| 88 | + if _, err := io.Copy(file, resp.Body); err != nil { |
| 89 | + os.Remove(tmpFile) |
| 90 | + return |
| 91 | + } |
| 92 | + } |
| 93 | + if b64 != "" { |
| 94 | + // 保存图片 |
| 95 | + if err := utils.Base64ToImage(b64, tmpFile); err != nil { |
| 96 | + os.Remove(tmpFile) |
| 97 | + return |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // 检测图片类型 |
| 102 | + mime, err := mimetype.DetectFile(tmpFile) |
| 103 | + if err != nil { |
| 104 | + os.Remove(tmpFile) |
| 105 | + return |
| 106 | + } |
| 107 | + original = fmt.Sprintf("%s/%s%s", cacheDir, time.Now().Local().Format("20060102150405"), mime.Extension()) |
| 108 | + os.Rename(tmpFile, original) |
| 109 | + |
| 110 | + // 生成缩略图 |
| 111 | + if mime.Extension() == ".gif" { // gif |
| 112 | + thumbnail = strings.ReplaceAll(original, "origin", "thumb") |
| 113 | + thumbnail = strings.ReplaceAll(original, ".gif", ".png") |
| 114 | + return original, thumbnail, gif2Png(original, thumbnail) |
| 115 | + } else { |
| 116 | + thumbnail = original |
| 117 | + return original, thumbnail, nil |
| 118 | + } |
50 | 119 | } |
0 commit comments