-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
285 lines (267 loc) · 7.07 KB
/
command.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
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func scanContinue() {
if skip {
return
}
fmt.Print("if want to continue, press any key (else, press ctrl + c)")
fmt.Scanln()
}
/*
args = [target directory, tags]
*/
func execUpload(cmd *cobra.Command, args []string) {
var (
err error
)
filePaths := []string{}
err = filepath.Walk(args[0], func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
for _, e := range ImageExtension {
if filepath.Ext(info.Name()) == e {
filePaths = append(filePaths, path)
}
}
return nil
})
if err != nil {
Logger.WithError(err).Errorln("error caused during walking directory")
}
Logger.Infof("%d images will be uploaded", len(filePaths))
logError := func(cur, total int, err error, path, action string) {
Logger.WithFields(logrus.Fields{
"error": err,
"path": path,
}).Errorf("(%d/%d) error : %s\n", cur+1, total, action)
}
for i, path := range filePaths {
// upload temporary image file
ftok, err := uploadFile(host, userToken, path)
if err != nil {
logError(i, len(filePaths), err, path, "upload file")
continue
}
// request reverse search
rev, err := reverseSearch(host, userToken, ftok)
if err != nil {
logError(i, len(filePaths), err, path, "search similar post")
continue
}
// create post
ptag := strings.Split(args[1], ",")
if err := createPost(host, userToken, ftok, ptag, safety, rev); err != nil {
logError(i, len(filePaths), err, path, "create post")
continue
}
Logger.Infof("(%d/%d) uploaded : %s", i+1, len(filePaths), path)
}
}
/*
args = [target directory]
*/
func execBatchUpload(cmd *cobra.Command, args []string) {
var (
Folders []BatchUploadFolder = []BatchUploadFolder{}
)
err := filepath.WalkDir(args[0], func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == d.Name() {
return nil
}
var (
Name string
Number int
)
if d.IsDir() {
switch args[1] {
case "pixiv":
n, err := fmt.Sscanf(d.Name(), "%s (%d)", &Name, &Number)
if n != 2 || err != nil {
fmt.Printf("fail to parse path '%s'\n", path)
return nil
}
case "name":
n, err := fmt.Sscanf(d.Name(), "%s", &Name)
Number = 0
if n != 1 || err != nil {
fmt.Printf("fail to parse path '%s'\n", path)
return nil
}
case "split":
Name = d.Name()
default:
return fmt.Errorf("unknown handler name: %s", args[1])
}
Folders = append(Folders, BatchUploadFolder{
Name: Name,
Number: Number,
Path: path,
})
}
return nil
})
if err != nil {
fmt.Printf("error: %s\n", err)
return
}
fmt.Printf("%d folders are parsed\n", len(Folders))
tag := ""
for _, f := range Folders {
switch args[1] {
case "pixiv":
tag = fmt.Sprintf("%s(%d)", strings.Replace(f.Name, " ", "_", -1), f.Number)
case "name":
tag = f.Name
case "split":
tag = strings.Replace(f.Name, " ", ",", -1)
default:
continue
}
Logger.WithFields(logrus.Fields{"path": f.Path, "tag": tag}).Infoln("upload folder")
execUpload(cmd, []string{f.Path, tag})
}
}
/*
args = [query, except favorite (bool)]
*/
func execDelete(cmd *cobra.Command, args []string) {
logError := func(err error) {
Logger.WithFields(logrus.Fields{
"error": err,
"query": args[0],
}).Errorln("error caused during querying posts")
}
res, err := queryPost(host, userToken, args[0], 0)
if err != nil {
logError(err)
}
Logger.Infof("%d posts are found, start recursive posts retrieving\n", res.Total)
posts := []Post{}
posts = append(posts, res.Results...)
currentPosition := len(res.Results)
if res.Total > len(res.Results) {
for {
if currentPosition >= res.Total {
break
}
res, err := queryPost(host, userToken, args[0], currentPosition)
if err != nil {
logError(err)
return
}
posts = append(posts, res.Results...)
currentPosition += len(res.Results)
}
}
Logger.Infof("posts retrieving complete. %d posts are expected, %d posts are retrieved\n", res.Total, len(posts))
scanContinue()
for i, p := range posts {
if args[1] == "true" && p.FavoriteCount > 0 {
Logger.Infof("(%d/%d) skipped : %d", i+1, len(posts), p.Id)
continue
}
if err := deletePost(host, userToken, p); err != nil {
Logger.WithFields(logrus.Fields{
"error": err,
"id": p.Id,
}).Errorf("(%d/%d) error : %d\n", i+1, len(posts), p.Id)
continue
}
Logger.Infof("(%d/%d) deleted : %d", i+1, len(posts), p.Id)
}
}
/*
args = [implication]
*/
func execBatchDelete(cmd *cobra.Command, args []string) {
/* retrieving first page of tag */
logError := func(err error) {
Logger.WithFields(logrus.Fields{
"error": err,
"query": args[0],
}).Errorln("error caused during querying posts")
}
res, err := queryTag(host, userToken, "", 0)
if err != nil {
logError(err)
}
Logger.Infof("%d tags are found, start recursive tags retrieving\n", res.Total)
/* retrieving whole tag pages */
tags := []Tag{}
tags = append(tags, res.Results...)
currentPosition := len(res.Results)
if res.Total > len(res.Results) {
for {
if currentPosition >= res.Total {
break
}
res, err := queryTag(host, userToken, "", currentPosition)
if err != nil {
logError(err)
return
}
tags = append(tags, res.Results...)
currentPosition += len(res.Results)
}
}
Logger.Infof("tags retrieving complete. %d tags are expected, %d tags are retrieved\n", res.Total, len(tags))
/* filter tags by imlicate tag */
implicateCandiate := []Tag{}
for _, t := range tags {
implications := lo.Map[Tag, string](t.Implications, func(implication Tag, index int) string {
return implication.Names[0]
})
if lo.Contains(implications, args[0]) {
implicateCandiate = append(implicateCandiate, t)
}
}
Logger.Infof("%d sanitize tag candidate found", len(implicateCandiate))
/* retrieving posts from candidate */
for i, it := range implicateCandiate {
/* exec delete command */
Logger.Infof("------ batch (%d/%d) %s", i, len(implicateCandiate), it.Names[0])
execDelete(nil, []string{it.Names[0], "true"})
/* remove target tag and add "suspected" tag */
mit := it
hasTargetTag := false
mit.Implications = lo.Filter[Tag](mit.Implications, func(item Tag, index int) bool {
if item.Names[0] != args[0] {
return true
} else {
hasTargetTag = true
return false
}
})
hasSuspected := lo.ContainsBy[Tag](mit.Implications, func(item Tag) bool {
return item.Names[0] == "suspected"
})
if !hasSuspected {
mit.Implications = append(mit.Implications, Tag{
Names: []string{"suspected"},
})
}
Logger.Infof("implication tags status: has '%s'? = %t / has 'suspected'? = %t", args[0], hasTargetTag, hasSuspected)
req := ImplicationUpdateRequest{
Version: mit.Version,
Implications: lo.Map[Tag, string](mit.Implications, func(item Tag, index int) string {
return item.Names[0]
}),
}
if _, err = updateTagImplications(host, userToken, mit.Names[0], req); err != nil {
Logger.WithError(err).Errorf("failed to update tag implications")
}
}
}