summaryrefslogtreecommitdiff
path: root/Sora/Data/Danbooru/DanbooruPostParser.swift
blob: 1e5cdc495711ccd9c7c3763caba4e6a428c7c199 (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
import Foundation

nonisolated class DanbooruPostParser {
  private struct FailableDecodable<Value: Decodable>: Decodable {
    let value: Value?

    init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()

      value = try? container.decode(Value.self)
    }
  }

  private let data: Data

  init(data: Data) {
    self.data = data
  }

  func parse() -> [BooruPost] {
    let decoder = JSONDecoder()

    decoder.dateDecodingStrategy = .custom { decoder in
      Self.parseDate(
        try (try decoder.singleValueContainer()).decode(String.self)
      ) ?? Date()
    }

    do {
      let decodedPosts = try decoder.decode([FailableDecodable<DanbooruPost>].self, from: data)
      let validPosts = decodedPosts.compactMap(\.value)
      let droppedRecordCount = decodedPosts.count - validPosts.count

      if droppedRecordCount > 0 {
        debugPrint(
          "DanbooruPostParser.parse: dropped \(droppedRecordCount) malformed records while decoding page."
        )
      }

      return validPosts.compactMap { post in post.toBooruPost() }
    } catch {
      debugPrint("DanbooruPostParser.parse: failed to decode response: \(error)")

      return []
    }
  }

  nonisolated(unsafe) private static let isoFormatter: ISO8601DateFormatter = {
    let formatter = ISO8601DateFormatter()

    formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]

    return formatter
  }()

  private static let alternativeFormatter: DateFormatter = {
    let formatter = DateFormatter()

    formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
    formatter.locale = Locale(identifier: "en_US_POSIX")

    return formatter
  }()

  private static func parseDate(_ input: String) -> Date? {
    if let date = isoFormatter.date(from: input) {
      return date
    }

    if let date = alternativeFormatter.date(from: input) {
      return date
    }

    if let timestamp = Double(input) {
      return Date(timeIntervalSince1970: timestamp)
    }

    return nil
  }
}