-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
182 lines (166 loc) · 5.4 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
package fantia
import (
"errors"
"fmt"
"net/http"
"path/filepath"
"strconv"
"github.com/KJHJason/Cultured-Downloader-Logic/constants"
cdlerrors "github.com/KJHJason/Cultured-Downloader-Logic/errors"
"github.com/KJHJason/Cultured-Downloader-Logic/gdrive"
"github.com/KJHJason/Cultured-Downloader-Logic/httpfuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/iofuncs"
)
func dlImagesFromPost(content *FantiaContent, postFolderPath string) []*httpfuncs.ToDownload {
// for images that are embedded in the post content
comment := content.Comment
matchedStr := constants.FANTIA_IMAGE_URL_REGEX.FindAllStringSubmatch(comment, -1)
urlsSlice := make([]*httpfuncs.ToDownload, 0, len(matchedStr))
for _, matched := range matchedStr {
imageUrl := constants.FANTIA_URL + matched[constants.FANTIA_REGEX_URL_INDEX]
urlsSlice = append(urlsSlice, &httpfuncs.ToDownload{
Url: imageUrl,
FilePath: filepath.Join(postFolderPath, constants.IMAGES_FOLDER),
})
}
// download images that are uploaded to their own section
postContentPhotos := content.PostContentPhotos
for _, image := range postContentPhotos {
imageUrl := image.URL.Original
urlsSlice = append(urlsSlice, &httpfuncs.ToDownload{
Url: imageUrl,
FilePath: filepath.Join(postFolderPath, constants.IMAGES_FOLDER),
})
}
return urlsSlice
}
func dlAttachmentsFromPost(content *FantiaContent, postFolderPath string) []*httpfuncs.ToDownload {
var urlsSlice []*httpfuncs.ToDownload
// get the attachment url string if it exists
attachmentUrl := content.AttachmentURI
if attachmentUrl != "" {
attachmentUrlStr := constants.FANTIA_URL + attachmentUrl
urlsSlice = append(urlsSlice, &httpfuncs.ToDownload{
Url: attachmentUrlStr,
FilePath: filepath.Join(postFolderPath, constants.ATTACHMENT_FOLDER),
})
} else if content.DownloadUri != "" {
// if the attachment url string does not exist,
// then get the download url for the file
downloadUrl := constants.FANTIA_URL + content.DownloadUri
filename := content.Filename
urlsSlice = append(urlsSlice, &httpfuncs.ToDownload{
Url: downloadUrl,
FilePath: filepath.Join(postFolderPath, constants.ATTACHMENT_FOLDER, filename),
})
}
return urlsSlice
}
// Process the JSON response from Fantia's API and
// returns a slice of urls and a slice of gdrive urls to download from
func processFantiaPost(res *http.Response, dlOptions *FantiaDlOptions) ([]*httpfuncs.ToDownload, []*httpfuncs.ToDownload, error) {
// processes a fantia post
// returns a map containing the post id and the url to download the file from
var postJson FantiaPost
if err := httpfuncs.LoadJsonFromResponse(res, &postJson); err != nil {
return nil, nil, err
}
if postJson.Redirect != "" {
if postJson.Redirect != "/recaptcha" {
return nil, nil, fmt.Errorf(
"fantia error %d: unknown redirect url, %q",
cdlerrors.UNEXPECTED_ERROR,
postJson.Redirect,
)
}
return nil, nil, cdlerrors.ErrRecaptcha
}
post := postJson.Post
postId := strconv.Itoa(post.ID)
postTitle := post.Title
creatorName := post.Fanclub.User.Name
postFolderPath := iofuncs.GetPostFolder(dlOptions.BaseDownloadDirPath, creatorName, postId, postTitle)
var urlsSlice []*httpfuncs.ToDownload
thumbnail := post.Thumb.Original
if dlOptions.DlThumbnails && thumbnail != "" {
urlsSlice = append(urlsSlice, &httpfuncs.ToDownload{
Url: thumbnail,
FilePath: postFolderPath,
})
}
gdriveLinks := gdrive.ProcessPostText(
post.Comment,
postFolderPath,
dlOptions.DlGdrive,
dlOptions.Configs.LogUrls,
)
postContent := post.PostContents
if postContent == nil {
return urlsSlice, gdriveLinks, nil
}
for _, content := range postContent {
commentGdriveLinks := gdrive.ProcessPostText(
content.Comment,
postFolderPath,
dlOptions.DlGdrive,
dlOptions.Configs.LogUrls,
)
if len(commentGdriveLinks) > 0 {
gdriveLinks = append(gdriveLinks, commentGdriveLinks...)
}
if dlOptions.DlImages {
urlsSlice = append(urlsSlice, dlImagesFromPost(&content, postFolderPath)...)
}
if dlOptions.DlAttachments {
urlsSlice = append(urlsSlice, dlAttachmentsFromPost(&content, postFolderPath)...)
}
}
return urlsSlice, gdriveLinks, nil
}
type processIllustArgs struct {
res *http.Response
postId string
postIdsLen int
msgSuffix string
}
// Process the JSON response to get the urls to download
func processIllustDetailApiRes(illustArgs *processIllustArgs, dlOptions *FantiaDlOptions) ([]*httpfuncs.ToDownload, []*httpfuncs.ToDownload, error) {
progress := dlOptions.MainProgBar
progress.SetToSpinner()
progress.UpdateBaseMsg(
fmt.Sprintf(
"Processing retrieved JSON for post %s from Fantia %s...",
illustArgs.postId,
illustArgs.msgSuffix,
),
)
progress.UpdateSuccessMsg(
fmt.Sprintf(
"Finished processing retrieved JSON for post %s from Fantia %s!",
illustArgs.postId,
illustArgs.msgSuffix,
),
)
progress.UpdateErrorMsg(
fmt.Sprintf(
"Something went wrong while processing retrieved JSON for post %s from Fantia %s.\nPlease refer to the logs for more details.",
illustArgs.postId,
illustArgs.msgSuffix,
),
)
progress.Start()
defer progress.SnapshotTask()
urlsToDownload, gdriveLinks, err := processFantiaPost(
illustArgs.res,
dlOptions,
)
if err != nil {
if errors.Is(err, cdlerrors.ErrRecaptcha) {
progress.UpdateErrorMsg(constants.ERR_RECAPTCHA_STR)
}
progress.Stop(true)
return nil, nil, err
}
progress.Stop(false)
return urlsToDownload, gdriveLinks, nil
}