diff options
| author | Fuwn <[email protected]> | 2025-02-17 19:59:40 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-02-17 19:59:40 -0800 |
| commit | 4b03f632491b2f988f1acf07a257cc23ef4f395b (patch) | |
| tree | b9f5093fdfbdf93d19266463a608d892776803b5 /Sora/Data/MoebooruXMLParser.swift | |
| download | sora-testing-4b03f632491b2f988f1acf07a257cc23ef4f395b.tar.xz sora-testing-4b03f632491b2f988f1acf07a257cc23ef4f395b.zip | |
feat: Initial commit
Diffstat (limited to 'Sora/Data/MoebooruXMLParser.swift')
| -rw-r--r-- | Sora/Data/MoebooruXMLParser.swift | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Sora/Data/MoebooruXMLParser.swift b/Sora/Data/MoebooruXMLParser.swift new file mode 100644 index 0000000..1053299 --- /dev/null +++ b/Sora/Data/MoebooruXMLParser.swift @@ -0,0 +1,49 @@ +import Foundation + +class MoebooruXMLParser: NSObject, XMLParserDelegate { + private var posts: [MoebooruPost] = [] + private var currentAttributes: [String: String] = [:] + private var currentPost: MoebooruPost? + + func parse(data: Data) -> [MoebooruPost] { + let parser = XMLParser(data: data) + + parser.delegate = self + parser.parse() + + return posts + } + + func parser(_: XMLParser, didStartElement elementName: String, + namespaceURI _: String?, qualifiedName _: String?, + attributes attributeDict: [String: String]) + { + if elementName == "post" { + currentAttributes = attributeDict + + if let id = Int(attributeDict["id"] ?? ""), + let createdAtTimestamp = TimeInterval(attributeDict["created_at"] ?? "") + { + if let score = Int(attributeDict["score"] ?? ""), + let width = Int(attributeDict["width"] ?? ""), + let height = Int(attributeDict["height"] ?? "") + { + posts.append(MoebooruPost( + id: id, + tags: attributeDict["tags"]?.components(separatedBy: " ") ?? [], + createdAt: Date(timeIntervalSince1970: createdAtTimestamp), + author: attributeDict["author"] ?? "", + source: URL(string: attributeDict["source"] ?? ""), + score: score, + fileURL: URL(string: attributeDict["file_url"] ?? ""), + previewURL: URL(string: attributeDict["preview_url"] ?? ""), + sampleURL: URL(string: attributeDict["sample_url"] ?? ""), + jpegURL: URL(string: attributeDict["jpeg_url"] ?? ""), + width: width, + height: height + )) + } + } + } + } +} |