-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdfunmarshal.go
249 lines (230 loc) · 7.5 KB
/
rdfunmarshal.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
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
package pgrdf
import (
"io"
"strings"
"github.com/mrcook/pgrdf/internal/unmarshaler"
)
// rdfUnmarshal will deserialise an RDF object to an Ebook object.
func rdfUnmarshal(r io.Reader) (*Ebook, error) {
rdf, err := unmarshaler.New(r)
if err != nil {
return nil, err
}
ebook := &Ebook{
ID: rdf.Ebook.Id(),
Titles: splitTitles(rdf.Ebook.Titles),
AlternateTitles: splitTitles(rdf.Ebook.Alternatives),
TableOfContents: rdf.Ebook.TableOfContents,
Publisher: rdf.Ebook.Publisher,
PublishedYear: rdf.Ebook.PublishedYear,
ReleaseDate: rdf.Ebook.Issued.Value,
Summary: rdf.Ebook.Summary,
Series: rdf.Ebook.Series,
Languages: nil,
LanguageDialect: rdf.Ebook.LanguageDialect,
LanguageNotes: rdf.Ebook.LanguageNotes,
PublicationNote: rdf.Ebook.PublicationNote,
EditionNote: rdf.Ebook.EditionNote,
ProductionNotes: rdf.Ebook.ProductionNotes,
Copyright: rdf.Ebook.Rights,
CopyrightClearanceCode: rdf.Ebook.DpClearanceCode,
BookType: "",
Notes: rdf.Ebook.Descriptions,
PhysicalDescriptionNote: rdf.Ebook.PhysicalDescriptionNote,
SourceLinks: rdf.Ebook.SourceLinks,
LCCN: rdf.Ebook.LCCN,
ISBN: rdf.Ebook.ISBN,
BookCovers: nil,
TitlePageImage: rdf.Ebook.TitlePageImage,
BackCover: rdf.Ebook.BackCoverImage,
Creators: nil,
Subjects: nil,
Files: nil,
Bookshelves: nil,
Downloads: rdf.Ebook.Downloads.Value,
AuthorLinks: nil,
CCComment: rdf.Work.Comment,
CCLicense: rdf.Work.License.Resource,
}
if rdf.Ebook.Type.Description.Value != nil {
ebook.SetBookType(rdf.Ebook.Type.Description.Value.Data)
}
for _, cover := range rdf.Ebook.BookCoverImages {
ebook.BookCovers = append(ebook.BookCovers, bookCoverFilename(cover))
}
for _, lang := range rdf.Ebook.Languages {
// NOTE: this should never happen, but let's check for nil anyway
if lang.Description.Value != nil {
ebook.Languages = append(ebook.Languages, lang.Description.Value.Data)
}
}
for _, l := range rdf.Descriptions {
ebook.AddAuthorLink(l.Description, l.About)
}
for _, c := range rdf.Ebook.Creators {
creator := Creator{
ID: c.AgentId(),
Name: c.Agent.Name,
Aliases: c.Agent.Aliases,
Role: RoleAut,
}
if c.Agent.BirthYear != nil {
creator.Born = c.Agent.BirthYear.Value
}
if c.Agent.DeathYear != nil {
creator.Died = c.Agent.DeathYear.Value
}
for _, webpage := range c.Agent.Webpages {
creator.WebPages = append(creator.WebPages, webpage.Resource)
}
ebook.AddCreator(creator)
}
for _, rel := range rdf.Ebook.RelAdapters {
addRelatorToCreators(ebook, rel, RoleAdp)
}
for _, rel := range rdf.Ebook.RelAfterwords {
addRelatorToCreators(ebook, rel, RoleAft)
}
for _, rel := range rdf.Ebook.RelAnnotators {
addRelatorToCreators(ebook, rel, RoleAnn)
}
for _, rel := range rdf.Ebook.RelArrangers {
addRelatorToCreators(ebook, rel, RoleArr)
}
for _, rel := range rdf.Ebook.RelArtists {
addRelatorToCreators(ebook, rel, RoleArt)
}
for _, rel := range rdf.Ebook.RelIntroductions {
addRelatorToCreators(ebook, rel, RoleAui)
}
for _, rel := range rdf.Ebook.RelCommentators {
addRelatorToCreators(ebook, rel, RoleCmm)
}
for _, rel := range rdf.Ebook.RelComposers {
addRelatorToCreators(ebook, rel, RoleCmp)
}
for _, rel := range rdf.Ebook.RelConductors {
addRelatorToCreators(ebook, rel, RoleCnd)
}
for _, rel := range rdf.Ebook.RelCompilers {
addRelatorToCreators(ebook, rel, RoleCom)
}
for _, rel := range rdf.Ebook.RelContributors {
addRelatorToCreators(ebook, rel, RoleCtb)
}
for _, rel := range rdf.Ebook.RelDubious {
addRelatorToCreators(ebook, rel, RoleDub)
}
for _, rel := range rdf.Ebook.RelEditors {
addRelatorToCreators(ebook, rel, RoleEdt)
}
for _, rel := range rdf.Ebook.RelEngravers {
addRelatorToCreators(ebook, rel, RoleEgr)
}
for _, rel := range rdf.Ebook.RelIllustrators {
addRelatorToCreators(ebook, rel, RoleIll)
}
for _, rel := range rdf.Ebook.RelLibrettists {
addRelatorToCreators(ebook, rel, RoleLbt)
}
for _, rel := range rdf.Ebook.RelOther {
addRelatorToCreators(ebook, rel, RoleOth)
}
for _, rel := range rdf.Ebook.RelPublishers {
addRelatorToCreators(ebook, rel, RolePbl)
}
for _, rel := range rdf.Ebook.RelPhotographers {
addRelatorToCreators(ebook, rel, RolePht)
}
for _, rel := range rdf.Ebook.RelPerformers {
addRelatorToCreators(ebook, rel, RolePrf)
}
for _, rel := range rdf.Ebook.RelPrinters {
addRelatorToCreators(ebook, rel, RolePrt)
}
for _, rel := range rdf.Ebook.RelResearchers {
addRelatorToCreators(ebook, rel, RoleRes)
}
for _, rel := range rdf.Ebook.RelTranscribers {
addRelatorToCreators(ebook, rel, RoleTrc)
}
for _, rel := range rdf.Ebook.RelTranslators {
addRelatorToCreators(ebook, rel, RoleTrl)
}
// NOTE: this should be deprecated
for _, rel := range rdf.Ebook.RelCollaborators {
addRelatorToCreators(ebook, rel, RoleClb)
}
for _, rel := range rdf.Ebook.RelUnknown {
addRelatorToCreators(ebook, rel, RoleUnk)
}
for _, s := range rdf.Ebook.Subjects {
if s.Description.Value == nil || s.Description.MemberOf == nil {
continue // NOTE: this should never happen, but let's check for nil anyway
}
ebook.AddSubject(s.Description.Value.Data, s.Description.MemberOf.Resource)
}
for _, f := range rdf.Ebook.HasFormats {
file := File{
URL: f.File.About,
Extent: f.File.Extent.Value,
Modified: f.File.Modified.Value,
}
for _, f := range f.File.Formats {
if f.Description.Value == nil {
continue // NOTE: this should never happen, but let's check for nil anyway
}
file.AddEncoding(f.Description.Value.Data)
}
ebook.AddBookFile(file)
}
for _, s := range rdf.Ebook.Bookshelves {
if s.Description.Value == nil || s.Description.MemberOf == nil {
continue // NOTE: this should never happen, but let's check for nil anyway
}
ebook.AddBookshelf(s.Description.Value.Data, s.Description.MemberOf.Resource)
}
return ebook, nil
}
func splitTitles(titles []string) []string {
var newTitles []string
for _, title := range titles {
title = strings.ReplaceAll(title, "\r", "\n")
for _, t := range strings.Split(title, "\n") {
t = strings.TrimSpace(t)
if len(t) > 0 {
newTitles = append(newTitles, t)
}
}
}
return newTitles
}
// Extract the book cover filename from the file path.
// marc901 tags contain a book cover filename from the HTML version of the ebook.
func bookCoverFilename(cover string) string {
parts := strings.Split(cover, "-h")
cover = parts[len(parts)-1]
cover = strings.TrimPrefix(cover, "/")
return cover
}
// addRelatorToCreators appends a MARC relator to the creators list with the given role and agent (if present).
func addRelatorToCreators(e *Ebook, relator unmarshaler.MarcRelator, role MarcRelator) {
creator := Creator{
ID: relator.AgentId(),
Role: role,
}
if relator.Agent != nil {
creator.Name = relator.Agent.Name
creator.Aliases = relator.Agent.Aliases
if relator.Agent.BirthYear != nil {
creator.Born = relator.Agent.BirthYear.Value
}
if relator.Agent.DeathYear != nil {
creator.Died = relator.Agent.DeathYear.Value
}
for _, webpage := range relator.Agent.Webpages {
creator.WebPages = append(creator.WebPages, webpage.Resource)
}
}
e.AddCreator(creator)
}