forked from Dictor/szurubooru-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
190 lines (178 loc) · 4.44 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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
/*
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))
fmt.Print("if want to continue, press enter (else, press ctrl + c)")
fmt.Scanln()
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)
}
}