-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
279 lines (256 loc) · 7.6 KB
/
process.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
package ugoira
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"github.com/KJHJason/Cultured-Downloader-Logic/api"
pixivcommon "github.com/KJHJason/Cultured-Downloader-Logic/api/pixiv/common"
"github.com/KJHJason/Cultured-Downloader-Logic/configs"
"github.com/KJHJason/Cultured-Downloader-Logic/constants"
cdlerrors "github.com/KJHJason/Cultured-Downloader-Logic/errors"
"github.com/KJHJason/Cultured-Downloader-Logic/extractor"
"github.com/KJHJason/Cultured-Downloader-Logic/httpfuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/iofuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/logger"
"github.com/KJHJason/Cultured-Downloader-Logic/progress"
)
// Map the Ugoira frame delays to their respective filenames
func MapDelaysToFilename(ugoiraFramesJson UgoiraFramesJson) map[string]int64 {
frameInfoMap := map[string]int64{}
for _, frame := range ugoiraFramesJson {
frameInfoMap[frame.File] = int64(frame.Delay)
}
return frameInfoMap
}
type UgoiraFfmpegArgs struct {
context context.Context
ffmpegPath string
outputPath string
ugoiraQuality int
}
// Converts the Ugoira to the desired output path using FFmpeg
func ConvertUgoira(ugoiraInfo *Ugoira, imagesFolderPath string, ugoiraFfmpeg *UgoiraFfmpegArgs) error {
outputExt := filepath.Ext(ugoiraFfmpeg.outputPath)
if !api.SliceContains(UGOIRA_ACCEPTED_EXT, outputExt) {
return fmt.Errorf(
"pixiv error %d: Output extension %s is not allowed for ugoira conversion",
cdlerrors.INPUT_ERROR,
outputExt,
)
}
concatDelayFilePath, sortedFilenames, err := writeDelays(ugoiraInfo, imagesFolderPath)
if err != nil {
return err
}
args, err := getFfmpegFlagsForUgoira(
&ffmpegOptions{
ugoiraArgs: ugoiraFfmpeg,
outputExt: outputExt,
concatDelayFilePath: concatDelayFilePath,
sortedFilenames: sortedFilenames,
},
imagesFolderPath,
)
if err != nil {
return err
}
// convert the frames to a gif or a video
cmd := exec.CommandContext(ugoiraFfmpeg.context, ugoiraFfmpeg.ffmpegPath, args...)
if runtime.GOOS == "windows" {
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
}
// cmd.Stderr = os.Stderr
// cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
os.Remove(ugoiraFfmpeg.outputPath)
if errors.Is(err, context.Canceled) {
return err
}
return fmt.Errorf(
"pixiv error %d: failed to convert ugoira to %s, more info => %w",
cdlerrors.CMD_ERROR,
ugoiraFfmpeg.outputPath,
err,
)
}
// delete unzipped folder which contains
// the frames images and the delays text file
os.RemoveAll(imagesFolderPath)
return nil
}
// Returns the ugoira's zip file path and the ugoira's converted file path
func GetUgoiraFilePaths(ugoireFilePath, ugoiraUrl, outputFormat string) (string, string) {
filePath := filepath.Join(ugoireFilePath, httpfuncs.GetLastPartOfUrl(ugoiraUrl))
outputFilePath := iofuncs.RemoveExtFromFilename(filePath) + outputFormat
return filePath, outputFilePath
}
func convertMultipleUgoira(ugoiraArgs *UgoiraArgs, ugoiraOptions *UgoiraOptions, config *configs.Config) []error {
// Create a context that can be cancelled when SIGINT/SIGTERM signal is received
ctx, cancel := context.WithCancel(ugoiraArgs.context)
defer cancel()
// Catch SIGINT/SIGTERM signal and cancel the context when received
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigs
cancel()
}()
defer signal.Stop(sigs)
var errSlice []error
downloadInfoLen := len(ugoiraArgs.ToDownload)
baseMsg := fmt.Sprintf("Converting Ugoira to %s ", ugoiraOptions.OutputFormat) + "[%d/" + fmt.Sprintf("%d]...", downloadInfoLen)
prog := ugoiraArgs.MainProgBar
prog.UpdateBaseMsg(baseMsg)
prog.UpdateSuccessMsg(
fmt.Sprintf(
"Finished converting %d Ugoira to %s!",
downloadInfoLen,
ugoiraOptions.OutputFormat,
),
)
prog.UpdateErrorMsg(
fmt.Sprintf(
"Something went wrong while converting %d Ugoira to %s!\nPlease refer to the logs for more details.",
downloadInfoLen,
ugoiraOptions.OutputFormat,
),
)
prog.SetToProgressBar()
prog.UpdateMax(downloadInfoLen)
defer prog.SnapshotTask()
prog.Start()
for i, ugoira := range ugoiraArgs.ToDownload {
zipFilePath, outputPath := GetUgoiraFilePaths(ugoira.FilePath, ugoira.Url, ugoiraOptions.OutputFormat)
if iofuncs.PathExists(outputPath) {
prog.Increment()
continue
}
if !iofuncs.PathExists(zipFilePath) {
prog.Increment()
continue
}
unzipFolderPath := filepath.Join(
filepath.Dir(zipFilePath),
"unzipped",
)
err := extractor.ExtractFiles(ctx, zipFilePath, unzipFolderPath, true)
if err != nil {
if errors.Is(err, context.Canceled) {
prog.StopInterrupt(
fmt.Sprintf(
"Stopped converting ugoira to %s [%d/%d]!",
ugoiraOptions.OutputFormat,
i,
len(ugoiraArgs.ToDownload),
),
)
}
err := fmt.Errorf(
"pixiv error %d: failed to unzip file %s, more info => %w",
cdlerrors.OS_ERROR,
zipFilePath,
err,
)
errSlice = append(errSlice, err)
prog.Increment()
continue
}
err = ConvertUgoira(
ugoira,
unzipFolderPath,
&UgoiraFfmpegArgs{
context: ctx,
ffmpegPath: config.FfmpegPath,
outputPath: outputPath,
ugoiraQuality: ugoiraOptions.Quality,
},
)
if err != nil {
errSlice = append(errSlice, err)
} else if ugoiraOptions.DeleteZip {
os.Remove(zipFilePath)
}
prog.Increment()
}
hasErr := false
if len(errSlice) > 0 {
hasErr = true
if hasCancelled := logger.LogErrors(false, logger.ERROR, errSlice...); hasCancelled {
prog.StopInterrupt(
fmt.Sprintf("Stopped converting ugoira to %s!", ugoiraOptions.OutputFormat),
)
ugoiraArgs.cancel()
}
}
prog.Stop(hasErr)
return errSlice
}
type UgoiraArgs struct {
context context.Context
cancel context.CancelFunc
UseMobileApi bool
ToDownload []*Ugoira
Cookies []*http.Cookie
MainProgBar progress.ProgressBar
}
func (u *UgoiraArgs) SetContext(ctx context.Context) {
u.context, u.cancel = context.WithCancel(ctx)
}
// Downloads multiple Ugoira artworks and converts them based on the output format
func DownloadMultipleUgoira(ugoiraArgs *UgoiraArgs, ugoiraOptions *UgoiraOptions, config *configs.Config, reqHandler httpfuncs.RequestHandler, progBarInfo *progress.ProgressBarInfo) []error {
var urlsToDownload []*httpfuncs.ToDownload
for _, ugoira := range ugoiraArgs.ToDownload {
filePath, outputFilePath := GetUgoiraFilePaths(
ugoira.FilePath,
ugoira.Url,
ugoiraOptions.OutputFormat,
)
if !iofuncs.PathExists(outputFilePath) {
urlsToDownload = append(urlsToDownload, &httpfuncs.ToDownload{
Url: ugoira.Url,
FilePath: filePath,
})
}
}
var useHttp3 bool
var headers map[string]string
if ugoiraArgs.UseMobileApi {
headers = map[string]string{
"Referer": constants.PIXIV_MOBILE_URL,
}
} else {
headers = pixivcommon.GetPixivRequestHeaders()
useHttp3 = httpfuncs.IsHttp3Supported(constants.PIXIV, true)
}
cancelled, err := httpfuncs.DownloadUrlsWithHandler(
urlsToDownload,
&httpfuncs.DlOptions{
Context: ugoiraArgs.context,
MaxConcurrency: constants.PIXIV_MAX_DOWNLOAD_CONCURRENCY,
SupportRange: constants.PIXIV_RANGE_SUPPORTED,
HeadReqTimeout: constants.DEFAULT_HEAD_REQ_TIMEOUT,
Headers: headers,
Cookies: ugoiraArgs.Cookies,
UseHttp3: useHttp3,
ProgressBarInfo: progBarInfo,
},
config, // Note: if isMobileApi is true, custom user-agent will be ignored
reqHandler,
)
if cancelled {
ugoiraArgs.cancel()
return nil
}
if len(err) > 0 {
return err
}
return convertMultipleUgoira(ugoiraArgs, ugoiraOptions, config)
}