-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity-book.go
More file actions
400 lines (376 loc) · 10.2 KB
/
entity-book.go
File metadata and controls
400 lines (376 loc) · 10.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package openbd
import (
"bytes"
"encoding/json"
"time"
"github.com/goark/errs"
)
//Book is entity class of book info.
type Book struct {
Onix Onix `json:"onix"`
Hanmoto Hanmoto `json:"hanmoto"`
Summary Summary `json:"summary"`
}
//Onix is entity class of JPRO-onix items
type Onix struct {
RecordReference string //ISBN code (key code)
NotificationType string
ProductIdentifier struct {
ProductIDType string
IDValue string //ISBN ?
}
DescriptiveDetail struct {
ProductComposition string
ProductForm string
Measure []struct {
MeasureType string
Measurement string
MeasureUnitCode string
} `json:",omitempty"`
Collection struct {
CollectionType string
CollectionSequence *struct {
CollectionSequenceType string `json:",omitempty"`
CollectionSequenceTypeName string `json:",omitempty"`
CollectionSequenceNumber string `json:",omitempty"`
} `json:",omitempty"`
TitleDetail *struct {
TitleType string `json:",omitempty"`
TitleElement []struct {
TitleElementLevel string
TitleText struct {
Content string `json:"content"`
CollationKey string `json:"collationkey,omitempty"`
}
} `json:",omitempty"`
} `json:",omitempty"`
}
TitleDetail struct {
TitleType string
TitleElement struct {
TitleElementLevel string
TitleText struct {
Content string `json:"content"`
Collationkey string `json:"collationkey,omitempty"`
}
}
}
Contributor []struct {
SequenceNumber string
ContributorRole []string `json:",omitempty"`
PersonName struct {
Content string `json:"content"`
Collationkey string `json:"collationkey,omitempty"`
}
BiographicalNote string `json:",omitempty"`
} `json:",omitempty"`
Language []struct {
LanguageRole string
LanguageCode string
CountryCode string
} `json:",omitempty"`
Extent []struct {
ExtentType string
ExtentValue string
ExtentUnit string
} `json:",omitempty"`
Subject []struct {
SubjectSchemeIdentifier string
SubjectCode string
SubjectHeadingText string `json:",omitempty"`
} `json:",omitempty"`
Audience []struct {
AudienceCodeType string
AudienceCodeValue string
} `json:",omitempty"`
}
CollateralDetail *struct {
TextContent []struct {
TextType string
ContentAudience string
Text string
} `json:",omitempty"`
SupportingResource []struct {
ResourceContentType string
ContentAudience string
ResourceMode string
ResourceVersion []struct {
ResourceForm string
ResourceVersionFeature []struct {
ResourceVersionFeatureType string
FeatureValue string
} `json:",omitempty"`
ResourceLink string
} `json:",omitempty"`
} `json:",omitempty"`
} `json:",omitempty"`
PublishingDetail struct {
Imprint struct {
ImprintIdentifier []struct {
ImprintIDType string
IDValue string
} `json:",omitempty"`
ImprintName string
}
Publisher struct {
PublisherIdentifier []struct {
PublisherIDType string
IDValue string
} `json:",omitempty"`
PublishingRole string
PublisherName string
}
PublishingDate []struct {
Date Date
PublishingDateRole string
} `json:",omitempty"`
}
ProductSupply struct {
SupplyDetail struct {
ReturnsConditions struct {
ReturnsCodeType string
ReturnsCode string
}
ProductAvailability string
Price []struct {
PriceType string
CurrencyCode string
PriceAmount string
} `json:",omitempty"`
}
}
}
//Hanmoto is entity class of Hanmoto dot com items
type Hanmoto struct {
DatePublished Date `json:"dateshuppan"`
DateModified Date `json:"datemodified"`
DateCreated Date `json:"datecreated"`
DateReleased Date `json:"datekoukai"`
IsLightNovel bool `json:"lanove,omitempty"`
HasReview bool `json:"hasshohyo,omitempty"`
Reviews []struct {
Reviewer string `json:"reviewer"`
Link string `json:"link"`
DateAppearance Date `json:"appearance"`
SourceKindID int `json:"kubun_id"`
SourceID int `json:"source_id"`
Source string `json:"source"`
PaperType string `json:"choyukan"`
PostUser string `json:"post_user"`
Han string `json:"han"`
Gou string `json:"gou"`
} `json:"reviews,omitempty"`
HasSample bool `json:"hastameshiyomi,omitempty"`
}
//Summary is entity class of summary data
type Summary struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Volume string `json:"volume"`
Series string `json:"series"`
Publisher string `json:"publisher"`
PubDate Date `json:"pubdate"`
Author string `json:"author"`
Cover string `json:"cover"`
}
func (book *Book) String() string {
if b, err := EncodeBook(book); err == nil {
return string(b)
}
return ""
}
//Id returns id code (= Book.Onix.RecordReference)
func (book *Book) Id() string {
if book == nil {
return ""
}
return book.Onix.RecordReference
}
//IsValid returns true if Book is valid data
func (book *Book) IsValid() bool {
return len(book.Id()) > 0
}
//ISBN returns ISBN code
func (book *Book) ISBN() string {
if !book.IsValid() {
return ""
}
if len(book.Onix.ProductIdentifier.IDValue) > 0 {
return book.Onix.ProductIdentifier.IDValue
}
if len(book.Summary.ISBN) > 0 {
return book.Summary.ISBN
}
return book.Onix.RecordReference
}
//Title returns string of Book Title
func (book *Book) Title() string {
if !book.IsValid() {
return ""
}
title := ""
if len(book.Onix.DescriptiveDetail.TitleDetail.TitleType) > 0 {
title = book.Onix.DescriptiveDetail.TitleDetail.TitleElement.TitleText.Content
}
if len(title) == 0 {
title = book.Summary.Title
}
return title
}
//SubTitle returns string of sub-title
func (book *Book) SubTitle() string {
if !book.IsValid() {
return ""
}
//TODO: Subtitle
return ""
}
//SeriesTitle returns string of series title
func (book *Book) SeriesTitle() string {
if !book.IsValid() {
return ""
}
if book.Onix.DescriptiveDetail.Collection.TitleDetail != nil {
for _, elm := range book.Onix.DescriptiveDetail.Collection.TitleDetail.TitleElement {
if elm.TitleElementLevel == "01" && len(elm.TitleText.Content) > 0 {
return elm.TitleText.Content
}
}
}
return book.Summary.Series
}
//Label returns string of book label
func (book *Book) Label() string {
if !book.IsValid() {
return ""
}
if book.Onix.DescriptiveDetail.Collection.TitleDetail != nil {
for _, elm := range book.Onix.DescriptiveDetail.Collection.TitleDetail.TitleElement {
if elm.TitleElementLevel == "02" && len(elm.TitleText.Content) > 0 {
return elm.TitleText.Content
}
}
}
return ""
}
//ImageURL returns string of book cover image URL
func (book *Book) ImageURL() string {
if !book.IsValid() {
return ""
}
if book.Onix.CollateralDetail != nil {
for _, elm := range book.Onix.CollateralDetail.SupportingResource {
if elm.ResourceContentType == "01" {
for _, v := range elm.ResourceVersion {
if v.ResourceForm == "02" && len(v.ResourceLink) > 0 {
return v.ResourceLink
}
}
}
}
}
return book.Summary.Cover
}
//Authors returns strings of Contributors
func (book *Book) Authors() []string {
authors := []string{}
if !book.IsValid() {
return authors
}
for _, elm := range book.Onix.DescriptiveDetail.Contributor {
if len(elm.PersonName.Content) > 0 {
authors = append(authors, elm.PersonName.Content)
}
}
if len(authors) > 0 {
return authors
}
return []string{book.Summary.Author}
}
//Publisher returns strings of Publisher
func (book *Book) Publisher() string {
if !book.IsValid() {
return ""
}
pub := book.Onix.PublishingDetail.Imprint.ImprintName
if len(pub) == 0 {
pub = book.Onix.PublishingDetail.Publisher.PublisherName
}
if len(pub) == 0 {
pub = book.Summary.Publisher
}
return pub
}
//PublicationDate returns Date of Publication
func (book *Book) PublicationDate() Date {
if !book.IsValid() {
return NewDate(time.Time{})
}
for _, pubdate := range book.Onix.PublishingDetail.PublishingDate {
if pubdate.PublishingDateRole == "01" && !pubdate.Date.IsZero() {
return pubdate.Date
}
}
if !book.Hanmoto.DatePublished.IsZero() {
return book.Hanmoto.DatePublished
}
return book.Summary.PubDate
}
//Description returns strings of book description or table of content
func (book *Book) Description() string {
if !book.IsValid() {
return ""
}
desc := ""
if book.Onix.CollateralDetail != nil {
for _, content := range book.Onix.CollateralDetail.TextContent {
switch content.TextType {
case "02", "04": //brief or table of content
if len(desc) == 0 {
desc = content.Text
}
case "03": //description
desc = content.Text
}
}
}
return desc
}
//DecodeBook returns Book instance from byte buffer
func DecodeBook(b []byte) (*Book, error) {
book := Book{}
if err := json.NewDecoder(bytes.NewReader(b)).Decode(&book); err != nil {
return &book, errs.New("error in DecodeBook() function", errs.WithCause(err))
}
return &book, nil
}
//DecodeBooks returns array of Book instance from byte buffer
func DecodeBooks(b []byte) ([]Book, error) {
var books []Book
if err := json.NewDecoder(bytes.NewReader(b)).Decode(&books); err != nil {
return books, errs.New("error in DecodeBooks() function", errs.WithCause(err))
}
return books, nil
}
//EncodeBook returns bytes encoded from Book instance
func EncodeBook(book *Book) ([]byte, error) {
return json.Marshal(book)
}
//EncodeBooks returns bytes encoded from list of Book
func EncodeBooks(books []Book) ([]byte, error) {
return json.Marshal(books)
}
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/