-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
80 lines (71 loc) · 2.36 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
package pixivmobile
import (
"strconv"
"github.com/KJHJason/Cultured-Downloader-Logic/api/pixiv/ugoira"
"github.com/KJHJason/Cultured-Downloader-Logic/httpfuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/iofuncs"
)
// Process the artwork JSON and returns a slice of map that contains the urls of the images and the file path
func (pixiv *PixivMobile) processArtworkJson(artworkJson *IllustJson) ([]*httpfuncs.ToDownload, *ugoira.Ugoira, error) {
if artworkJson == nil {
return nil, nil, nil
}
artworkId := strconv.Itoa(artworkJson.Id)
artworkTitle := artworkJson.Title
artworkType := artworkJson.Type
artistName := artworkJson.User.Name
artworkFolderPath := iofuncs.GetPostFolder(
pixiv.baseDownloadDirPath, artistName, artworkId, artworkTitle,
)
if artworkType == "ugoira" {
ugoiraInfo, err := pixiv.getUgoiraMetadata(artworkId, artworkFolderPath)
if err != nil {
return nil, nil, err
}
return nil, ugoiraInfo, nil
}
var artworksToDownload []*httpfuncs.ToDownload
singlePageImageUrl := artworkJson.MetaSinglePage.OriginalImageUrl
if singlePageImageUrl != "" {
artworksToDownload = append(artworksToDownload, &httpfuncs.ToDownload{
Url: singlePageImageUrl,
FilePath: artworkFolderPath,
})
} else {
for _, image := range artworkJson.MetaPages {
imageUrl := image.ImageUrls.Original
artworksToDownload = append(artworksToDownload, &httpfuncs.ToDownload{
Url: imageUrl,
FilePath: artworkFolderPath,
})
}
}
return artworksToDownload, nil, nil
}
// The same as the processArtworkJson function but for multiple JSONs at once
// (Those with the "illusts" key which holds a slice of maps containing the artwork JSON)
func (pixiv *PixivMobile) processMultipleArtworkJson(resJson *ArtworksJson) ([]*httpfuncs.ToDownload, []*ugoira.Ugoira, []error) {
if resJson == nil {
return nil, nil, nil
}
artworksMaps := resJson.Illusts
if len(artworksMaps) == 0 {
return nil, nil, nil
}
var errSlice []error
var ugoiraToDl []*ugoira.Ugoira
var artworksToDl []*httpfuncs.ToDownload
for _, artwork := range artworksMaps {
artworks, ugoiraVal, err := pixiv.processArtworkJson(artwork)
if err != nil {
errSlice = append(errSlice, err)
continue
}
if ugoiraVal != nil {
ugoiraToDl = append(ugoiraToDl, ugoiraVal)
continue
}
artworksToDl = append(artworksToDl, artworks...)
}
return artworksToDl, ugoiraToDl, errSlice
}