-
Notifications
You must be signed in to change notification settings - Fork 125
/
main.go
273 lines (232 loc) · 6.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package localimage
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"modernc.org/mathutil"
"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/robot"
)
var (
storageFolder = ""
pdfSuffix = ".pdf"
)
func init() {
engine := control.Register("localimage", &control.Options{
Alias: "读取本地图片",
Help: "指令:\n" +
"* 列出图片目录\n" +
"* 来点[目录名称]图片\n" +
"* 来[数量]张[目录名称]图片\n" +
"* 来点[搜索词]图片\n" +
"* 来[数量]张[搜索词]图片\n" +
"* [目录名称]\n" +
"* [目录名称] [数量]\n",
DataFolder: "localimage",
})
storageFolder = engine.GetCacheFolder()
engine.OnFullMatch("列出图片目录").SetBlock(true).Handle(func(ctx *robot.Ctx) {
folders, _ := GetSubFolder(storageFolder)
folderInfos := dir(folders)
if len(folderInfos) == 0 {
ctx.ReplyTextAndAt("目录内容为空,请管理员添加")
return
}
ctx.ReplyTextAndAt(strings.Join(folderInfos, "\n"))
})
engine.OnRegex(`^来点(.+)图片$`).SetBlock(true).Handle(func(ctx *robot.Ctx) {
if hitDir(storageFolder, ctx.State["regex_matched"].([]string)[1]) {
replyFixedCategory(ctx, storageFolder, ctx.State["regex_matched"].([]string)[1], smallAmount())
} else {
replySearch(ctx, storageFolder, ctx.State["regex_matched"].([]string)[1], smallAmount())
}
})
engine.OnRegex(`^来(\d+)张(.+)图片$`).SetBlock(true).Handle(func(ctx *robot.Ctx) {
words := ctx.State["regex_matched"].([]string)
num, _ := strconv.Atoi(words[1])
if hitDir(storageFolder, words[2]) {
replyFixedCategory(ctx, storageFolder, words[2], num)
} else {
replySearch(ctx, storageFolder, words[2], num)
}
})
folders, _ := GetSubFolder(storageFolder)
if len(folders) == 0 {
return
}
folderNames := make([]string, 0, len(folders))
for _, folder := range folders {
folderNames = append(folderNames, folder.Name())
}
engine.OnFullMatchGroup(folderNames).SetBlock(true).Handle(func(ctx *robot.Ctx) {
replyFixedCategory(ctx, storageFolder, ctx.State["matched"].(string), smallAmount())
})
engine.OnRegex(fmt.Sprintf(`^(%s) ?(\d+)$`, strings.Join(folderNames, "|"))).SetBlock(true).Handle(func(ctx *robot.Ctx) {
words := ctx.State["regex_matched"].([]string)
if num, err := strconv.Atoi(words[2]); err == nil {
replyFixedCategory(ctx, storageFolder, words[1], num)
}
})
}
func dir(folders []os.DirEntry) []string {
if len(folders) == 0 {
return nil
}
folderInfos := make([]string, 0, len(folders))
//遍历文件
for _, folder := range folders {
//获取文件名
name := folder.Name()
//跳过 . 和 ..
if name == "." || name == ".." {
continue
}
info, _ := folder.Info()
//获取文件修改时间
modTime := info.ModTime().Local()
//格式化输出结果
fileInfo := fmt.Sprintf("%s %s", name, modTime.Format("2006-01-02 15:04"))
folderInfos = append(folderInfos, fileInfo)
}
return folderInfos
}
func replySearch(ctx *robot.Ctx, storageFolder, keywords string, num int) {
if num <= 0 || keywords == "" {
return
}
title, imageUrls := searchImage(storageFolder, keywords, num)
reply(ctx, title, keywords, imageUrls)
}
func replyFixedCategory(ctx *robot.Ctx, storageFolder, category string, num int) {
if num <= 0 || category == "" {
return
}
title, imageUrls := getImage(storageFolder, category, num)
reply(ctx, title, category, imageUrls)
}
func reply(ctx *robot.Ctx, title string, target string, imageUrls []string) {
if title == "" {
ctx.ReplyTextAndAt(fmt.Sprintf("获取%s失败,请稍后重试", target))
return
}
ctx.ReplyTextAndAt(title)
for _, url := range imageUrls {
if strings.HasSuffix(url, pdfSuffix) {
ctx.ReplyFile(url)
} else {
ctx.ReplyImage(url)
if dur, err := time.ParseDuration(fmt.Sprintf("%vms", int(rand.Float64()*2000))); err == nil {
// 等待 2s 内的一个随机数
time.Sleep(dur)
}
}
}
}
func hitDir(storageFolder, keywords string) bool {
folders, _ := GetSubFolder(storageFolder)
if len(folders) == 0 {
return false
}
for _, folder := range folders {
if folder.Name() == keywords {
return true
}
}
return false
}
func getImage(storageFolder, category string, num int) (string, []string) {
categoryPath := filepath.Join(storageFolder, category)
folders, _ := GetSubFolder(categoryPath)
if len(folders) == 0 {
return "", nil
}
title, imageFiles, err := get(categoryPath, folders[rand.Intn(len(folders))], num)
if err != nil {
return "", nil
}
if len(imageFiles) == 0 {
// empty, then retry
return getImage(storageFolder, category, num)
}
return title, imageFiles
}
func searchImage(storageFolder string, keywords string, num int) (string, []string) {
folders, _ := GetSubFolder(storageFolder)
if len(folders) == 0 {
return "", nil
}
dirEntryVos := make([]DirEntryVo, 0)
for _, folder := range folders {
categoryPath := filepath.Join(storageFolder, folder.Name())
subFolders, _ := ReadDir(categoryPath, func(dirEntry os.DirEntry) bool {
return dirEntry.IsDir() && strings.Contains(strings.ToLower(dirEntry.Name()), strings.ToLower(keywords))
})
for _, subFolder := range subFolders {
dirEntryVos = append(dirEntryVos, DirEntryVo{subFolder, folder})
}
}
if len(dirEntryVos) == 0 {
return "", nil
}
// 有可能目录下为空,最多查三次
for i := 0; i < 3; i++ {
dirEntryVo := dirEntryVos[rand.Intn(len(dirEntryVos))]
categoryPath := filepath.Join(storageFolder, dirEntryVo.parentFolder.Name())
title, imageFiles, err := get(categoryPath, dirEntryVo.folder, num)
if err != nil {
return "", nil
}
if len(imageFiles) != 0 {
return title, imageFiles
}
}
return "", nil
}
func get(targetPath string, entry os.DirEntry, num int) (string, []string, error) {
title := entry.Name()
topicPath := filepath.Join(targetPath, title)
imageFiles, err := ReadDir(topicPath, IsImageFile)
if err != nil {
return "", nil, err
}
sort.Slice(imageFiles, func(i, j int) bool {
left, _ := imageFiles[i].Info()
right, _ := imageFiles[j].Info()
return left.ModTime().Before(right.ModTime())
})
num = mathutil.Min(num, len(imageFiles))
images := make([]string, 0, num)
// 小于 10 张,直接返回
if num <= 10 {
for i := 0; i < num; i++ {
images = append(images, "local://"+filepath.Join(topicPath, imageFiles[i].Name()))
}
return title, images, nil
}
// 大于 10 张,返回文件
for i := 0; i < num; i++ {
images = append(images, filepath.Join(topicPath, imageFiles[i].Name()))
}
pdfFilePath := filepath.Join(topicPath, title+"-"+strconv.Itoa(num)+pdfSuffix)
if !Exist(pdfFilePath) {
err := GeneratePdfFile(pdfFilePath, images)
if err != nil {
log.Errorf("生成 pdf 文件 %s 失败, err: %v", pdfFilePath, err)
return "", nil, err
}
}
pwd, _ := os.Getwd()
return title, []string{filepath.Join(pwd, pdfFilePath)}, nil
}
func smallAmount() int {
return rand.Intn(5) + 1
}
func GetStorageFolder() string {
return storageFolder
}