-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthor.go
More file actions
315 lines (263 loc) · 8.47 KB
/
Copy pathauthor.go
File metadata and controls
315 lines (263 loc) · 8.47 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package openlibrary
import (
"errors"
"net/http"
"path"
)
type AuthorPictureKind string
const (
AuthorByID AuthorPictureKind = "id"
AuthorByOLID AuthorPictureKind = "olid"
)
type AuthorSize string
const (
AuthorPictureSmall AuthorSize = "S"
AuthorPictureMedium AuthorSize = "M"
AuthorPictureLarge AuthorSize = "L"
)
type AuthorsAPI struct {
openlibraryClient *Client
}
type Author struct {
Key string `json:"key"`
Name string `json:"name"`
Type string `json:"type"`
AlternateNames []string `json:"alternate_names,omitempty"`
Bio *TextBlock `json:"bio,omitempty"`
BirthDate *string `json:"birth_date,omitempty"`
DeathDate *string `json:"death_date,omitempty"`
Date *string `json:"date,omitempty"`
EntityType *string `json:"entity_type,omitempty"`
FullerName *string `json:"fuller_name,omitempty"`
PersonalName *string `json:"personal_name,omitempty"`
Title *string `json:"title,omitempty"`
Photos []int64 `json:"photos,omitempty"`
Links []Link `json:"links,omitempty"`
RemoteIDs *RemoteIDs `json:"remote_ids,omitempty"`
Revision int64 `json:"revision"`
LatestRevision *int64 `json:"latest_revision,omitempty"`
Created *InternalDateTime `json:"created,omitempty"`
LastModified InternalDateTime `json:"last_modified"`
}
type AuthorType struct {
Key string `json:"key"`
}
type RemoteIDs struct {
Wikidata *string `json:"wikidata,omitempty"`
Viaf *string `json:"viaf,omitempty"`
}
type AuthorSearch struct {
openlibraryClient *Client
query string
}
type AuthorSearchResult struct {
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
AlternateNames []string `json:"alternate_names,omitempty"`
BirthDate string `json:"birth_date,omitempty"`
DeathDate string `json:"death_date,omitempty"`
Date string `json:"date,omitempty"`
WorkCount int `json:"work_count,omitempty"`
TopWork string `json:"top_work,omitempty"`
TopSubjects []string `json:"top_subjects,omitempty"`
Type string `json:"type,omitempty"`
RatingsAverage float64 `json:"ratings_average,omitempty"`
RatingsSortable float64 `json:"ratings_sortable,omitempty"`
RatingsCount int `json:"ratings_count,omitempty"`
RatingsCount1 int `json:"ratings_count_1,omitempty"`
RatingsCount2 int `json:"ratings_count_2,omitempty"`
RatingsCount3 int `json:"ratings_count_3,omitempty"`
RatingsCount4 int `json:"ratings_count_4,omitempty"`
RatingsCount5 int `json:"ratings_count_5,omitempty"`
WantToReadCount int `json:"want_to_read_count,omitempty"`
AlreadyReadCount int `json:"already_read_count,omitempty"`
CurrentlyReadingCount int `json:"currently_reading_count,omitempty"`
ReadinglogCount int `json:"readinglog_count,omitempty"`
Version int64 `json:"_version_,omitempty"`
}
type AuthorSearchResponse struct {
NumFound int `json:"numFound,omitempty"`
Start int `json:"start,omitempty"`
NumFoundExact bool `json:"numFoundExact,omitempty"`
Docs []AuthorSearchResult `json:"docs,omitempty"`
}
func (c *Client) Authors() *AuthorsAPI {
api := &AuthorsAPI{
openlibraryClient: c,
}
return api
}
func (api *AuthorsAPI) Query(author string) *AuthorSearch {
return &AuthorSearch{
openlibraryClient: api.openlibraryClient,
query: author,
}
}
func (api *AuthorSearch) Search() (resp *AuthorSearchResponse, err error) {
_, err = api.openlibraryClient.httpClient.
SetBaseURL(baseURL).R().
SetHeader("Accept", "application/json").
SetQueryParam("q", api.query).
SetResult(&resp).
Get("/search/authors")
if err != nil {
return
}
return
}
type AuthorAPI struct {
openlibraryClient *Client
identifier string
data *Author
}
func (api *AuthorsAPI) ByIdentifier(identifier string) *AuthorAPI {
return &AuthorAPI{
openlibraryClient: api.openlibraryClient,
identifier: identifier,
}
}
func (api *AuthorAPI) Fetch() (err error) {
_, err = api.openlibraryClient.httpClient.R().
SetHeader("Accept", "application/json").
SetResult(&api.data).
Get(api.identifier + ".json")
if err != nil {
return
}
return
}
func (api *AuthorAPI) Data() (author *Author) {
return api.data
}
func (api *AuthorAPI) Photo() (photoAPI *AuthorPhotoAPI) {
if api.data == nil {
return
}
_, value := path.Split(api.data.Key)
return &AuthorPhotoAPI{
openlibraryClient: api.openlibraryClient,
kind: AuthorByOLID,
size: AuthorPictureMedium,
value: value,
}
}
type AuthorPhotoAPI struct {
openlibraryClient *Client
value string
kind AuthorPictureKind
size AuthorSize
}
func (api *Author) Photo() (photoAPI *AuthorPhotoAPI) {
_, value := path.Split(api.Key)
return &AuthorPhotoAPI{
openlibraryClient: NewClient(),
kind: AuthorByOLID,
size: AuthorPictureMedium,
value: value,
}
}
func (api *AuthorSearchResult) Photo() (photoAPI *AuthorPhotoAPI) {
_, value := path.Split(api.Key)
return &AuthorPhotoAPI{
openlibraryClient: NewClient(),
kind: AuthorByOLID,
size: AuthorPictureMedium,
value: value,
}
}
func (api *AuthorsAPI) Photo() *AuthorPhotoAPI {
return &AuthorPhotoAPI{
openlibraryClient: api.openlibraryClient,
kind: AuthorByID,
size: AuthorPictureMedium,
}
}
func (api *AuthorPhotoAPI) ID(id string) *AuthorPhotoAPI {
api.value = id
api.kind = AuthorByID
return api
}
func (api *AuthorPhotoAPI) OLID(olid string) *AuthorPhotoAPI {
api.value = olid
api.kind = AuthorByOLID
return api
}
func (api *AuthorPhotoAPI) Small() *AuthorPhotoAPI {
api.size = AuthorPictureSmall
return api
}
func (api *AuthorPhotoAPI) Medium() *AuthorPhotoAPI {
api.size = AuthorPictureMedium
return api
}
func (api *AuthorPhotoAPI) Large() *AuthorPhotoAPI {
api.size = AuthorPictureLarge
return api
}
func (api *AuthorPhotoAPI) Get() (imgBytes []byte, mimeType string, err error) {
if api.value == "" {
return imgBytes, mimeType, errors.New("No value")
}
endpoint := path.Join("/a", string(api.kind), api.value+"-"+string(api.size)) + ".jpg?default=false"
res, err := api.openlibraryClient.httpClient.SetBaseURL(coverBaseURL).R().Get(endpoint)
if err != nil {
return
}
if res.StatusCode() != 200 {
return
}
imgBytes = res.Bytes()
mimeType = http.DetectContentType(imgBytes)
return
}
type AuthorWorksAPI struct {
openlibraryClient *Client
authorKey string
limit int
offset int
}
func (api *Author) Works() (worksAPI *AuthorWorksAPI) {
_, authorKey := path.Split(api.Key)
return &AuthorWorksAPI{
openlibraryClient: NewClient(),
authorKey: authorKey,
limit: 10,
offset: 0,
}
}
func (api *AuthorSearchResult) Works() (worksAPI *AuthorWorksAPI) {
_, authorKey := path.Split(api.Key)
return &AuthorWorksAPI{
openlibraryClient: NewClient(),
authorKey: authorKey,
limit: 10,
offset: 0,
}
}
func (api *AuthorWorksAPI) Limit(limit int) *AuthorWorksAPI {
api.limit = limit
return api
}
func (api *AuthorWorksAPI) Offset(offset int) *AuthorWorksAPI {
api.offset = offset
return api
}
type AuthorWorksResponse struct {
Links struct {
Self string `json:"self,omitempty"`
Work string `json:"work,omitempty"`
Next string `json:"next,omitempty"`
} `json:"links"`
Size int `json:"size,omitempty"`
Entries []Works `json:"entries"`
}
func (api *AuthorWorksAPI) Fetch() (resp *AuthorWorksResponse, err error) {
_, err = api.openlibraryClient.httpClient.R().
SetHeader("Accept", "application/json").
SetResult(&resp).
Get(path.Join("/authors", api.authorKey, "works.json"))
if err != nil {
return
}
return
}