forked from Ascoware/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramme.swift
555 lines (463 loc) · 19.5 KB
/
Programme.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
// Converted to Swift 5.7 by Swiftify v5.7.28606 - https://swiftify.com/
//
// Programme.m
// Get_iPlayer GUI
//
// Created by Thomas Willson on 7/13/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
import Foundation
import CocoaLumberjackSwift
@objc enum ProgrammeType : Int {
case tv
case radio
case itv
}
@objc(Programme)
@objcMembers public class Programme : NSObject, NSSecureCoding {
private var getNameRunning = false
dynamic var tvNetwork: String = ""
dynamic var showName: String = ""
dynamic var pid: String = ""
dynamic var status: String = ""
dynamic var seriesName: String = ""
dynamic var episodeName: String = ""
dynamic var complete = false
dynamic var successful = false
dynamic var timeadded: NSNumber = 0
dynamic var path: String = ""
dynamic var season: Int = 0
dynamic var episode: Int = 0
var processedPID = false
var radio = false
var podcast = false
var realPID: String = ""
var subtitlePath: String = ""
var reasonForFailure: String = ""
var availableModes: String = ""
var url: String = ""
var desc: String = ""
//Extended Metadata
var extendedMetadataRetrieved = false
var successfulRetrieval = false
var duration: Int = 0
var categories: String = ""
// First broadcast: first time show was ever aired.
// Optional because we only fetch this with getName or extended metadata.
var firstBroadcast: Date?
// Last broadcast: most recent airing. Can be nil for an older program.
var lastBroadcast: Date?
dynamic var lastBroadcastString: String = ""
var modeSizes: [[String:String]] = []
var thumbnailURLString: String = ""
var thumbnail: NSImage?
var getiPlayerProxy: GetiPlayerProxy?
var addedByPVR = false
var type: ProgrammeType {
if radio {
return .radio
} else if tvNetwork.hasPrefix("ITV") {
return .itv
} else {
return .tv
}
}
var typeDescription: String {
let dic = [
NSNumber(value: ProgrammeType.tv.rawValue): "BBC TV",
NSNumber(value: ProgrammeType.radio.rawValue): "BBC Radio",
NSNumber(value: ProgrammeType.itv.rawValue): "ITV"
]
return dic[NSNumber(value: type.rawValue)] ?? "Unknown"
}
public override init() {
super.init()
status = runDownloads.boolValue ? "Waiting…" : ""
}
public class var supportsSecureCoding: Bool {
return true
}
public override var description: String {
return "\(pid): \(showName)"
}
public func encode(with coder: NSCoder) {
coder.encode(showName, forKey: "showName")
coder.encode(pid, forKey: "pid")
coder.encode(tvNetwork, forKey: "tvNetwork")
coder.encode(status, forKey: "status")
coder.encode(path, forKey: "path")
coder.encode(seriesName, forKey: "seriesName")
coder.encode(episodeName, forKey: "episodeName")
coder.encode(timeadded, forKey: "timeadded")
coder.encode(processedPID, forKey: "processedPID")
coder.encode(radio, forKey: "radio")
coder.encode(realPID, forKey: "realPID")
coder.encode(url, forKey: "url")
coder.encode(season, forKey: "season")
coder.encode(episode, forKey: "episode")
coder.encode(lastBroadcast, forKey: "lastBroadcast")
coder.encode(lastBroadcastString, forKey: "lastBroadcastString")
}
public required init?(coder: NSCoder) {
super.init()
pid = coder.decodeObject(forKey: "pid") as? String ?? ""
showName = coder.decodeObject(forKey: "showName") as? String ?? ""
tvNetwork = coder.decodeObject(forKey: "tvNetwork") as? String ?? ""
status = coder.decodeObject(forKey: "status") as? String ?? ""
complete = false
successful = false
path = coder.decodeObject(forKey: "path") as? String ?? ""
seriesName = coder.decodeObject(forKey: "seriesName") as? String ?? ""
episodeName = coder.decodeObject(forKey: "episodeName") as? String ?? ""
if let decodeObject = coder.decodeObject(forKey: "timeadded") as? NSNumber {
timeadded = decodeObject
}
processedPID = coder.decodeBool(forKey: "processedPID")
radio = coder.decodeBool(forKey: "radio")
realPID = coder.decodeObject(forKey: "realPID") as? String ?? ""
url = coder.decodeObject(forKey: "url") as? String ?? ""
subtitlePath = ""
reasonForFailure = ""
availableModes = ""
desc = ""
getNameRunning = false
addedByPVR = false
season = coder.decodeInteger(forKey: "season")
episode = coder.decodeInteger(forKey: "episode")
lastBroadcast = coder.decodeObject(forKey: "lastBroadcast") as? Date ?? Date()
lastBroadcastString = coder.decodeObject(forKey: "lastBroadcastString") as? String ?? ""
}
func retrieveExtendedMetadata() {
DDLogInfo("Retrieving Extended Metadata")
getiPlayerProxy = GetiPlayerProxy()
getiPlayerProxy?.loadInBackground(for: #selector(getRemoteMetadata(_:proxyDict:)), with: nil, onTarget: self, silently: false)
}
// Look for 'field:' at the beginning of a line in 'lines'. If found, and 'secondField' is empty, return the rest
// of the line after the whitespace beyond the 'field:'.
// If secondField is provided, treat the value portion of the line as a new key/value pair
// and look for 'secondField:'. Then return the rest of the line.
func scanField(_ field: String, lines: [String], secondField: String = "") -> String {
var value: String? = nil
for line in lines {
if line.hasPrefix("\(field):") {
let scanner = Scanner(string: line)
scanner.scanUpTo("\(field):", into: nil)
scanner.scanString("\(field):", into: nil)
scanner.scanCharacters(from: .whitespaces, into: nil)
value = scanner.scanUpToCharactersFromSet(set: .newlines)
if !secondField.isEmpty, let lineRemainder = value, lineRemainder.hasPrefix(secondField) {
let remainder = Scanner(string: lineRemainder)
remainder.scanUpTo("\(secondField):", into: nil)
remainder.scanString("\(secondField):", into: nil)
remainder.scanCharacters(from: .whitespaces, into: nil)
value = remainder.scanUpToCharactersFromSet(set: .newlines)
break
}
}
}
return value ?? ""
}
func cancelMetadataRetrieval() {
// if metadataTask?.isRunning ?? false {
// metadataTask?.interrupt()
// DDLogInfo("Metadata retrieval cancelled for \(description)");
// }
//
// taskOutput = ""
// metadataTask = nil
// pipe = nil
}
public override func isEqual(_ object: Any?) -> Bool {
if let other = object as? Programme {
return other.showName == showName && other.pid == pid
} else {
return false
}
}
func getNameSynchronous() {
getName()
while getNameRunning {
Thread.sleep(until: Date(timeIntervalSinceNow: 0.1))
}
}
func getName() {
autoreleasepool {
getNameRunning = true
let getNameTask = Process()
let getNamePipe = Pipe()
let listArgument = "--listformat=<index>|<pid>|<type>|<name>|<seriesnum>|<episode>|<channel>|<available>|<web>"
let fieldsArgument = "--fields=index,pid"
let wantedID = pid
let args = [
AppController.shared().getiPlayerPath,
GetiPlayerArguments.sharedController().noWarningArg,
GetiPlayerArguments.sharedController().cacheExpiryArg,
"--nopurge",
GetiPlayerArguments.sharedController().typeArgument(forCacheUpdate: false),
listArgument,
GetiPlayerArguments.sharedController().profileDirArg,
fieldsArgument,
wantedID
]
getNameTask.arguments = args
getNameTask.launchPath = AppController.shared().perlBinaryPath
getNameTask.standardOutput = getNamePipe
let getNameFh = getNamePipe.fileHandleForReading
var envVariableDictionary = [String : String]()
envVariableDictionary["HOME"] = (("~") as NSString).expandingTildeInPath
envVariableDictionary["PERL_UNICODE"] = "AS"
envVariableDictionary["PATH"] = AppController.shared().perlEnvironmentPath
getNameTask.environment = envVariableDictionary
getNameTask.launch()
let data = getNameFh.readDataToEndOfFile()
if let stringData = String(data: data, encoding: .utf8) {
processGetNameDataFromSearch(stringData)
}
}
}
@objc func processGetNameDataFromSearch(_ getNameData: String) {
let array = getNameData.components(separatedBy: .newlines)
let wantedID = self.pid
var found = false
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ"
for string in array {
// TODO: remove use of index in future version
let elements = string.components(separatedBy: "|")
if elements.count < 9 {
continue
}
var pid = "", showName = "", episode = "", index = "", type = "", tvNetwork = "", url = "", dateAired = "", season = ""
if elements.count == 9 {
index = elements[0]
pid = elements[1]
type = elements[2]
showName = elements[3]
season = elements[4]
episode = elements[5]
tvNetwork = elements[6]
dateAired = elements[7]
url = elements[8]
} else {
let getNameException = NSAlert()
getNameException.addButton(withTitle: "OK")
getNameException.messageText = "Unknown Error!"
getNameException.informativeText = "An unknown error occured whilst trying to parse Get_iPlayer output (processGetNameData)."
getNameException.alertStyle = .warning
getNameException.runModal()
}
if (wantedID == pid) || (wantedID == index) {
found = true
self.showName = showName
episodeName = episode
if let date = dateFormatter.date(from: dateAired) {
lastBroadcast = date
lastBroadcastString = DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .none)
}
if !pid.isEmpty {
self.pid = pid
}
if !tvNetwork.isEmpty {
self.tvNetwork = tvNetwork
}
if !url.isEmpty {
self.url = url
}
if !season.isEmpty, let seasonNum = Int(season) {
self.season = seasonNum
}
status = runDownloads.boolValue ? "Waiting…" : "Available"
if type == "radio" {
radio = true
}
}
break
}
processedPID = found
if !found {
status = "Not in cache"
if !tvNetwork.contains("ITV") && !tvNetwork.contains("STV") {
showName = "Retrieving Metadata..."
getNameFromPID()
} else {
getNameRunning = false
}
} else {
getNameRunning = false
}
}
func getNameFromPID() {
DDLogInfo("Retrieving Metadata For PID \(pid)")
getiPlayerProxy = GetiPlayerProxy()
getiPlayerProxy?.loadInBackground(for: #selector(getRemoteMetadata(_:proxyDict:)), with: nil, onTarget: self, silently: false)
}
@objc func getRemoteMetadata(_ sender: Any?, proxyDict: [AnyHashable : Any]) {
performSelector(inBackground: #selector(getRemoteMetadataThreadWithProxy(proxyDict:)), with: proxyDict)
}
@objc func getRemoteMetadataThreadWithProxy(proxyDict: [AnyHashable : Any]) {
autoreleasepool {
getiPlayerProxy = nil
if let error = proxyDict["error"] as? NSError, error.code == kProxyLoadCancelled {
return
}
let metadataTask = Process()
let metadataPipe = Pipe()
var args = [
AppController.shared().getiPlayerPath,
"--nopurge",
GetiPlayerArguments.sharedController().noWarningArg,
GetiPlayerArguments.sharedController().cacheExpiryArg,
GetiPlayerArguments.sharedController().profileDirArg,
"--info",
"--pid",
pid]
// Only add a --versions parameter for audio described or signed. Otherwise, let get_iplayer figure it out.
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
}
if needVersions {
nonDefaultVersions.append("default")
var versionArg = "--versions="
versionArg += nonDefaultVersions.joined(separator: ",")
args.append(versionArg)
}
if let httpProxy = proxyDict["proxy"] as? HTTPProxy {
args.append("-p\(httpProxy.url)")
if UserDefaults.standard.bool(forKey: "AlwaysUseProxy") == false {
args.append("--partial-proxy")
}
}
DDLogVerbose("get metadata args:")
for arg in args {
DDLogVerbose("\(arg)")
}
metadataTask.arguments = args
metadataTask.launchPath = AppController.shared().perlBinaryPath
metadataTask.standardOutput = metadataPipe
let getNameFh = metadataPipe.fileHandleForReading
var envVariableDictionary = [String : String]()
envVariableDictionary["HOME"] = (("~") as NSString).expandingTildeInPath
envVariableDictionary["PERL_UNICODE"] = "AS"
envVariableDictionary["PATH"] = AppController.shared().perlEnvironmentPath
metadataTask.environment = envVariableDictionary
metadataTask.launch()
let data = getNameFh.readDataToEndOfFile()
if let stringData = String(data: data, encoding: .utf8) {
processMetadataTaskOutput(stringData)
}
getNameRunning = false
}
}
func processMetadataTaskOutput(_ output: String) {
let outputLines = output.components(separatedBy: .newlines)
var validOutput: [String] = []
for line in outputLines {
if line.hasPrefix("INFO:") || line.hasPrefix("WARNING:") || line.isEmpty {
continue
}
validOutput.append(line)
}
// If the PID is valid and the show exists it will have a 'versions:' line.
// If that's not there no need to go any further.
var default_version: String? = nil
let info_versions = scanField("versions", lines: validOutput)
if info_versions.isEmpty {
status = "Not Available"
return
}
tvNetwork = scanField("channel", lines: validOutput)
radio = scanField("type", lines: validOutput) == "radio"
let versions = info_versions.components(separatedBy: ",")
for version in versions {
if (version == "default") || ((version == "original") && (default_version != "default")) || (default_version == nil && (version != "signed") && (version != "audiodescribed")) {
default_version = version
}
}
status = runDownloads.boolValue ? "Waiting…" : "Available"
categories = scanField("categories", lines: validOutput)
desc = scanField("desc", lines: validOutput)
let durationStr = scanField("runtime", lines: validOutput)
duration = Int(durationStr) ?? 0
let broadcast = scanField("firstbcast", lines: validOutput)
if !broadcast.isEmpty {
firstBroadcast = ISO8601DateFormatter().date(from: broadcast)
}
url = scanField("web", lines: validOutput)
seriesName = scanField("nameshort", lines: validOutput)
episodeName = scanField("episodeshort", lines: validOutput)
showName = scanField("longname", lines: validOutput)
let seasonNumber = scanField("seriesnum", lines: validOutput)
season = Int(seasonNumber) ?? 0
let episodeNumber = scanField("episodenum", lines: validOutput)
episode = Int(episodeNumber) ?? 0
// parse mode sizes
modeSizes.removeAll()
for version in versions {
var group: String? = nil
switch version {
case default_version:
group = "A"
case "signed":
group = "C"
case "audiodescribed":
group = "D"
default:
group = "B"
}
var modePairs: [[String: String]] = []
var allSizes = scanField("modesizes", lines: validOutput, secondField: version)
if allSizes.isEmpty {
allSizes = scanField("qualitysizes", lines: validOutput, secondField: version)
}
let sizePairs = allSizes.components(separatedBy: ",")
if !sizePairs.isEmpty {
for sizePair in sizePairs {
let components = sizePair.components(separatedBy: "=")
let mode = components[0]
let size = components[1]
var info: [String : String] = [:]
info["mode"] = mode
info["size"] = size
info["group"] = group
info["version"] = version == default_version ? "default" : version
modePairs.append(info)
}
}
modeSizes.append(contentsOf: modePairs)
}
let thumbURL = scanField("thumbnail", lines: validOutput)
DDLogDebug("Thumbnail URL: \(thumbURL)")
if let anUrl = URL(string: thumbURL) {
let request = URLRequest(url: anUrl)
let dataTask = URLSession.shared.dataTask(with: request) { [self] data, response, error in
thumbnailRequestFinished(data)
}
dataTask.resume()
} else {
thumbnailRequestFinished(nil)
}
}
func thumbnailRequestFinished(_ thumbnailData: Data?) {
if let thumbnailData {
if let data = NSImage(data: thumbnailData) {
thumbnail = data
}
}
successfulRetrieval = true
extendedMetadataRetrieved = true
NotificationCenter.default.post(name: NSNotification.Name("ExtendedInfoRetrieved"), object: self)
processedPID = true
}
}
extension Programme : Comparable {
public static func < (lhs: Programme, rhs: Programme) -> Bool {
return lhs.showName < rhs.showName
}
}