summaryrefslogtreecommitdiff
path: root/Sora/Data/Moebooru/MoebooruPostXMLParser.swift
blob: e5a73e9e9d3dc5b4850b9b9b365a7a8a7e3b980a (plain) (blame)
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
import Foundation

class MoebooruPostXMLParser: NSObject, XMLParserDelegate {
    private var posts: [MoebooruPost] = []
    private var currentPost: MoebooruPost?
    private var parser: XMLParser

    init(data: Data) {
        parser = XMLParser(data: data)

        super.init()

        parser.delegate = self
    }

    func parse() -> [MoebooruPost] {
        parser.parse()

        return posts
    }

    func parser(_: XMLParser, didStartElement elementName: String, namespaceURI _: String?, qualifiedName _: String?, attributes attributeDict: [String: String] = [:]) {
        if elementName == "post" {
            guard let id = attributeDict["id"],
                  let tags = attributeDict["tags"],
                  let createdAtStr = attributeDict["created_at"],
                  let createdAt = Int(createdAtStr),
                  let updatedAtStr = attributeDict["updated_at"],
                  let updatedAt = Int(updatedAtStr),
                  let creatorId = attributeDict["creator_id"],
                  let approverId = attributeDict["approver_id"],
                  let author = attributeDict["author"],
                  let change = attributeDict["change"],
                  let source = attributeDict["source"],
                  let score = attributeDict["score"],
                  let md5 = attributeDict["md5"],
                  let fileSizeStr = attributeDict["file_size"],
                  let fileSize = Int(fileSizeStr),
                  let fileExt = attributeDict["file_ext"],
                  let fileUrl = attributeDict["file_url"],
                  let isShownStr = attributeDict["is_shown_in_index"],
                  let previewUrl = attributeDict["preview_url"],
                  let previewWidthStr = attributeDict["preview_width"],
                  let previewWidth = Int(previewWidthStr),
                  let previewHeightStr = attributeDict["preview_height"],
                  let previewHeight = Int(previewHeightStr),
                  let actualPreviewWidthStr = attributeDict["actual_preview_width"],
                  let actualPreviewWidth = Int(actualPreviewWidthStr),
                  let actualPreviewHeightStr = attributeDict["actual_preview_height"],
                  let actualPreviewHeight = Int(actualPreviewHeightStr),
                  let sampleUrl = attributeDict["sample_url"],
                  let sampleWidthStr = attributeDict["sample_width"],
                  let sampleWidth = Int(sampleWidthStr),
                  let sampleHeightStr = attributeDict["sample_height"],
                  let sampleHeight = Int(sampleHeightStr),
                  let sampleFileSizeStr = attributeDict["sample_file_size"],
                  let sampleFileSize = Int(sampleFileSizeStr),
                  let jpegUrl = attributeDict["jpeg_url"],
                  let jpegWidthStr = attributeDict["jpeg_width"],
                  let jpegWidth = Int(jpegWidthStr),
                  let jpegHeightStr = attributeDict["jpeg_height"],
                  let jpegHeight = Int(jpegHeightStr),
                  let jpegFileSizeStr = attributeDict["jpeg_file_size"],
                  let jpegFileSize = Int(jpegFileSizeStr),
                  let rating = attributeDict["rating"],
                  let isRatingLockedStr = attributeDict["is_rating_locked"],
                  let hasChildrenStr = attributeDict["has_children"],
                  let parentId = attributeDict["parent_id"],
                  let status = attributeDict["status"],
                  let isPendingStr = attributeDict["is_pending"],
                  let widthStr = attributeDict["width"],
                  let width = Int(widthStr),
                  let heightStr = attributeDict["height"],
                  let height = Int(heightStr),
                  let isHeldStr = attributeDict["is_held"],
                  let framesPendingString = attributeDict["frames_pending_string"],
                  let framesString = attributeDict["frames_string"],
                  let isNoteLockedStr = attributeDict["is_note_locked"],
                  let lastNotedAtStr = attributeDict["last_noted_at"],
                  let lastNotedAt = Int(lastNotedAtStr),
                  let lastCommentedAtStr = attributeDict["last_commented_at"],
                  let lastCommentedAt = Int(lastCommentedAtStr)
            else {
                return
            }

            currentPost = MoebooruPost(
                id: id,
                tags: tags.components(separatedBy: " "),
                createdAt: createdAt,
                updatedAt: updatedAt,
                creatorID: creatorId,
                approverID: approverId,
                author: author,
                change: change,
                source: source,
                score: score,
                md5: md5,
                fileSize: fileSize,
                fileExtension: fileExt,
                fileURL: URL(string: fileUrl)!,
                isShownInIndex: isShownStr == "true",
                previewURL: URL(string: previewUrl)!,
                previewWidth: previewWidth,
                previewHeight: previewHeight,
                actualPreviewWidth: actualPreviewWidth,
                actualPreviewHeight: actualPreviewHeight,
                sampleURL: URL(string: sampleUrl)!,
                sampleWidth: sampleWidth,
                sampleHeight: sampleHeight,
                sampleFileSize: sampleFileSize,
                jpegURL: URL(string: jpegUrl)!,
                jpegWidth: jpegWidth,
                jpegHeight: jpegHeight,
                jpegFileSize: jpegFileSize,
                rating: rating,
                isRatingLocked: isRatingLockedStr == "true",
                hasChildren: hasChildrenStr == "true",
                parentId: parentId,
                status: status,
                isPending: isPendingStr == "true",
                width: width,
                height: height,
                isHeld: isHeldStr == "true",
                framesPendingString: framesPendingString,
                framesString: framesString,
                isNoteLocked: isNoteLockedStr == "true",
                lastNotedAt: lastNotedAt,
                lastCommentedAt: lastCommentedAt
            )
        }
    }

    func parser(_: XMLParser, didEndElement elementName: String, namespaceURI _: String?, qualifiedName _: String?) {
        if elementName == "post", let post = currentPost {
            posts.append(post)

            currentPost = nil
        }
    }

    #if DEBUG
        func parser(_: XMLParser, parseErrorOccurred parseError: any Error) {
            print(parseError)
        }
    #endif
}