aboutsummaryrefslogtreecommitdiff
path: root/HoloBar/CharacterFetcher.swift
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-02-09 03:28:19 -0800
committerFuwn <[email protected]>2025-02-09 03:28:19 -0800
commit73f1813a0aeebc535beffc8ac6f8e00bb7f2c1a1 (patch)
treeb98b59ba7887136a3f6ea86cc6564b7dbbda68c5 /HoloBar/CharacterFetcher.swift
parentfeat: initial release (diff)
downloadholobar-73f1813a0aeebc535beffc8ac6f8e00bb7f2c1a1.tar.xz
holobar-73f1813a0aeebc535beffc8ac6f8e00bb7f2c1a1.zip
feat(CharacterFetcher): display character affiliation)
Diffstat (limited to 'HoloBar/CharacterFetcher.swift')
-rw-r--r--HoloBar/CharacterFetcher.swift38
1 files changed, 37 insertions, 1 deletions
diff --git a/HoloBar/CharacterFetcher.swift b/HoloBar/CharacterFetcher.swift
index 56c4a2f..f2de7fc 100644
--- a/HoloBar/CharacterFetcher.swift
+++ b/HoloBar/CharacterFetcher.swift
@@ -21,7 +21,7 @@ class CharacterFetcher: ObservableObject {
let html = String(data: data, encoding: .utf8) else { return }
DispatchQueue.main.async {
- self.characters = self.parseHTML(html: html)
+ self.fetchAffiliations(for: self.parseHTML(html: html))
}
}
@@ -52,4 +52,40 @@ class CharacterFetcher: ObservableObject {
return fetchedCharacters
}
+
+ private func fetchAffiliations(for characters: [Character]) {
+ let group = DispatchGroup()
+ var updatedCharacters: [Character] = []
+
+ for character in characters {
+ group.enter()
+
+ let task = URLSession.shared.dataTask(with: character.profileURL) { data, _, error in
+ defer { group.leave() }
+
+ guard let data = data, error == nil,
+ let html = String(data: data, encoding: .utf8) else { return }
+
+ do {
+ let document = try SwiftSoup.parse(html)
+
+ if let affiliationElement = try? document.select("#affiliation a").first(),
+ let affiliation = try? affiliationElement.text()
+ {
+ updatedCharacters.append(Character(name: "\(character.name) (\(affiliation))", profileURL: character.profileURL))
+ } else {
+ updatedCharacters.append(character)
+ }
+ } catch {
+ updatedCharacters.append(character)
+ }
+ }
+
+ task.resume()
+ }
+
+ group.notify(queue: .main) {
+ self.characters = updatedCharacters
+ }
+ }
}