forked from GetiPlayerAutomator/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathBBCDownload.swift
398 lines (341 loc) · 16.4 KB
/
BBCDownload.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
// Converted to Swift 5.7 by Swiftify v5.7.28606 - https://swiftify.com/
//
// BBCDownload.swift
// Get iPlayer Automator
//
// Created by Scott Kovatch on 11/9/22.
//
import Foundation
import CocoaLumberjackSwift
@objc class BBCDownload: Download {
var reasonForFailure: String?
// MARK: Overridden Methods
required init(programme: Programme, tvFormatList: [TVFormat], radioFormatList: [RadioFormat], proxy: HTTPProxy?) {
super.init()
reasonForFailure = nil
self.proxy = proxy
show = programme
defaultsPrefix = "BBC_"
downloadPath = UserDefaults.standard.string(forKey: "DownloadPath") ?? ""
DDLogInfo("Downloading \(show.showName)")
//Initialize Formats
var formatArg = "--quality="
var formatStrings: [String] = []
if show.radio {
formatStrings = radioFormatList.compactMap { radioFormats[$0.format] as? String }
} else {
formatStrings = tvFormatList.compactMap { tvFormats[$0.format] as? String }
}
let commaSeparatedFormats = formatStrings.joined(separator: ",")
formatArg += commaSeparatedFormats
//Set Proxy Arguments
var proxyArg: String? = nil
var partialProxyArg: String? = nil
if let proxy {
proxyArg = "-p\(proxy.url)"
if UserDefaults.standard.bool(forKey: "AlwaysUseProxy") {
partialProxyArg = "--partial-proxy"
}
}
//Initialize the rest of the arguments
let noWarningArg = GetiPlayerArguments.sharedController().noWarningArg
let atomicParsleyPath = URL(fileURLWithPath: AppController.shared().extraBinariesPath).appendingPathComponent("AtomicParsley").path
let atomicParsleyArg = "--atomicparsley=\(atomicParsleyPath)"
let ffmpegArg = "--ffmpeg=\(URL(fileURLWithPath: AppController.shared().extraBinariesPath).appendingPathComponent("ffmpeg").path)"
let downloadPathArg = "--output=\(downloadPath)"
let subDirArg = "--subdir"
let progressArg = "--logprogress"
let getArg = "--pid"
let searchArg = show.pid
let whitespaceArg = "--whitespace"
//AudioDescribed & Signed
var needVersions = false
var nonDefaultVersions: [String] = []
if UserDefaults.standard.bool(forKey: "AudioDescribedNew") {
nonDefaultVersions.append("audiodescribed")
needVersions = true
}
if UserDefaults.standard.bool(forKey: "SignedNew") {
nonDefaultVersions.append("signed")
needVersions = true
}
//We don't want this to refresh now!
let cacheExpiryArg = GetiPlayerArguments.sharedController().cacheExpiryArg
let profileDirArg = GetiPlayerArguments.sharedController().profileDirArg
//Add Arguments that can't be NULL
var args = [AppController.shared().getiPlayerPath, profileDirArg, noWarningArg, atomicParsleyArg, cacheExpiryArg, downloadPathArg, subDirArg, progressArg, formatArg, getArg, searchArg, whitespaceArg, "--attempts=5", "--thumbsize=640", ffmpegArg, "--log-progress"]
if let proxyArg {
args.append(proxyArg)
}
if let partialProxyArg {
args.append(partialProxyArg)
}
// Only add a --versions parameter for audio described or signed. Otherwise, let get_iplayer figure it out.
if needVersions {
nonDefaultVersions.append("default")
var versionArg = "--versions="
versionArg += nonDefaultVersions.joined(separator: ",")
args.append(versionArg)
}
//Verbose?
if UserDefaults.standard.bool(forKey: "Verbose") {
args.append("--verbose")
}
if UserDefaults.standard.bool(forKey: "DownloadSubtitles") {
args.append("--subtitles")
if UserDefaults.standard.bool(forKey: "EmbedSubtitles") {
args.append("--subs-embed")
}
}
//Naming Convention
if !UserDefaults.standard.bool(forKey: "XBMC_naming") {
args.append("--file-prefix=<name> - <episode> ((<modeshort>))")
} else {
args.append("--file-prefix=<nameshort><.senum><.episodeshort>")
args.append("--subdir-format=<nameshort>")
}
// 50 FPS frames?
if UserDefaults.standard.bool(forKey: "Use25FPSStreams") {
args.append("--tv-lower-bitrate")
}
//Tagging
if !UserDefaults.standard.bool(forKey: "TagShows") {
args.append("--no-tag")
}
for arg in args {
DDLogVerbose("\(arg)")
}
if UserDefaults.standard.bool(forKey: "TagRadioAsPodcast") {
args.append("--tag-podcast-radio")
show.podcast = true
}
task = Process()
pipe = Pipe()
errorPipe = Pipe()
task?.arguments = args
task?.launchPath = AppController.shared().perlBinaryPath
task?.standardOutput = pipe
task?.standardError = errorPipe
var envVariableDictionary = [String: String]()
envVariableDictionary["HOME"] = (("~") as NSString).expandingTildeInPath
envVariableDictionary["PERL_UNICODE"] = "AS"
envVariableDictionary["PATH"] = AppController.shared().perlEnvironmentPath
task?.environment = envVariableDictionary
let fh = pipe?.fileHandleForReading
let errorFh = errorPipe?.fileHandleForReading
NotificationCenter.default.addObserver(
self,
selector: #selector(downloadDataNotification(_:)),
name: FileHandle.readCompletionNotification,
object: fh)
fh?.readInBackgroundAndNotify()
NotificationCenter.default.addObserver(
self,
selector: #selector(downloadDataNotification(_:)),
name: FileHandle.readCompletionNotification,
object: errorFh)
errorFh?.readInBackgroundAndNotify()
NotificationCenter.default.addObserver(
self,
selector: #selector(downloadFinished(_:)),
name: Process.didTerminateNotification,
object: task)
task?.launch()
//Prepare UI
setCurrentProgress("Starting download...")
show.status = "Starting.."
}
override var description: String {
return "BBC Download (ID=\(show.pid))"
}
// MARK: Task Control
@objc func downloadDataNotification(_ n: Notification?) {
if let data = n?.userInfo?[NSFileHandleNotificationDataItem] as? Data,
let s = String(data: data, encoding: .utf8) {
processGetiPlayerOutput(s)
}
let fh = n?.object as? FileHandle
fh?.readInBackgroundAndNotify()
}
@objc func downloadFinished(_ notification: Notification?) {
if runDownloads.boolValue {
complete()
}
NotificationCenter.default.removeObserver(self)
NotificationCenter.default.post(name: NSNotification.Name("DownloadFinished"), object: show)
task = nil
pipe = nil
errorPipe = nil
}
func complete() {
// If we have a path it was successful. Note that and return.
if !show.path.isEmpty {
show.complete = true
show.successful = true
show.status = "Download Complete"
return
}
// Handle all other error cases.
show.complete = true
show.successful = false
if let reasonForFailure {
show.reasonForFailure = reasonForFailure
}
if reasonForFailure == "FileExists" {
show.status = "Failed: File already exists"
DDLogError("\(show.showName) failed, already exists")
} else if reasonForFailure == "ShowNotFound" {
show.status = "Failed: PID not found"
} else if reasonForFailure == "proxy" {
let proxyOption = UserDefaults.standard.string(forKey: "Proxy")
if proxyOption == "None" {
show.status = "Failed: See Log"
DDLogError("REASON FOR FAILURE: VPN or System Proxy failed. If you are using a VPN or a proxy configured in System Preferences, contact the VPN or proxy provider for assistance.")
show.reasonForFailure = "ShowNotFound"
} else if proxyOption == "Provided" {
show.status = "Failed: Bad Proxy"
DDLogError("REASON FOR FAILURE: Proxy failed. If in the UK, please disable the proxy in the preferences.")
show.reasonForFailure = "Provided_Proxy"
} else if proxyOption == "Custom" {
show.status = "Failed: Bad Proxy"
DDLogError("REASON FOR FAILURE: Proxy failed. If in the UK, please disable the proxy in the preferences.")
DDLogError("If outside the UK, please use a different proxy.")
show.reasonForFailure = "Custom_Proxy"
}
DDLogError("\(show.showName) failed")
} else if reasonForFailure == "Specified_Modes" {
show.status = "Failed: No Specified Modes"
DDLogError("REASON FOR FAILURE: None of the modes in your download format list are available for this show.")
DDLogError("Try adding more modes.")
DDLogError("\(show.showName) failed")
} else if reasonForFailure == "InHistory" {
show.status = "Failed: In download history"
DDLogError("InHistory")
} else if reasonForFailure == "AudioDescribedOnly" {
show.reasonForFailure = "AudioDescribedOnly"
} else if reasonForFailure == "External_Disconnected" {
show.status = "Failed: HDD not Accessible"
DDLogError("REASON FOR FAILURE: The specified download directory could not be written to.")
DDLogError("Most likely this is because your external hard drive is disconnected but it could also be a permission issue")
DDLogError("\(show.showName) failed")
} else if reasonForFailure == "Download_Directory_Permissions" {
show.status = "Failed: Download Directory Unwriteable"
DDLogError("REASON FOR FAILURE: The specified download directory could not be written to.")
DDLogError("Please check the permissions on your download directory.")
DDLogError("\(show.showName) failed")
} else {
// Failed for an unknown reason.
show.status = "Download Failed"
DDLogError("\(show.showName) failed")
}
}
func processGetiPlayerOutput(_ outp: String?) {
let array = outp?.components(separatedBy: .newlines)
//Parse each line individually.
for output in array ?? [] {
if output.count == 0 {
continue
}
if output.hasPrefix("DEBUG:") {
DDLogDebug("\(output)")
} else if output.hasPrefix("WARNING:") {
DDLogWarn("\(output)")
} else {
DDLogInfo("\(output)")
}
if output.hasPrefix("INFO: Downloading subtitles") {
let scanner = Scanner(string: output)
var srtPath: String?
scanner.scanString("INFO: Downloading Subtitles to \'", into: nil)
srtPath = scanner.scanUpToString(".srt\'")
srtPath = URL(fileURLWithPath: srtPath ?? "").appendingPathExtension("srt").path
show.subtitlePath = srtPath ?? ""
} else if output.hasPrefix("INFO: Wrote file ") {
let scanner = Scanner(string: output)
scanner.scanString("INFO: Wrote file ", into: nil)
let path = scanner.scanUpToCharactersFromSet(set: .newlines)
show.path = path ?? ""
} else if output.hasPrefix("INFO: No specified modes") && output.hasSuffix("--quality=)") {
reasonForFailure = "Specified_Modes"
let modeScanner = Scanner(string: output)
modeScanner.scanUpTo("--quality=", into: nil)
modeScanner.scanString("--quality=", into: nil)
let availableModes = modeScanner.scanUpToString(")")
show.availableModes = availableModes ?? ""
} else if output.hasSuffix("use --force to override") {
reasonForFailure = "InHistory"
} else if output.contains("Permission denied") {
if output.contains("/Volumes") {
//Most likely disconnected external HDD {
reasonForFailure = "External_Disconnected"
} else {
reasonForFailure = "Download_Directory_Permissions"
}
} else if output.hasPrefix("WARNING: Use --overwrite") {
reasonForFailure = "FileExists"
} else if output.hasPrefix("ERROR: Failed to get version pid") {
reasonForFailure = "ShowNotFound"
} else if output.hasPrefix("WARNING: If you use a VPN") || output.hasSuffix("blocked by the BBC") {
reasonForFailure = "proxy"
} else if output.hasPrefix("WARNING: No programmes are available for this pid with version(s):") || output.hasPrefix("INFO: No versions of this programme were selected") {
let versionScanner = Scanner(string: output)
versionScanner.scanUpTo("available versions:", into: nil)
versionScanner.scanString("available versions:", into: nil)
versionScanner.scanCharacters(from: .whitespaces, into: nil)
let availableVersions = versionScanner.scanUpToString(")")
if (availableVersions as NSString?)?.range(of: "audiodescribed").location != NSNotFound || (availableVersions as NSString?)?.range(of: "signed").location != NSNotFound {
reasonForFailure = "AudioDescribedOnly"
}
} else if output.hasPrefix("INFO: Downloading thumbnail") {
show.status = "Downloading Artwork.."
setPercentage(102)
setCurrentProgress("Downloading Artwork.. -- \(show.showName)")
} else if output.hasPrefix("INFO:") || output.hasPrefix("WARNING:") || output.hasPrefix("ERROR:") || output.hasSuffix("default") || output.hasPrefix(show.pid) {
// Do nothing! This ensures we don't process any other info messages
} else if output.hasSuffix("[audio+video]") || output.hasSuffix("[audio]") || output.hasSuffix("[video]") {
//Process iPhone/Radio Downloads Status Message
let scanner = Scanner(string: output)
var percentage: Decimal?
var h: Decimal?
var m: Decimal?
var s: Decimal?
scanner.scanUpToCharacters(
from: .decimalDigits,
into: nil)
percentage = scanner.scanDecimal()
setPercentage(NSDecimalNumber(decimal: percentage ?? .zero).doubleValue)
// Jump ahead to the ETA field.
scanner.scanUpTo("ETA: ", into: nil)
scanner.scanString("ETA: ", into: nil)
scanner.scanUpToCharacters(
from: .decimalDigits,
into: nil)
h = scanner.scanDecimal()
scanner.scanUpToCharacters(
from: .decimalDigits,
into: nil)
m = scanner.scanDecimal()
scanner.scanUpToCharacters(
from: .decimalDigits,
into: nil)
s = scanner.scanDecimal()
scanner.scanUpToCharacters(
from: .decimalDigits,
into: nil)
let eta = String(format: "%.2ld:%.2ld:%.2ld remaining", NSDecimalNumber(decimal: h ?? .zero).intValue, NSDecimalNumber(decimal: m ?? .zero).intValue, NSDecimalNumber(decimal: s ?? .zero).intValue)
setCurrentProgress(eta)
var format = "Video downloaded: %ld%%"
if output.hasSuffix("[audio+video]") {
format = "Downloaded %ld%%"
} else if output.hasSuffix("[audio]") {
format = "Audio download: %ld%%"
} else if output.hasSuffix("[video]") {
format = "Video download: %ld%%"
}
if let percentage {
show.status = String(format: format, NSDecimalNumber(decimal: percentage).intValue)
}
}
}
}
}