-
Notifications
You must be signed in to change notification settings - Fork 117
/
OPDS2Parser.swift
267 lines (244 loc) · 9.69 KB
/
OPDS2Parser.swift
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
//
// Copyright 2024 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
import ReadiumShared
public enum OPDS2ParserError: Error {
case invalidJSON
case metadataNotFound
case invalidLink
case missingTitle
case invalidFacet
case invalidGroup
case invalidPublication
case invalidNavigation
}
public class OPDS2Parser: Loggable {
/// Parse an OPDS feed or publication.
/// Feed can only be v2 (JSON).
/// - parameter url: The feed URL
public static func parseURL(url: URL, completion: @escaping (ParseData?, Error?) -> Void) {
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, let response = response else {
completion(nil, error ?? OPDSParserError.documentNotFound)
return
}
do {
let parseData = try self.parse(jsonData: data, url: url, response: response)
completion(parseData, nil)
} catch {
completion(nil, error)
}
}.resume()
}
/// Parse an OPDS feed or publication.
/// Feed can only be v2 (JSON).
/// - parameter jsonData: The json raw data
/// - parameter url: The feed URL
/// - parameter response: The response payload
/// - Returns: The intermediate structure of type ParseData
public static func parse(jsonData: Data, url: URL, response: URLResponse) throws -> ParseData {
var parseData = ParseData(url: url, response: response, version: .OPDS2)
guard let jsonRoot = try? JSONSerialization.jsonObject(with: jsonData, options: []) else {
throw OPDS2ParserError.invalidJSON
}
guard let topLevelDict = jsonRoot as? [String: Any] else {
throw OPDS2ParserError.invalidJSON
}
do {
if topLevelDict["navigation"] == nil,
topLevelDict["groups"] == nil,
topLevelDict["publications"] == nil,
topLevelDict["facets"] == nil
{
// Publication only
parseData.publication = try Publication(json: topLevelDict)
} else {
// Feed
parseData.feed = try parse(feedURL: url, jsonDict: topLevelDict)
}
} catch {
log(.warning, error)
}
return parseData
}
/// Parse an OPDS feed.
/// Feed can only be v2 (JSON).
/// - parameter jsonDict: The json top level dictionary
/// - Returns: The resulting Feed
public static func parse(feedURL: URL, jsonDict: [String: Any]) throws -> Feed {
guard let metadataDict = jsonDict["metadata"] as? [String: Any] else {
throw OPDS2ParserError.metadataNotFound
}
guard let title = metadataDict["title"] as? String else {
throw OPDS2ParserError.missingTitle
}
let feed = Feed(title: title)
parseMetadata(opdsMetadata: feed.metadata, metadataDict: metadataDict)
for (k, v) in jsonDict {
switch k {
case "@context":
switch v {
case let s as String:
feed.context.append(s)
case let sArr as [String]:
feed.context.append(contentsOf: sArr)
default:
continue
}
case "metadata": // Already handled above
continue
case "links":
guard let links = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidLink
}
try parseLinks(feed: feed, feedURL: feedURL, links: links)
case "facets":
guard let facets = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidFacet
}
try parseFacets(feed: feed, feedURL: feedURL, facets: facets)
case "publications":
guard let publications = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidPublication
}
try parsePublications(feed: feed, feedURL: feedURL, publications: publications)
case "navigation":
guard let navLinks = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidNavigation
}
try parseNavigation(feed: feed, feedURL: feedURL, navLinks: navLinks)
case "groups":
guard let groups = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidGroup
}
try parseGroups(feed: feed, feedURL: feedURL, groups: groups)
default:
continue
}
}
return feed
}
static func parseMetadata(opdsMetadata: OpdsMetadata, metadataDict: [String: Any]) {
for (k, v) in metadataDict {
switch k {
case "title":
if let title = v as? String {
opdsMetadata.title = title
}
case "numberOfItems":
opdsMetadata.numberOfItem = v as? Int
case "itemsPerPage":
opdsMetadata.itemsPerPage = v as? Int
case "modified":
if let dateStr = v as? String {
opdsMetadata.modified = dateStr.dateFromISO8601
}
case "@type":
opdsMetadata.rdfType = v as? String
case "currentPage":
opdsMetadata.currentPage = v as? Int
default:
continue
}
}
}
static func parseFacets(feed: Feed, feedURL: URL, facets: [[String: Any]]) throws {
for facetDict in facets {
guard let metadata = facetDict["metadata"] as? [String: Any] else {
throw OPDS2ParserError.invalidFacet
}
guard let title = metadata["title"] as? String else {
throw OPDS2ParserError.invalidFacet
}
let facet = Facet(title: title)
parseMetadata(opdsMetadata: facet.metadata, metadataDict: metadata)
for (k, v) in facetDict {
if k == "links" {
guard let links = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidFacet
}
for linkDict in links {
var link = try Link(json: linkDict)
try link.normalizeHREFs(to: feedURL)
facet.links.append(link)
}
}
}
feed.facets.append(facet)
}
}
static func parseLinks(feed: Feed, feedURL: URL, links: [[String: Any]]) throws {
for linkDict in links {
var link = try Link(json: linkDict)
try link.normalizeHREFs(to: feedURL)
feed.links.append(link)
}
}
static func parsePublications(feed: Feed, feedURL: URL, publications: [[String: Any]]) throws {
for pubDict in publications {
let pub = try Publication(json: pubDict)
feed.publications.append(pub)
}
}
static func parseNavigation(feed: Feed, feedURL: URL, navLinks: [[String: Any]]) throws {
for navDict in navLinks {
var link = try Link(json: navDict)
try link.normalizeHREFs(to: feedURL)
feed.navigation.append(link)
}
}
static func parseGroups(feed: Feed, feedURL: URL, groups: [[String: Any]]) throws {
for groupDict in groups {
guard let metadata = groupDict["metadata"] as? [String: Any] else {
throw OPDS2ParserError.invalidGroup
}
guard let title = metadata["title"] as? String else {
throw OPDS2ParserError.invalidGroup
}
let group = Group(title: title)
parseMetadata(opdsMetadata: group.metadata, metadataDict: metadata)
for (k, v) in groupDict {
switch k {
case "metadata":
// Already handled above
continue
case "links":
guard let links = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidGroup
}
for linkDict in links {
var link = try Link(json: linkDict)
try link.normalizeHREFs(to: feedURL)
group.links.append(link)
}
case "navigation":
guard let links = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidGroup
}
for linkDict in links {
var link = try Link(json: linkDict)
try link.normalizeHREFs(to: feedURL)
group.navigation.append(link)
}
case "publications":
guard let publications = v as? [[String: Any]] else {
throw OPDS2ParserError.invalidGroup
}
for pubDict in publications {
let publication = try Publication(json: pubDict)
group.publications.append(publication)
}
default:
continue
}
}
feed.groups.append(group)
}
}
}
private func hrefNormalizer(_ baseURL: URL?) -> (String) -> (String) {
{ href in URLHelper.getAbsolute(href: href, base: baseURL) ?? href }
}