|
| 1 | +import Foundation |
| 2 | + |
| 3 | +struct YoutubeSearchRequest: Encodable { |
| 4 | + let title: String |
| 5 | + let artist: String |
| 6 | +} |
| 7 | + |
| 8 | +struct YoutubeVideo: Identifiable, Decodable, Equatable { |
| 9 | + let id: String |
| 10 | + let title: String |
| 11 | + let duration: Double |
| 12 | + private let urlString: String? |
| 13 | + |
| 14 | + enum CodingKeys: String, CodingKey { |
| 15 | + case id |
| 16 | + case title |
| 17 | + case duration |
| 18 | + case url |
| 19 | + } |
| 20 | + |
| 21 | + init(from decoder: Decoder) throws { |
| 22 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 23 | + title = try container.decode(String.self, forKey: .title) |
| 24 | + |
| 25 | + if let numericDuration = try? container.decode(Double.self, forKey: .duration) { |
| 26 | + duration = numericDuration |
| 27 | + } else { |
| 28 | + let durationText = try container.decode(String.self, forKey: .duration) |
| 29 | + guard let parsedDuration = Self.parseDuration(durationText) else { |
| 30 | + throw DecodingError.dataCorruptedError( |
| 31 | + forKey: .duration, |
| 32 | + in: container, |
| 33 | + debugDescription: "Unsupported duration format: \(durationText)" |
| 34 | + ) |
| 35 | + } |
| 36 | + duration = parsedDuration |
| 37 | + } |
| 38 | + |
| 39 | + urlString = try? container.decodeIfPresent(String.self, forKey: .url) |
| 40 | + |
| 41 | + if |
| 42 | + let decodedID = (try? container.decodeIfPresent(String.self, forKey: .id))? |
| 43 | + .trimmingCharacters(in: .whitespacesAndNewlines), |
| 44 | + !decodedID.isEmpty |
| 45 | + { |
| 46 | + id = decodedID |
| 47 | + } else if let urlString, let extractedVideoID = Self.extractVideoID(from: urlString) { |
| 48 | + id = extractedVideoID |
| 49 | + } else { |
| 50 | + throw DecodingError.dataCorruptedError( |
| 51 | + forKey: .id, |
| 52 | + in: container, |
| 53 | + debugDescription: "Missing youtube video id." |
| 54 | + ) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + var thumbnailURL: URL? { |
| 59 | + guard let videoID = normalizedVideoID else { |
| 60 | + return nil |
| 61 | + } |
| 62 | + return URL(string: "https://i.ytimg.com/vi/\(videoID)/hqdefault.jpg") |
| 63 | + } |
| 64 | + |
| 65 | + var embedURL: URL? { |
| 66 | + if let urlString, let embedURL = URL(string: urlString) { |
| 67 | + return embedURL |
| 68 | + } |
| 69 | + |
| 70 | + guard let videoID = normalizedVideoID else { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + return URL(string: "https://www.youtube.com/embed/\(videoID)?playsinline=1") |
| 75 | + } |
| 76 | + |
| 77 | + private var normalizedVideoID: String? { |
| 78 | + if let extractedFromID = Self.extractVideoID(from: id) { |
| 79 | + return extractedFromID |
| 80 | + } |
| 81 | + |
| 82 | + if !id.isEmpty, !id.contains("http"), !id.contains("/") { |
| 83 | + return id |
| 84 | + } |
| 85 | + |
| 86 | + if let urlString, let extractedFromURL = Self.extractVideoID(from: urlString) { |
| 87 | + return extractedFromURL |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + private static func extractVideoID(from value: String) -> String? { |
| 94 | + let trimmedValue = value.trimmingCharacters(in: .whitespacesAndNewlines) |
| 95 | + guard !trimmedValue.isEmpty else { |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + guard let components = URLComponents(string: trimmedValue) else { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + let pathComponents = components.path.split(separator: "/").map(String.init) |
| 104 | + if let embedIndex = pathComponents.firstIndex(of: "embed"), |
| 105 | + pathComponents.indices.contains(embedIndex + 1) { |
| 106 | + let candidate = pathComponents[embedIndex + 1] |
| 107 | + if !candidate.isEmpty { |
| 108 | + return candidate |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if |
| 113 | + let host = components.host?.lowercased(), |
| 114 | + host.contains("youtu.be"), |
| 115 | + let firstPath = pathComponents.first, |
| 116 | + !firstPath.isEmpty |
| 117 | + { |
| 118 | + return firstPath |
| 119 | + } |
| 120 | + |
| 121 | + if let watchVideoID = components.queryItems?.first(where: { $0.name == "v" })?.value, |
| 122 | + !watchVideoID.isEmpty { |
| 123 | + return watchVideoID |
| 124 | + } |
| 125 | + |
| 126 | + if components.scheme == nil, components.host == nil, !trimmedValue.contains("/") { |
| 127 | + return trimmedValue |
| 128 | + } |
| 129 | + |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + private static let durationRegex = try? NSRegularExpression( |
| 134 | + pattern: #"^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?$"# |
| 135 | + ) |
| 136 | + |
| 137 | + private static func parseDuration(_ value: String) -> Double? { |
| 138 | + guard let regex = durationRegex else { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + let range = NSRange(value.startIndex..<value.endIndex, in: value) |
| 143 | + guard let match = regex.firstMatch(in: value, options: [], range: range) else { |
| 144 | + return nil |
| 145 | + } |
| 146 | + |
| 147 | + func component(at index: Int) -> Double { |
| 148 | + let componentRange = match.range(at: index) |
| 149 | + guard |
| 150 | + componentRange.location != NSNotFound, |
| 151 | + let swiftRange = Range(componentRange, in: value), |
| 152 | + let parsed = Double(value[swiftRange]) |
| 153 | + else { |
| 154 | + return 0 |
| 155 | + } |
| 156 | + return parsed |
| 157 | + } |
| 158 | + |
| 159 | + return component(at: 1) * 3600 + component(at: 2) * 60 + component(at: 3) |
| 160 | + } |
| 161 | +} |
0 commit comments