diff options
| author | Fuwn <[email protected]> | 2025-02-20 19:37:12 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-02-20 19:37:12 -0800 |
| commit | 2a20920675c82a64131b7c41d5c5288f718c7a0b (patch) | |
| tree | f29285405cffe545489ecaf2fa825eec56410b29 | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-2a20920675c82a64131b7c41d5c5288f718c7a0b.tar.xz sora-testing-2a20920675c82a64131b7c41d5c5288f718c7a0b.zip | |
feat: Development commit
| -rw-r--r-- | Sora/Data/Danbooru/DanbooruManager.swift | 114 | ||||
| -rw-r--r-- | Sora/Data/Danbooru/DanbooruPost.swift | 27 | ||||
| -rw-r--r-- | Sora/Data/Danbooru/DanbooruPostXMLParser.swift | 98 | ||||
| -rw-r--r-- | Sora/Data/Moebooru/MoebooruManager.swift | 114 | ||||
| -rw-r--r-- | Sora/Data/Moebooru/MoebooruPost.swift | 46 | ||||
| -rw-r--r-- | Sora/Data/Moebooru/MoebooruPostXMLParser.swift | 147 |
6 files changed, 0 insertions, 546 deletions
diff --git a/Sora/Data/Danbooru/DanbooruManager.swift b/Sora/Data/Danbooru/DanbooruManager.swift deleted file mode 100644 index 7bbb3a7..0000000 --- a/Sora/Data/Danbooru/DanbooruManager.swift +++ /dev/null @@ -1,114 +0,0 @@ -import SwiftUI - -@MainActor -class DanbooruManager: ObservableObject { - @Published var posts: [DanbooruPost] = [] - @Published var allTags: [BooruTag] = [] - @Published var isLoading: Bool = false - @Published var currentPage: Int = 1 - @Published var searchText = "" - @Published var endOfData: Bool = false - #if os(macOS) - @Published var selectedPost: MoebooruPost? - #endif - private var currentTask: Task<Void, Never>? - var tags: [String] { - if searchText.isEmpty { - return [] - } - - return searchText - .split(separator: " ") - .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } - .filter { !$0.isEmpty } - } - - init() { - fetchAllTags() - } - - func fetchPosts(page: Int = 1, limit: Int = 100, tags: [String] = [], replace: Bool = false) async { - guard !isLoading else { return } - - currentTask?.cancel() - - currentTask = Task { - isLoading = true - - defer { isLoading = false } - - if replace { - self.posts = [] - self.currentPage = 1 - } - - guard let url = URL(string: "https://safebooru.org/index.php?page=dapi&s=post&q=index&pid=\(page)&limit=\(limit)&tags=\(tags.joined(separator: "+"))") else { return } - - do { - let (data, _) = try await URLSession.shared.data(from: url) - - if Task.isCancelled { return } - - DispatchQueue.main.async { - let newPosts = Array(Set(DanbooruPostXMLParser(data: data).parse())).sorted { $0.id > $1.id } - - if newPosts == [] { - self.endOfData = true - } else { - self.posts += newPosts - } - } - } catch { - if (error as? URLError)?.code != .cancelled { - #if DEBUG - print(error) - #endif - } - } - } - } - - func performSearch() { - Task { - await fetchPosts( - page: 1, - tags: tags, - replace: true - ) - } - } - - func loadNextPage() { - guard !isLoading else { return } - - Task { - await fetchPosts(page: currentPage + 1, tags: tags) - - DispatchQueue.main.async { - self.currentPage += 1 - } - } - } - - func fetchAllTags(limit: Int = 100_000) { - Task { - guard let url = URL(string: "https://safebooru.org/index.php?page=dapi&s=tag&q=index&limit=\(limit)") else { return } - - do { - let (data, _) = try await URLSession.shared.data(from: url) - - if Task.isCancelled { return } - - DispatchQueue.main.async { - self.allTags = (BooruTagXMLParser(data: data).parse()).sorted { $0.count > $1.count } - } - } catch { - if (error as? URLError)?.code != .cancelled { - #if DEBUG - print(error) - #endif - } - } - } - } -} diff --git a/Sora/Data/Danbooru/DanbooruPost.swift b/Sora/Data/Danbooru/DanbooruPost.swift deleted file mode 100644 index 17b0d0f..0000000 --- a/Sora/Data/Danbooru/DanbooruPost.swift +++ /dev/null @@ -1,27 +0,0 @@ -import Foundation - -struct DanbooruPost: Identifiable, Hashable { - let id: String - let height: Int - let score: String - let fileURL: URL - let parentID: String - let sampleURL: URL - let sampleWidth: Int - let sampleHeight: Int - let previewURL: URL - let rating: String - let tags: [String] - let width: Int - let change: String - let md5: String - let creatorID: String - let hasChildren: Bool - let createdAt: String - let status: String - let source: String - let hasNotes: Bool - let hasComments: Bool - let previewWidth: Int - let previewHeight: Int -} diff --git a/Sora/Data/Danbooru/DanbooruPostXMLParser.swift b/Sora/Data/Danbooru/DanbooruPostXMLParser.swift deleted file mode 100644 index 3d99465..0000000 --- a/Sora/Data/Danbooru/DanbooruPostXMLParser.swift +++ /dev/null @@ -1,98 +0,0 @@ -import Foundation - -class DanbooruPostXMLParser: NSObject, XMLParserDelegate { - private var posts: [DanbooruPost] = [] - private var currentPost: DanbooruPost? - private var parser: XMLParser - - init(data: Data) { - parser = XMLParser(data: data) - - super.init() - - parser.delegate = self - } - - func parse() -> [DanbooruPost] { - 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 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 hasChildrenStr = attributeDict["has_children"], - let createdAt = attributeDict["created_at"], - let status = attributeDict["status"], - let source = attributeDict["source"], - let hasNotesStr = attributeDict["has_notes"], - let hasCommentsStr = attributeDict["has_comments"], - let previewWidthStr = attributeDict["preview_width"], - let previewWidth = Int(previewWidthStr), - let previewHeightStr = attributeDict["preview_height"], - let previewHeight = Int(previewHeightStr) - else { - return - } - - currentPost = DanbooruPost( - 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: rating, - tags: tags.components(separatedBy: " ").filter { !$0.isEmpty }, - width: width, - change: change, - md5: md5, - creatorID: creatorId, - hasChildren: hasChildrenStr == "true", - createdAt: createdAt, - status: status, - source: source, - hasNotes: hasNotesStr == "true", - hasComments: hasCommentsStr == "true", - previewWidth: previewWidth, - previewHeight: previewHeight - ) - } - } - - 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 -} diff --git a/Sora/Data/Moebooru/MoebooruManager.swift b/Sora/Data/Moebooru/MoebooruManager.swift deleted file mode 100644 index d4f18d2..0000000 --- a/Sora/Data/Moebooru/MoebooruManager.swift +++ /dev/null @@ -1,114 +0,0 @@ -import SwiftUI - -@MainActor -class MoebooruManager: ObservableObject { - @Published var posts: [MoebooruPost] = [] - @Published var allTags: [BooruTag] = [] - @Published var isLoading: Bool = false - @Published var currentPage: Int = 1 - @Published var searchText = "" - @Published var endOfData: Bool = false - #if os(macOS) - @Published var selectedPost: MoebooruPost? - #endif - private var currentTask: Task<Void, Never>? - var tags: [String] { - if searchText.isEmpty { - return [] - } - - return searchText - .split(separator: " ") - .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } - .filter { !$0.isEmpty } - } - - init() { - fetchAllTags() - } - - func fetchPosts(page: Int = 1, limit: Int = 100, tags: [String] = [], replace: Bool = false) async { - guard !isLoading else { return } - - currentTask?.cancel() - - currentTask = Task { - isLoading = true - - defer { isLoading = false } - - if replace { - self.posts = [] - self.currentPage = 1 - } - - guard let url = URL(string: "https://yande.re/post.xml?page=\(page)&limit=\(limit)&tags=\(tags.joined(separator: "+"))") else { return } - - do { - let (data, _) = try await URLSession.shared.data(from: url) - - if Task.isCancelled { return } - - DispatchQueue.main.async { - let newPosts = Array(Set(MoebooruPostXMLParser(data: data).parse())).sorted { $0.id > $1.id } - - if newPosts == [] { - self.endOfData = true - } else { - self.posts += newPosts - } - } - } catch { - if (error as? URLError)?.code != .cancelled { - #if DEBUG - print(error) - #endif - } - } - } - } - - func performSearch() { - Task { - await fetchPosts( - page: 1, - tags: tags, - replace: true - ) - } - } - - func loadNextPage() { - guard !isLoading else { return } - - Task { - await fetchPosts(page: currentPage + 1, tags: tags) - - DispatchQueue.main.async { - self.currentPage += 1 - } - } - } - - func fetchAllTags(limit: Int = 100_000) { - Task { - guard let url = URL(string: "https://yande.re/tag.xml?limit=\(limit)") else { return } - - do { - let (data, _) = try await URLSession.shared.data(from: url) - - if Task.isCancelled { return } - - DispatchQueue.main.async { - self.allTags = (BooruTagXMLParser(data: data).parse()).sorted { $0.count > $1.count } - } - } catch { - if (error as? URLError)?.code != .cancelled { - #if DEBUG - print(error) - #endif - } - } - } - } -} diff --git a/Sora/Data/Moebooru/MoebooruPost.swift b/Sora/Data/Moebooru/MoebooruPost.swift deleted file mode 100644 index 215da5e..0000000 --- a/Sora/Data/Moebooru/MoebooruPost.swift +++ /dev/null @@ -1,46 +0,0 @@ -import Foundation - -struct MoebooruPost: Identifiable, Hashable { - let id: String - let tags: [String] - let createdAt: Int - let updatedAt: Int - let creatorID: String - let approverID: String - let author: String - let change: String - let source: String - let score: String - let md5: String - let fileSize: Int - let fileExtension: String - let fileURL: URL - let isShownInIndex: Bool - let previewURL: URL - let previewWidth: Int - let previewHeight: Int - let actualPreviewWidth: Int - let actualPreviewHeight: Int - let sampleURL: URL - let sampleWidth: Int - let sampleHeight: Int - let sampleFileSize: Int - let jpegURL: URL - let jpegWidth: Int - let jpegHeight: Int - let jpegFileSize: Int - let rating: String - let isRatingLocked: Bool - let hasChildren: Bool - let parentId: String - let status: String - let isPending: Bool - let width: Int - let height: Int - let isHeld: Bool - let framesPendingString: String - let framesString: String - let isNoteLocked: Bool - let lastNotedAt: Int - let lastCommentedAt: Int -} diff --git a/Sora/Data/Moebooru/MoebooruPostXMLParser.swift b/Sora/Data/Moebooru/MoebooruPostXMLParser.swift deleted file mode 100644 index e5a73e9..0000000 --- a/Sora/Data/Moebooru/MoebooruPostXMLParser.swift +++ /dev/null @@ -1,147 +0,0 @@ -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 -} |