aboutsummaryrefslogtreecommitdiff
path: root/HoloBar/CharacterFetcher.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-02-07 00:46:37 -0800
committerFuwn <[email protected]>2025-02-07 00:48:26 -0800
commitf3b0018992dec16349ac6132d1ae7f35be62c1ca (patch)
treec68e062b07e88352d18ef641f8138839f84467f1 /HoloBar/CharacterFetcher.swift
downloadholobar-f3b0018992dec16349ac6132d1ae7f35be62c1ca.tar.xz
holobar-f3b0018992dec16349ac6132d1ae7f35be62c1ca.zip
feat: initial release
Diffstat (limited to 'HoloBar/CharacterFetcher.swift')
-rw-r--r--HoloBar/CharacterFetcher.swift55
1 files changed, 55 insertions, 0 deletions
diff --git a/HoloBar/CharacterFetcher.swift b/HoloBar/CharacterFetcher.swift
new file mode 100644
index 0000000..56c4a2f
--- /dev/null
+++ b/HoloBar/CharacterFetcher.swift
@@ -0,0 +1,55 @@
+import Foundation
+import SwiftSoup
+
+class CharacterFetcher: ObservableObject {
+ @Published var characters: [Character] = []
+
+ init() {
+ let today = Calendar.current.dateComponents([.month, .day], from: Date())
+
+ if let month = today.month, let day = today.day {
+ fetchCharacters(for: month, day: day)
+ }
+ }
+
+ func fetchCharacters(for month: Int, day: Int) {
+ let urlString = "https://hololist.net/birthday/?birthday_month=\(String(format: "%02d", month))&birthday_day=\(String(format: "%02d", day))"
+ guard let url = URL(string: urlString) else { return }
+
+ let task = URLSession.shared.dataTask(with: url) { data, _, error in
+ guard let data = data, error == nil,
+ let html = String(data: data, encoding: .utf8) else { return }
+
+ DispatchQueue.main.async {
+ self.characters = self.parseHTML(html: html)
+ }
+ }
+
+ task.resume()
+ }
+
+ private func parseHTML(html: String) -> [Character] {
+ var fetchedCharacters: [Character] = []
+
+ do {
+ let document = try SwiftSoup.parse(html)
+ let characterElements = try document.select("div.d-flex.mb-4.rounded")
+
+ for element in characterElements {
+ if let nameElement = try? element.select("a.line-truncate span").first(),
+ let linkElement = try? element.select("a.line-truncate").first(),
+ let profileHref = try? linkElement.attr("href"),
+ let profileURL = URL(string: profileHref)
+ {
+ let name = try nameElement.text()
+
+ fetchedCharacters.append(Character(name: name, profileURL: profileURL))
+ }
+ }
+ } catch {
+ fetchedCharacters.append(Character(name: "Error parsing HTML", profileURL: URL(string: "#")!))
+ }
+
+ return fetchedCharacters
+ }
+}