diff options
| author | Fuwn <[email protected]> | 2025-02-09 03:28:19 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-02-09 03:28:19 -0800 |
| commit | 73f1813a0aeebc535beffc8ac6f8e00bb7f2c1a1 (patch) | |
| tree | b98b59ba7887136a3f6ea86cc6564b7dbbda68c5 | |
| parent | feat: initial release (diff) | |
| download | holobar-73f1813a0aeebc535beffc8ac6f8e00bb7f2c1a1.tar.xz holobar-73f1813a0aeebc535beffc8ac6f8e00bb7f2c1a1.zip | |
feat(CharacterFetcher): display character affiliation)
| -rw-r--r-- | HoloBar/CharacterFetcher.swift | 38 |
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 + } + } } |