forked from Ascoware/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITVDownload.swift
387 lines (308 loc) · 14.7 KB
/
ITVDownload.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
//
// ITVDownload.swift
// Get iPlayer Automator
//
// Created by Scott Kovatch on 1/1/18.
//
import Foundation
import Kanna
import SwiftyJSON
import CocoaLumberjackSwift
@objc public class ITVDownload : Download {
var maxResolution: String
override public var description: String {
return "ITV Download (ID=\(show.pid))"
}
@objc public init(programme: Programme, proxy: HTTPProxy?) {
self.maxResolution = UserDefaults.standard.string(forKey: "MaxITVScreenHeight") ?? ""
super.init()
self.proxy = proxy
self.show = programme
self.defaultsPrefix = "ITV_"
self.downloadPath = UserDefaults.standard.string(forKey: "DownloadPath") ?? ""
self.running = true
setCurrentProgress("Retrieving Programme Metadata... \(show.showName)")
setPercentage(102)
programme.status = "Initialising..."
DDLogInfo("Downloading \(show.showName)")
DispatchQueue.main.async {
guard let requestURL = URL(string: self.show.url) else {
return
}
self.currentRequest?.cancel()
var downloadRequest = URLRequest(url:requestURL)
downloadRequest.timeoutInterval = 10
var session = URLSession.shared
if let proxy = self.proxy {
// Create an NSURLSessionConfiguration that uses the proxy
var proxyDict: [AnyHashable : Any] = [kCFNetworkProxiesHTTPEnable : true,
kCFNetworkProxiesHTTPProxy : proxy.host,
kCFNetworkProxiesHTTPPort : proxy.port,
kCFNetworkProxiesHTTPSEnable : true,
kCFNetworkProxiesHTTPSProxy : proxy.host,
kCFNetworkProxiesHTTPSPort : proxy.port]
if let user = proxy.user, let password = proxy.password {
proxyDict[kCFProxyUsernameKey] = user
proxyDict[kCFProxyPasswordKey] = password
}
let configuration = URLSessionConfiguration.ephemeral
configuration.connectionProxyDictionary = proxyDict
// Create a NSURLSession with our proxy aware configuration
session = URLSession(configuration:configuration, delegate:nil, delegateQueue:OperationQueue.main)
}
DDLogVerbose("ITV: Requesting Metadata")
self.currentRequest = session.dataTask(with: downloadRequest) { (data, response, error) in
if let httpResponse = response as? HTTPURLResponse {
self.metaRequestFinished(response: httpResponse, data: data, error: error)
}
}
self.currentRequest?.resume()
}
}
func metaRequestFinished(response: HTTPURLResponse, data: Data?, error: Error?) {
guard self.running else {
return
}
guard response.statusCode != 0 || response.statusCode == 200, let data = data, let responseString = String(data:data, encoding:.utf8) else {
var message: String = ""
if response.statusCode == 0 {
message = "ERROR: No response received (probably a proxy issue): \(error?.localizedDescription ?? "Unknown error")"
self.show.reasonForFailure = "Internet_Connection"
self.show.status = "Failed: Bad Proxy"
} else {
message = "ERROR: Could not retrieve programme metadata: \(error?.localizedDescription ?? "Unknown error")"
self.show.status = "Download Failed"
}
self.show.successful = false
self.show.complete = true
DDLogError(message)
NotificationCenter.default.post(name: NSNotification.Name(rawValue:"DownloadFinished"), object:self.show)
return
}
DDLogDebug("DEBUG: Metadata response status code: \(response.statusCode)")
let showMetadata: [Programme]
if show.tvNetwork.hasPrefix("ITV") {
showMetadata = ITVMetadataExtractor.getShowMetadata(htmlPageContent: responseString)
} else {
showMetadata = STVMetadataExtractor.getShowMetadata(html: responseString)
}
if showMetadata.count == 0 {
DDLogError("ERROR: Metadata failure -- most likely this means the show was DRM protected and therefore not supported.")
self.show.complete = true
self.show.successful = false
self.show.status = "Download Failed: DRM protected"
self.show.reasonForFailure = "DRMProtected"
NotificationCenter.default.removeObserver(self)
NotificationCenter.default.post(name: NSNotification.Name(rawValue:"DownloadFinished"), object:self.show)
return
}
let showInfo = showMetadata[0]
self.show.desc = showInfo.desc
self.show.episode = showInfo.episode
self.show.season = showInfo.season
self.show.episodeName = showInfo.episodeName
self.show.thumbnailURLString = showInfo.thumbnailURLString
DDLogInfo("INFO: Metadata processed.")
//Create Download Path
self.createDownloadPath()
// show.path will be set when youtube-dl tells us the destination.
DispatchQueue.main.async {
self.launchYoutubeDL()
}
}
@objc public func youtubeDLProgress(progressNotification: Notification?) {
guard let fileHandle = progressNotification?.object as? FileHandle else {
return
}
guard let data = progressNotification?.userInfo?[NSFileHandleNotificationDataItem] as? Data,
data.count > 0,
let s = String(data: data, encoding: .utf8) else {
return
}
fileHandle.readInBackgroundAndNotify()
let lines = s.components(separatedBy: .newlines)
for line in lines {
DDLogInfo(line)
if line.contains("Writing video subtitles") {
//ITV Download (ID=2a4910a0046): [info] Writing video subtitles to: /Users/skovatch/Movies/TV Shows/LA Story/LA Story - Just Friends - 2a4910a0046.en.vtt
let scanner = Scanner(string: line)
scanner.scanUpToString("to: ")
scanner.scanString("to: ")
subtitlePath = scanner.scanUpToString("\n") ?? ""
DDLogDebug("Subtitle path = \(subtitlePath)")
}
if line.contains("Destination: ") {
let scanner = Scanner(string: line)
scanner.scanUpToString("Destination: ")
scanner.scanString("Destination: ")
self.show.path = scanner.scanUpToString("\n") ?? ""
DDLogDebug("Downloading to \(self.show.path)")
}
// youtube-dl native download generates a percentage complete and ETA remaining
var progress: String? = nil
var remaining: String? = nil
if line.contains("[download]") {
let scanner = Scanner(string: line)
scanner.scanUpToString("[download]")
scanner.scanString("[download]")
progress = scanner.scanUpToString("%")?.trimmingCharacters(in: .whitespaces)
scanner.scanUpToString("ETA ")
scanner.scanString("ETA ")
remaining = scanner.scanUpToCharactersFromSet(set: .whitespacesAndNewlines)
if let progress = progress, let progressVal = Double(progress) {
setPercentage(progressVal)
show.status = "Downloaded \(progress)%"
}
if let remaining = remaining {
setCurrentProgress("Downloading \(show.showName) -- \(remaining) until done")
}
}
if line.hasSuffix("has already been downloaded") {
let scanner = Scanner(string: line)
scanner.scanUpToString("[download]")
scanner.scanString("[download]")
self.show.path = scanner.scanUpToString("has already been downloaded")?.trimmingCharacters(in: .whitespaces) ?? ""
}
if line.hasPrefix("WARNING: Failed to download m3u8 information") {
self.show.reasonForFailure = "proxy"
}
}
}
public func youtubeDLTaskFinished(_ proc: Process) {
DDLogInfo("yt-dlp finished downloading")
self.task = nil
self.pipe = nil
self.errorPipe = nil
let exitCode = proc.terminationStatus
if exitCode == 0 {
self.show.complete = true
self.show.successful = true
let info = ["Programmes" : [self.show]]
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "AddProgToHistory"), object:self, userInfo:info)
self.youtubeDLFinishedDownload()
}
} else {
self.show.complete = true
self.show.successful = false
self.show.status = "Failed"
// Something went wrong inside youtube-dl.
NotificationCenter.default.removeObserver(self)
NotificationCenter.default.post(name: NSNotification.Name(rawValue:"DownloadFinished"), object:self.show)
}
}
@objc public func youtubeDLFinishedDownload() {
if UserDefaults.standard.bool(forKey: "TagShows") {
tagDownloadWithMetadata()
} else {
atomicParsleyFinished(nil)
}
}
private func launchYoutubeDL() {
setCurrentProgress("Downloading \(show.showName)")
setPercentage(102)
show.status = "Downloading..."
task = Process()
pipe = Pipe()
errorPipe = Pipe()
task?.standardInput = FileHandle.nullDevice
task?.standardOutput = pipe
task?.standardError = errorPipe
let fh = pipe?.fileHandleForReading
let errorFh = errorPipe?.fileHandleForReading
guard let youtubeDLFolder = Bundle.main.path(forResource: "yt-dlp_macos", ofType:nil),
let cacertFile = Bundle.main.url(forResource: "cacert", withExtension: "pem") else {
return
}
let youtubeDLBinary = youtubeDLFolder + "/yt-dlp_macos"
var args: [String] = [show.url,
"--user-agent",
"Mozilla/5.0",
"-o",
downloadPath]
var maxResolutionInt = 720
if let mappedFormat = stvFormats[self.maxResolution] as? String, let formatInt = Int(mappedFormat) {
maxResolutionInt = formatInt
}
if maxResolutionInt > 0 {
args.append("-f")
args.append("best[height<=\(maxResolutionInt)]")
hdVideo = maxResolutionInt >= 720
}
if UserDefaults.standard.bool(forKey: "DownloadSubtitles") {
args.append("--write-sub")
args.append("--sub-format")
args.append("dfxp/vtt")
args.append("--convert-subtitles")
args.append("srt")
if UserDefaults.standard.bool(forKey: "EmbedSubtitles") {
args.append("--embed-subs")
}
}
if UserDefaults.standard.bool(forKey: "Verbose") {
args.append("--verbose")
}
if UserDefaults.standard.bool(forKey: "TagShows") {
args.append("--embed-thumbnail")
}
if let proxyHost = self.proxy?.host {
var proxyString = ""
if let user = self.proxy?.user, let password = self.proxy?.password {
proxyString += "\(user):\(password)@"
}
proxyString += proxyHost
if let port = self.proxy?.port {
proxyString += ":\(port)"
}
args.append("--proxy")
args.append(proxyString)
}
DDLogVerbose("DEBUG: youtube-dl args:\(args)")
task?.launchPath = youtubeDLBinary
task?.arguments = args
let extraBinaryPath = AppController.shared().extraBinariesPath
var envVariableDictionary = [String : String]()
envVariableDictionary["PATH"] = "\(youtubeDLFolder):\(extraBinaryPath)"
envVariableDictionary["SSL_CERT_FILE"] = cacertFile.path
task?.environment = envVariableDictionary
DDLogVerbose("DEBUG: youtube-dl environment: \(envVariableDictionary)")
NotificationCenter.default.addObserver(self, selector: #selector(self.youtubeDLProgress), name: FileHandle.readCompletionNotification, object: fh)
NotificationCenter.default.addObserver(self, selector: #selector(self.youtubeDLProgress), name: FileHandle.readCompletionNotification, object: errorFh)
task?.terminationHandler = youtubeDLTaskFinished
task?.launch()
fh?.readInBackgroundAndNotify()
errorFh?.readInBackgroundAndNotify()
}
func createDownloadPath() {
var fileName = show.showName
// XBMC naming is always used on ITV shows to ensure unique names.
if !show.seriesName.isEmpty {
fileName = show.seriesName;
}
if show.season == 0 {
show.season = 1
if show.episode == 0 {
show.episode = 1
}
}
let format = !show.episodeName.isEmpty ? "%@.s%02lde%02ld.%@" : "%@.s%02lde%02ld"
fileName = String(format: format, fileName, show.season, show.episode, show.episodeName)
//Create Download Path
var dirName = show.seriesName
if dirName.isEmpty {
dirName = show.showName
}
downloadPath = UserDefaults.standard.string(forKey:"DownloadPath") ?? ""
dirName = dirName.replacingOccurrences(of: "/", with: "-").replacingOccurrences(of: ":", with: " -")
downloadPath = (downloadPath as NSString).appendingPathComponent(dirName)
var filepart = String(format:"%@.%%(ext)s",fileName).replacingOccurrences(of: "/", with: "-").replacingOccurrences(of: ":", with: " -")
do {
try FileManager.default.createDirectory(atPath: downloadPath, withIntermediateDirectories: true)
let dateRegex = try NSRegularExpression(pattern: "(\\d{2})[-_](\\d{2})[-_](\\d{4})")
filepart = dateRegex.stringByReplacingMatches(in: filepart, range: NSRange(location: 0, length: filepart.count), withTemplate: "$3-$2-$1")
} catch {
DDLogError("Failed to create download directory! ")
}
downloadPath = (downloadPath as NSString).appendingPathComponent(filepart)
}
}