/
artwork.go
121 lines (111 loc) · 3.13 KB
/
artwork.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
package artwork
import (
"context"
"errors"
"net/url"
"time"
"github.com/NateScarlet/pixiv/pkg/client"
"github.com/NateScarlet/pixiv/pkg/image"
"github.com/NateScarlet/pixiv/pkg/user"
"github.com/tidwall/gjson"
)
// Page is a artwork page.
type Page struct {
Image image.URLs
Width int64
Height int64
}
// Artwork data
type Artwork struct {
ID string
Title string
Type string
Description string
Image image.URLs
Created time.Time
Tags []string
Author user.User
Width int64
Height int64
PageCount int64
CommentCount int64
LikeCount int64
ViewCount int64
BookmarkCount int64
Pages []Page
}
// Fetch additional data from pixiv single artwork api,
func (i *Artwork) Fetch(ctx context.Context) (err error) {
if i.ID == "" {
return errors.New("pixiv: artwork: no id specified")
}
var c = client.For(ctx)
resp, err := c.GetWithContext(ctx, c.EndpointURL("/ajax/illust/"+i.ID, nil).String())
if err != nil {
return
}
defer resp.Body.Close()
body, err := client.ParseAPIResult(resp.Body)
if err != nil {
return
}
i.Title = body.Get("illustTitle").String()
i.Type = body.Get("illustType").String()
i.Description = body.Get("description").String()
i.Image.Mini = body.Get("urls.mini").String()
i.Image.Thumb = body.Get("urls.thumb").String()
i.Image.Small = body.Get("urls.small").String()
i.Image.Regular = body.Get("urls.regular").String()
i.Image.Original = body.Get("urls.original").String()
i.Created = body.Get("createDate").Time()
i.Author.ID = body.Get("userId").String()
i.Author.Name = body.Get("userName").String()
i.Width = body.Get("width").Int()
i.Height = body.Get("height").Int()
i.PageCount = body.Get("pageCount").Int()
i.CommentCount = body.Get("commentCount").Int()
i.LikeCount = body.Get("likeCount").Int()
i.ViewCount = body.Get("viewCount").Int()
i.BookmarkCount = body.Get("bookmarkCount").Int()
tags := []string{}
for _, i := range body.Get("tags.tags.#.tag").Array() {
tags = append(tags, i.String())
}
i.Tags = tags
return
}
// FetchPages get all pages for artwork from pixiv artwork pages api,
func (i *Artwork) FetchPages(ctx context.Context) (err error) {
if i.ID == "" {
return errors.New("pixiv: artwork: no id specified")
}
var c = client.For(ctx)
resp, err := c.GetWithContext(ctx, c.EndpointURL("/ajax/illust/"+i.ID+"/pages", nil).String())
if err != nil {
return
}
defer resp.Body.Close()
data, err := client.ParseAPIResult(resp.Body)
if err != nil {
return
}
pages := make([]Page, 0, int(data.Get("#").Int()))
data.ForEach(func(key, value gjson.Result) bool {
i := Page{}
i.Image.Mini = value.Get("urls.thumb_mini").String()
i.Image.Thumb = i.Image.Mini
i.Image.Small = value.Get("urls.small").String()
i.Image.Regular = value.Get("urls.regular").String()
i.Image.Original = value.Get("urls.original").String()
i.Width = value.Get("width").Int()
i.Height = value.Get("height").Int()
pages = append(pages, i)
return true
})
i.Pages = pages
return
}
// URL to view artwork web page.
func (i Artwork) URL(ctx context.Context) *url.URL {
return client.For(ctx).EndpointURL("/artworks/"+i.ID, nil)
}