-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathYouTubePlayer+Source.swift
423 lines (368 loc) · 13 KB
/
YouTubePlayer+Source.swift
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import Foundation
// MARK: - YouTubePlayer+Source
public extension YouTubePlayer {
/// A YouTube player source.
enum Source: Hashable, Sendable {
/// Video.
case video(id: String)
/// Videos.
case videos(ids: [String])
/// Playlist.
case playlist(id: String)
/// Channel.
case channel(name: String)
}
}
// MARK: - Identifiable
extension YouTubePlayer.Source: Identifiable {
/// The identifier.
public var id: String {
switch self {
case .video(let id):
return id
case .videos(let ids):
return ids.joined(separator: ",")
case .playlist(let id):
return id
case .channel(let name):
return name
}
}
}
// MARK: - URL
public extension YouTubePlayer.Source {
/// The URL representation of the source.
var url: URL? {
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "www.youtube.com"
switch self {
case .video(let id):
guard !id.isEmpty else {
return nil
}
urlComponents.path = "/watch"
urlComponents.queryItems = [
.init(
name: "v",
value: id
)
]
case .videos(let ids):
guard !ids.isEmpty else {
return nil
}
urlComponents.path = "/watch_videos"
urlComponents.queryItems = [
.init(
name: "video_ids",
value: ids.joined(separator: ",")
)
]
case .playlist(let id):
guard !id.isEmpty else {
return nil
}
urlComponents.path = "/playlist"
urlComponents.queryItems = [
.init(
name: "list",
value: id
)
]
case .channel(let name):
guard !name.isEmpty else {
return nil
}
urlComponents.path = "/@\(name)"
}
return urlComponents.url
}
}
// MARK: - Video ID
public extension YouTubePlayer.Source {
/// The video identifier, if available.
var videoID: String? {
if case .video(let id) = self {
return id
} else {
return nil
}
}
}
// MARK: - Video IDs
public extension YouTubePlayer.Source {
/// The video identifiers, if available.
var videoIDs: [String]? {
if case .videos(let ids) = self {
return ids
} else {
return nil
}
}
}
// MARK: - Playlist ID
public extension YouTubePlayer.Source {
/// The playlist identifier, if available.
var playlistID: String? {
if case .playlist(let id) = self {
return id
} else {
return nil
}
}
}
// MARK: - Channel Name
public extension YouTubePlayer.Source {
/// The channel name, if available.
var channelName: String? {
if case .channel(let name) = self {
return name
} else {
return nil
}
}
}
// MARK: - ExpressibleByArrayLiteral
extension YouTubePlayer.Source: ExpressibleByArrayLiteral {
/// Creates a new instance of ``YouTubePlayer/Source``
/// - Parameter videoIDs: The video identifiers.
public init(
arrayLiteral videoIDs: String...
) {
self = .videos(ids: videoIDs)
}
}
// MARK: - ExpressibleByURL
extension YouTubePlayer.Source: ExpressibleByURL {
/// Creates a new instance of ``YouTubePlayer/Source``
/// - Parameter url: The URL.
public init?(
url: URL
) {
// For each url extraction rule set
for urlExtractionRuleSet in [
Self.playlistExtractionRuleSet,
Self.videoExtractionRuleSet,
Self.videosExtractionRuleSet,
Self.channelURLExtractionRuleSet
] {
// Verify if a source can be extracted from rule set
guard let source = url.extract(using: urlExtractionRuleSet) else {
// Otherwise continue with next rule set
continue
}
// Initialize with source
self = source
// Return out of function
return
}
// Return nil as url could not be parsed
return nil
}
/// The `.video(id:)` case ``URL.ExtractionRuleSet``
private static let videoExtractionRuleSet = URL.ExtractionRuleSet(
rules: [
.firstPathComponent(host: "youtu.be"),
.queryItem(
name: "v",
pathComponents: ["watch"]
),
.pathComponent(after: "v"),
.pathComponent(after: "embed"),
.pathComponent(after: "shorts"),
.pathComponent(after: "live"),
.pathComponent(after: "e")
],
output: Self.video
)
/// The `.videos(ids:)` case ``URL.ExtractionRuleSet``
private static let videosExtractionRuleSet = URL.ExtractionRuleSet(
rules: [
.queryItem(
name: "video_ids",
pathComponents: ["watch_videos"]
)
],
output: { match -> Self? in
guard case let videoIDs = match.components(separatedBy: ","),
!videoIDs.isEmpty else {
return nil
}
return .videos(ids: videoIDs)
}
)
/// The `.playlist(id:)` case ``URL.ExtractionRuleSet``
private static let playlistExtractionRuleSet = URL.ExtractionRuleSet(
rules: [
.queryItem(
name: "list",
pathComponents: ["playlist"]
),
.queryItem(
name: "list",
pathComponents: ["watch"]
),
.queryItem(
name: "list",
pathComponents: ["embed", "videoseries"]
)
],
output: Self.playlist
)
/// The `.channel(name:)` case ``URL.ExtractionRuleSet``
private static let channelURLExtractionRuleSet = URL.ExtractionRuleSet(
rules: [
.pathComponent(after: "channel"),
.pathComponent(after: "c"),
.pathComponent(after: "user"),
.firstPathComponentWithPrefix("@", removePrefix: true),
.pathComponent(after: "feed")
],
output: Self.channel
)
}
// MARK: - URL+extract(using:)
private extension URL {
/// An extraction rule.
enum ExtractionRule: Hashable, Sendable {
/// Extracts the first path component if the host matches.
case firstPathComponent(host: String)
/// Extracts the first path component where the prefix matches and optionally removes it.
case firstPathComponentWithPrefix(Character, removePrefix: Bool)
/// Extracts the path component after the provided one..
case pathComponent(after: String)
/// Extracts the value of the query item where the name matches and the supplied path components.
case queryItem(name: String, pathComponents: [String])
}
/// Extracts a value from this url using the provided ``URL.ExtractionRule``
/// - Parameter rule: The url extraction rule.
func extract(
using rule: ExtractionRule
) -> String? {
// Initialize path components by dropping the first path component which is represented as "/"
lazy var pathComponents = Array(self.pathComponents.dropFirst())
switch rule {
case .firstPathComponent(let host):
// Verify host matches and the first path component is available
guard self.host == host else {
// Otherwise return nil
return nil
}
// Return the first path component
return pathComponents.first
case .firstPathComponentWithPrefix(let prefix, let removePrefix):
// Verify the first path component is available and has the provided prefix
guard let firstPathComponent = pathComponents.first,
firstPathComponent.first == prefix,
case let prefixRemovedFirstPathComponent = String(firstPathComponent.dropFirst()),
!prefixRemovedFirstPathComponent.isEmpty else {
// Otherwise return nil
return nil
}
// Return first path component with or without prefix
return removePrefix ? prefixRemovedFirstPathComponent : firstPathComponent
case .pathComponent(let pathComponent):
// Verify index of path component is available and the next path component is available
guard let pathComponentIndex = pathComponents.firstIndex(of: pathComponent),
case let nextPathComponentIndex = pathComponentIndex + 1,
pathComponents.indices.contains(nextPathComponentIndex) else {
// Otherwise return nil
return nil
}
// Return next path component
return pathComponents[nextPathComponentIndex]
case .queryItem(let name, let expectedPathComponents):
// Verify that the path components match and that the query item is available
guard pathComponents == expectedPathComponents,
let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true),
let queryItem = urlComponents.queryItems?.first(where: { $0.name == name }),
let queryItemValue = queryItem.value,
!queryItemValue.isEmpty else {
// Otherwise return nil
return nil
}
// Return value of query item
return queryItemValue
}
}
/// An extraction rule set.
struct ExtractionRuleSet<Output>: Sendable {
/// The extraction rules.
let rules: [ExtractionRule]
/// A closure used to convert the match to the output, if possible.
let output: @Sendable (String) -> Output?
}
/// Extracts the `Output` from this url using the provided ``URL.ExtractionRuleSet``.
/// - Parameter ruleSet: The url extraction rule set.
func extract<Output>(
using ruleSet: ExtractionRuleSet<Output>
) -> Output? {
// For each rule
for rule in ruleSet.rules {
// Verify a match is available and can be converted to the output
guard let match = self.extract(using: rule),
let output = ruleSet.output(match) else {
// Otherwise continue with the next rule
continue
}
// Return output
return output
}
// Return nil as no rule has matched
return nil
}
}
// MARK: - Codable
extension YouTubePlayer.Source: Codable {
/// Creates a new instance of ``YouTubePlayer/Source``
/// - Parameter decoder: The decoder.
public init(
from decoder: Decoder
) throws {
lazy var decodingError = DecodingError
.dataCorrupted(
.init(
codingPath: decoder.codingPath,
debugDescription: "Unsupported source representation"
)
)
if let urlString = try? decoder.singleValueContainer().decode(String.self),
let source = Self(urlString: urlString) {
self = source
} else if let videoIDs = try? decoder.singleValueContainer().decode([String].self),
!videoIDs.isEmpty {
self = .videos(ids: videoIDs)
} else if let container = try? decoder.container(keyedBy: YouTubePlayer.Parameters.CodingKeys.self) {
if let listType = try? container.decode(YouTubePlayer.Parameters.ListType.self, forKey: .listType),
let list = try? container.decode(String.self, forKey: .list) {
switch listType {
case .playlist:
self = .playlist(id: list)
case .channel:
self = .channel(name: list)
}
} else if let playlist = try? container.decode(String.self, forKey: .playlist),
case let videoIDs = playlist.components(separatedBy: ","),
!videoIDs.isEmpty {
if videoIDs.count == 1, let videoID = videoIDs.first {
self = .video(id: videoID)
} else {
self = .videos(ids: videoIDs)
}
} else {
throw decodingError
}
} else {
throw decodingError
}
}
/// Encode.
/// - Parameter encoder: The encoder.
public func encode(
to encoder: Encoder
) throws {
var container = encoder.singleValueContainer()
try container.encode(self.url)
}
}