-
Notifications
You must be signed in to change notification settings - Fork 142
/
wwdc2017.swift
executable file
·558 lines (460 loc) · 19.6 KB
/
wwdc2017.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/swift -swift-version 4
/*
Author: Olivier HO-A-CHUCK
Date: June 17th 2017
About this script:
WWDC 2017 is ending and even if there are some great tools out there (https://github.com/insidegui/WWDC) that allow to see and download video sessions,
I Still need to get my video doggy bag to fly back home. And Moscone alsways provide with great bandwidth.
So as I had never really started to code in Swift I decided to start here (I know it's late - but I'm no more a developer) and copy/pasted some internet peace
of codes to get a Swift Script that bulk download all sessions.
You may have understand my usual disclamer : "I'm a Marketing guy" so don't blame my messy (Swift beginer) code.
Please feel free to make this script better if you feel like so. There is plenty to do.
License: Do what you want with it. But notice that this script comes with no warranty and will not be maintained.
Usage: wwdc2017.swift
Default behavior: without any options the script will download all available hd videos. And will re-take non fully downloaded ones.
Please use --help option to get currently available options
TODO:
- basically all previous script option (previuous years, checks, cleaner code, etc.)
*/
import Cocoa
import Foundation
import SystemConfiguration
enum VideoQuality: String {
case HD = "hd"
case SD = "sd"
}
//http://stackoverflow.com/a/30743763
class Reachability {
class func isConnectedToNetwork() -> Bool {
guard let flags = getFlags() else { return false }
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
class func getFlags() -> SCNetworkReachabilityFlags? {
guard let reachability = ipv4Reachability() ?? ipv6Reachability() else {
return nil
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(reachability, &flags) {
return nil
}
return flags
}
class func ipv6Reachability() -> SCNetworkReachability? {
var zeroAddress = sockaddr_in6()
zeroAddress.sin6_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin6_family = sa_family_t(AF_INET6)
return withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
})
}
class func ipv4Reachability() -> SCNetworkReachability? {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
return withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
})
}
}
extension Notification.Name {
static let flagsChanged = Notification.Name("FlagsChanged")
}
struct Network {
static var reachability: Reachability?
enum Status: String, CustomStringConvertible {
case unreachable, wifi, wwan
var description: String { return rawValue }
}
enum Error: Swift.Error {
case failedToSetCallout
case failedToSetDispatchQueue
case failedToCreateWith(String)
case failedToInitializeWith(sockaddr_in)
}
}
class DownloadSessionManager : NSObject, URLSessionDownloadDelegate {
static let sharedInstance = DownloadSessionManager()
var filePath : String?
var url: URL?
var resumeData: Data?
let semaphore = DispatchSemaphore.init(value: 0)
var session : URLSession!
override init() {
super.init()
self.resetSession()
}
func resetSession() {
self.session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
}
func downloadFile(fromURL url: URL, toPath path: String) {
self.filePath = path
self.url = url
self.resumeData = nil
taskStartedAt = Date()
let task = session.downloadTask(with: url)
task.resume()
semaphore.wait()
}
func resumeDownload() {
//TODO: reset session in appropriate URLSessionDelegate function?
self.resetSession()
if let resumeData = self.resumeData {
print("resuming file download...")
let task = session.downloadTask(withResumeData: resumeData)
task.resume()
self.resumeData = nil
semaphore.wait()
} else {
print("retrying file download...")
self.downloadFile(fromURL: self.url!, toPath: self.filePath!)
}
}
func show(progress: Int, barWidth: Int, speedInK: Int) {
print("\r[", terminator: "")
let pos = Int(Double(barWidth*progress)/100.0)
for i in 0...barWidth {
switch(i) {
case _ where i < pos:
print("🁢", terminator:"")
break
case pos:
print("🁢", terminator:"")
break
default:
print(" ", terminator:"")
break
}
}
print("] \(progress)% \(speedInK)KB/s", terminator:"")
fflush(__stdoutp)
}
var taskStartedAt : Date?
//MARK : URLSessionDownloadDelegate stuff
func urlSession(_: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
let now = Date()
let timeDownloaded = now.timeIntervalSince(taskStartedAt!)
let kbs = Int( floor( Float(totalBytesWritten) / 1024.0 / Float(timeDownloaded) ) )
show(progress: Int(Double(totalBytesWritten)/Double(totalBytesExpectedToWrite)*100.0), barWidth: 70, speedInK: kbs)
}
func urlSession(_: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
defer {
semaphore.signal()
}
print("")
guard let _ = self.filePath else {
print("No destination path to copy the downloaded file at \(location)")
return
}
print("moving \(location) to \(self.filePath!)")
do {
try FileManager.default.moveItem(at: location, to: URL.init(fileURLWithPath: "\(filePath!)"))
}
catch let error {
print("Ooops! Something went wrong: \(error)")
}
}
func urlSession(_: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let error = error else {
//No error. Already handled in URLSession(session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingToURL location: URL)
return
}
defer {
defer {
semaphore.signal()
}
if !Reachability.isConnectedToNetwork() {
print("Waiting for connection to be restored")
repeat {
sleep(1)
} while !Reachability.isConnectedToNetwork()
}
self.resumeDownload()
}
print("")
print("Ooops! Something went wrong: \(error.localizedDescription)")
guard let resumeData = (error as NSError).userInfo[NSURLSessionDownloadTaskResumeData] as? Data else {
return
}
self.resumeData = resumeData
}
}
class wwdcVideosController {
class func getHDorSDdURLs(fromHTML: String, format: VideoQuality) -> (String) {
let pat = "\\b.*(https://.*" + format.rawValue + ".*\\.mp4)\\b"
let regex = try! NSRegularExpression(pattern: pat, options: [])
let matches = regex.matches(in: fromHTML, options: [], range: NSRange(location: 0, length: fromHTML.count))
var videoURL = ""
if !matches.isEmpty {
let range = matches[0].range(at: 1)
let r = fromHTML.index(fromHTML.startIndex, offsetBy: range.location) ..<
fromHTML.index(fromHTML.startIndex, offsetBy: range.location+range.length)
videoURL = String(fromHTML[r])
}
return videoURL
}
class func getPDFResourceURL(fromHTML: String) -> (String) {
let pat = "\\b.*(https://.*\\.pdf)\\b"
let regex = try! NSRegularExpression(pattern: pat, options: [])
let matches = regex.matches(in: fromHTML, options: [], range: NSRange(location: 0, length: fromHTML.count))
var pdfResourceURL = ""
if !matches.isEmpty {
let range = matches[0].range(at: 1)
let r = fromHTML.index(fromHTML.startIndex, offsetBy: range.location) ..<
fromHTML.index(fromHTML.startIndex, offsetBy: range.location+range.length)
pdfResourceURL = String(fromHTML[r])
}
return pdfResourceURL
}
class func getTitle(fromHTML: String) -> (String) {
let pat = "<h1>(.*)</h1>"
let regex = try! NSRegularExpression(pattern: pat, options: [])
let matches = regex.matches(in: fromHTML, options: [], range: NSRange(location: 0, length: fromHTML.count))
var title = ""
if !matches.isEmpty {
let range = matches[0].range(at: 1)
let r = fromHTML.index(fromHTML.startIndex, offsetBy: range.location) ..<
fromHTML.index(fromHTML.startIndex, offsetBy: range.location+range.length)
title = String(fromHTML[r])
}
return title
}
class func getSampleCodeURL(fromHTML: String) -> [String] {
let pat = "\\b.*(href=\".*/content/samplecode/.*\")\\b"
let regex = try! NSRegularExpression(pattern: pat, options: [])
let matches = regex.matches(in: fromHTML, options: [], range: NSRange(location: 0, length: fromHTML.count))
var sampleURLPaths : [String] = []
for match in matches {
let range = match.range(at: 1)
let r = fromHTML.index(fromHTML.startIndex, offsetBy: range.location) ..<
fromHTML.index(fromHTML.startIndex, offsetBy: range.location+range.length)
var path = String(fromHTML[r])
// Tack on the hostname if it's not already there (some URLs are listed as
// relative URL while some are fully-qualified).
let prefixReplacementString: String
if path.contains("href=\"http") == false {
prefixReplacementString = "https://developer.apple.com"
} else {
prefixReplacementString = ""
}
path = path.replacingOccurrences(of: "href=\"", with: prefixReplacementString)
// Strip target attribute suffix
path = path.replacingOccurrences(of: "\" target=\"", with: "/")
sampleURLPaths.append(path)
}
var sampleArchivePaths : [String] = []
for urlPath in sampleURLPaths {
let jsonText = getStringContent(fromURL: urlPath + "book.json")
if let data = jsonText.data(using: .utf8) {
let object = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
if let dictionary = object as? NSDictionary {
if let relativePath = dictionary["sampleCode"] as? String {
sampleArchivePaths.append(urlPath + relativePath)
}
}
}
}
return sampleArchivePaths
}
class func getStringContent(fromURL: String) -> (String) {
/* Configure session, choose between:
* defaultSessionConfiguration
* ephemeralSessionConfiguration
* backgroundSessionConfigurationWithIdentifier:
And set session-wide properties, such as: HTTPAdditionalHeaders,
HTTPCookieAcceptPolicy, requestCachePolicy or timeoutIntervalForRequest.
*/
/* Create session, and optionally set a URLSessionDelegate. */
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
/* Create the Request:
My API (2) (GET https://developer.apple.com/videos/play/wwdc2017/201/)
https://developer.apple.com/videos/play/wwdc2017/102/
*/
var result = ""
guard let URL = URL(string: fromURL) else {return result}
var request = URLRequest(url: URL)
request.httpMethod = "GET"
/* Start a new Task */
let semaphore = DispatchSemaphore.init(value: 0)
let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) -> Void in
if (error == nil) {
/* Success */
// let statusCode = (response as! NSHTTPURLResponse).statusCode
// print("URL Session Task Succeeded: HTTP \(statusCode)")
result = String.init(data: data!, encoding:
.ascii)!
}
else {
/* Failure */
print("URL Session Task Failed: %@", error!.localizedDescription);
}
semaphore.signal()
})
task.resume()
semaphore.wait()
return result
}
class func getSessionsList(fromHTML: String) -> Array<String> {
let pat = "\"\\/videos\\/play\\/wwdc2017\\/([0-9]*)\\/\""
let regex = try! NSRegularExpression(pattern: pat, options: [])
let matches = regex.matches(in: fromHTML, options: [], range: NSRange(location: 0, length: fromHTML.count))
var sessionsListArray = [String]()
for match in matches {
for n in 0..<match.numberOfRanges {
let range = match.range(at: n)
let r = fromHTML.index(fromHTML.startIndex, offsetBy: range.location) ..<
fromHTML.index(fromHTML.startIndex, offsetBy: range.location+range.length)
switch n {
case 1:
//print(htmlSessionList.substring(with: r))
sessionsListArray.append(String(fromHTML[r]))
default: break
}
}
}
return sessionsListArray
}
class func downloadFile(urlString: String, forSession sessionIdentifier: String = "???") {
var fileName = URL(fileURLWithPath: urlString).lastPathComponent
if fileName.hasPrefix(sessionIdentifier) == false {
fileName = "\(sessionIdentifier)_\(fileName)"
}
guard !FileManager.default.fileExists(atPath: "./" + fileName) else {
print("\(fileName): already exists, nothing to do!")
return
}
print("[Session \(sessionIdentifier)] Getting \(fileName) (\(urlString)):")
guard let url = URL(string: urlString) else {
print("<\(urlString)> is not valid URL!")
return
}
DownloadSessionManager.sharedInstance.downloadFile(fromURL: url, toPath: "\(fileName)")
}
}
func showHelpAndExit() {
print("wwdc2017 - a simple swifty video sessions bulk download.\nJust Get'em all!")
print("usage: wwdc2017.swift [--hd] [--sd] [--pdf] [--pdf-only] [--sessions] [--sample] [--sample-only] [--list-only] [--help]\n")
exit(0)
}
/* Managing options */
var format = VideoQuality.HD
var shouldDownloadPDFResource = false
var shouldDownloadVideoResource = true
var shouldDownloadSampleCodeResource = false
var gettingSessions = false
var sessionsSet:Set<String> = Set()
var arguments = CommandLine.arguments
arguments.remove(at: 0)
for argument in arguments {
switch argument {
case "-h", "--help":
showHelpAndExit()
break
case "--hd":
format = .HD
gettingSessions = false
case "--sd":
format = .SD
gettingSessions = false
case "--pdf":
shouldDownloadPDFResource = true
gettingSessions = false
case "--pdf-only":
shouldDownloadPDFResource = true
shouldDownloadVideoResource = false
gettingSessions = false
case "--sample":
shouldDownloadSampleCodeResource = true
gettingSessions = false
case "--sample-only":
shouldDownloadSampleCodeResource = true
shouldDownloadVideoResource = false
gettingSessions = false
case "--sessions", "-s":
gettingSessions = true
break
case "--list-only", "-l":
shouldDownloadVideoResource = false
break;
case _ where Int(argument) != nil:
if(!gettingSessions) {
fallthrough
}
sessionsSet.insert(argument)
break
default:
print("\(argument) is not a \(#file) command.\n")
showHelpAndExit()
}
}
if(shouldDownloadVideoResource) {
switch format {
case .HD:
print("Downloading HD videos in current directory")
break
case .SD:
print("Downloading SD videos in current directory")
break
}
}
func sortFunc(value1: String, value2: String) -> Bool {
let filteredVal1 = value1[..<value1.index(value1.startIndex, offsetBy: 3)]
let filteredVal2 = value2[..<value2.index(value2.startIndex, offsetBy: 3)]
return filteredVal1 < filteredVal2;
}
/* Retreiving list of all video session */
let htmlSessionListString = wwdcVideosController.getStringContent(fromURL: "https://developer.apple.com/videos/wwdc2017/")
print("Let me ask Apple about currently available sessions. This can take some time (15 to 20 sec.) ...")
var sessionsListArray = wwdcVideosController.getSessionsList(fromHTML: htmlSessionListString)
//get unique values
sessionsListArray=Array(Set(sessionsListArray))
/* getting individual videos */
if sessionsSet.count != 0 {
let sessionsListSet = Set(sessionsListArray)
sessionsListArray = Array(sessionsSet.intersection(sessionsListSet))
}
sessionsListArray.sort(by: sortFunc)
for (_, value) in sessionsListArray.enumerated() {
let htmlText = wwdcVideosController.getStringContent(fromURL: "https://developer.apple.com/videos/play/wwdc2017/" + value + "/")
let title = wwdcVideosController.getTitle(fromHTML: htmlText)
print("\n[Session \(value)] : \(title)")
if shouldDownloadVideoResource {
let videoURLString = wwdcVideosController.getHDorSDdURLs(fromHTML: htmlText, format: format)
if videoURLString.isEmpty {
print("Video : Video is not yet available !!!")
} else {
print("Video : \(videoURLString)")
wwdcVideosController.downloadFile(urlString: videoURLString, forSession: value)
}
}
if shouldDownloadPDFResource {
let pdfResourceURLString = wwdcVideosController.getPDFResourceURL(fromHTML: htmlText)
if pdfResourceURLString.isEmpty {
print("PDF : PDF is not yet available !!!")
} else {
print("PDF : \(pdfResourceURLString)")
wwdcVideosController.downloadFile(urlString: pdfResourceURLString, forSession: value)
}
}
if shouldDownloadSampleCodeResource {
let sampleURLPaths = wwdcVideosController.getSampleCodeURL(fromHTML: htmlText)
if sampleURLPaths.isEmpty {
print("SampleCode: Resource not yet available !!!")
} else {
print("SampleCode: ")
for path in sampleURLPaths {
print("\(path)")
wwdcVideosController.downloadFile(urlString: path, forSession: value)
}
}
}
}