diff options
Diffstat (limited to 'Sora')
| -rw-r--r-- | Sora/Data/Booru/Post/BooruPostXMLParser.swift | 167 |
1 files changed, 56 insertions, 111 deletions
diff --git a/Sora/Data/Booru/Post/BooruPostXMLParser.swift b/Sora/Data/Booru/Post/BooruPostXMLParser.swift index 96aa23a..377bc3e 100644 --- a/Sora/Data/Booru/Post/BooruPostXMLParser.swift +++ b/Sora/Data/Booru/Post/BooruPostXMLParser.swift @@ -24,6 +24,58 @@ class BooruPostXMLParser: NSObject, XMLParserDelegate { return posts } + private func makePost(from dict: [String: String]) -> BooruPost? { + guard let id = dict["id"], + let heightStr = dict["height"], let height = Int(heightStr), + let score = dict["score"], + let fileUrlStr = dict["file_url"], let fileUrl = URL(string: fileUrlStr), + let parentId = dict["parent_id"], + let sampleUrlStr = dict["sample_url"], let sampleUrl = URL(string: sampleUrlStr), + let sampleWidthStr = dict["sample_width"], let sampleWidth = Int(sampleWidthStr), + let sampleHeightStr = dict["sample_height"], let sampleHeight = Int(sampleHeightStr), + let previewUrlStr = dict["preview_url"], let previewUrl = URL(string: previewUrlStr), + let rating = dict["rating"], + let tagsString = dict["tags"], + let widthStr = dict["width"], let width = Int(widthStr), + let change = dict["change"], + let md5 = dict["md5"], + let creatorId = dict["creator_id"], + let createdAtStr = dict["created_at"], + let createdAt = parseCreatedAt(createdAtStr), + let status = dict["status"], + let source = dict["source"], + let previewWidthStr = dict["preview_width"], let previewWidth = Int(previewWidthStr), + let previewHeightStr = dict["preview_height"], let previewHeight = Int(previewHeightStr) + else { + return nil + } + + return BooruPost( + id: id, + height: height, + score: score, + fileURL: fileUrl, + parentID: parentId, + sampleURL: sampleUrl, + sampleWidth: sampleWidth, + sampleHeight: sampleHeight, + previewURL: previewUrl, + rating: BooruRating(rating), + tags: tagsString.components(separatedBy: CharacterSet.whitespacesAndNewlines) + .filter { !$0.isEmpty }, + width: width, + change: change, + md5: md5, + creatorID: creatorId, + authorID: dict["author_id"], + createdAt: createdAt, + status: status, + source: source, + previewWidth: previewWidth, + previewHeight: previewHeight + ) + } + func parser( _ parser: XMLParser, // swiftlint:disable:this unused_parameter didStartElement elementName: String, @@ -40,59 +92,9 @@ class BooruPostXMLParser: NSObject, XMLParserDelegate { } } else { if elementName == "post" { - guard let id = attributeDict["id"], - let heightStr = attributeDict["height"], - let height = Int(heightStr), - let score = attributeDict["score"], - let fileUrl = attributeDict["file_url"], - let parentId = attributeDict["parent_id"], - let sampleUrl = attributeDict["sample_url"], - let sampleWidthStr = attributeDict["sample_width"], - let sampleWidth = Int(sampleWidthStr), - let sampleHeightStr = attributeDict["sample_height"], - let sampleHeight = Int(sampleHeightStr), - let previewUrl = attributeDict["preview_url"], - let rating = attributeDict["rating"], - let tags = attributeDict["tags"], - let widthStr = attributeDict["width"], - let width = Int(widthStr), - let change = attributeDict["change"], - let md5 = attributeDict["md5"], - let creatorId = attributeDict["creator_id"], - let createdAt = attributeDict["created_at"], - let status = attributeDict["status"], - let source = attributeDict["source"], - let previewWidthStr = attributeDict["preview_width"], - let previewWidth = Int(previewWidthStr), - let previewHeightStr = attributeDict["preview_height"], - let previewHeight = Int(previewHeightStr) - else { - return + if let post = makePost(from: attributeDict) { + currentPost = post } - - currentPost = BooruPost( - id: id, - height: height, - score: score, - fileURL: URL(string: fileUrl)!, - parentID: parentId, - sampleURL: URL(string: sampleUrl)!, - sampleWidth: sampleWidth, - sampleHeight: sampleHeight, - previewURL: URL(string: previewUrl)!, - rating: BooruRating(rating), - tags: tags.components(separatedBy: " ").filter { !$0.isEmpty }, - width: width, - change: change, - md5: md5, - creatorID: creatorId, - authorID: attributeDict["author_id"], - createdAt: parseCreatedAt(createdAt)!, - status: status, - source: source, - previewWidth: previewWidth, - previewHeight: previewHeight - ) } } } @@ -114,66 +116,9 @@ class BooruPostXMLParser: NSObject, XMLParserDelegate { ) { if provider == .gelbooru { if elementName == "post" { - guard let id = currentPostData["id"], - let heightStr = currentPostData["height"], - let height = Int(heightStr), - let score = currentPostData["score"], - let fileUrlStr = currentPostData["file_url"], - let fileUrl = URL(string: fileUrlStr), - let parentId = currentPostData["parent_id"], - let sampleUrlStr = currentPostData["sample_url"], - let sampleUrl = URL(string: sampleUrlStr), - let sampleWidthStr = currentPostData["sample_width"], - let sampleWidth = Int(sampleWidthStr), - let sampleHeightStr = currentPostData["sample_height"], - let sampleHeight = Int(sampleHeightStr), - let previewUrlStr = currentPostData["preview_url"], - let previewUrl = URL(string: previewUrlStr), - let rating = currentPostData["rating"], - let tagsString = currentPostData["tags"], - let widthStr = currentPostData["width"], - let width = Int(widthStr), - let change = currentPostData["change"], - let md5 = currentPostData["md5"], - let creatorId = currentPostData["creator_id"], - let createdAtStr = currentPostData["created_at"], - let createdAt = parseCreatedAt(createdAtStr), - let status = currentPostData["status"], - let source = currentPostData["source"], - let previewWidthStr = currentPostData["preview_width"], - let previewWidth = Int(previewWidthStr), - let previewHeightStr = currentPostData["preview_height"], - let previewHeight = Int(previewHeightStr) - else { - return + if let post = makePost(from: currentPostData) { + posts.append(post) } - - posts.append( - BooruPost( - id: id, - height: height, - score: score, - fileURL: fileUrl, - parentID: parentId, - sampleURL: sampleUrl, - sampleWidth: sampleWidth, - sampleHeight: sampleHeight, - previewURL: previewUrl, - rating: BooruRating(rating), - tags: tagsString.components(separatedBy: CharacterSet.whitespacesAndNewlines) - .filter { !$0.isEmpty }, - width: width, - change: change, - md5: md5, - creatorID: creatorId, - authorID: currentPostData["author_id"], - createdAt: createdAt, - status: status, - source: source, - previewWidth: previewWidth, - previewHeight: previewHeight - ) - ) } else if let currentElement = currentElementName { currentPostData[currentElement] = currentText.trimmingCharacters( in: .whitespacesAndNewlines |