forked from GetiPlayerAutomator/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 26
/
GetCurrentWebpage.swift
283 lines (242 loc) · 11.8 KB
/
GetCurrentWebpage.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
// Converted with Swiftify v1.0.6472 - https://objectivec2swift.com/
//
// GetCurrentWebpage.swift
// Get_iPlayer GUI
//
import ScriptingBridge
import SwiftyJSON
import Kanna
import CocoaLumberjackSwift
@objc public class GetCurrentWebpage : NSObject {
private class func extractMetadata(url: String, tabTitle: String, pageSource: String, completion: ([Programme]) -> Void) {
if url.hasPrefix("https://www.bbc.co.uk/iplayer/episode/") {
// PID is always the second-to-last element in the URL.
let show = Programme()
if let nsUrl = URL(string: url) {
show.pid = nsUrl.deletingLastPathComponent().lastPathComponent
}
let nameScanner = Scanner(string: tabTitle)
nameScanner.scanString("BBC iPlayer - ")
show.showName = nameScanner.scanUpToString( " - ") ?? ""
show.tvNetwork = "BBC"
completion([show])
return
} else if url.hasPrefix("https://www.bbc.co.uk/iplayer/episodes/") {
// https://www.bbc.co.uk/iplayer/episodes/p00yzlr0/line-of-duty?seriesId=b01k9pm3
// It looks like a PID, but it's a 'brand ID' The real URL is embedded in an anchor tag.
let show = Programme()
if let htmlPage = try? HTML(html: pageSource, encoding: .utf8) {
// There should only be one 'video' element.
if let anchorElement = htmlPage.at_xpath("//a[@class='play-cta__inner play-cta__inner--do-not-wrap play-cta__inner--link']") {
let showURLString = anchorElement.at_xpath("//@href")?.text ?? ""
if let pageURL = URL(string: url), let showURL = URL(string: showURLString, relativeTo: pageURL) {
show.pid = showURL.deletingLastPathComponent().lastPathComponent
show.url = showURL.absoluteString
}
}
}
let nameScanner = Scanner(string: tabTitle)
nameScanner.scanString("BBC iPlayer - ")
show.showName = nameScanner.scanUpToString( " - ") ?? ""
show.tvNetwork = "BBC"
completion([show])
return
} else if url.hasPrefix("https://www.bbc.co.uk/radio/play/") || url.hasPrefix("https://www.bbc.co.uk/sounds/play/") {
// PID is always the last element in the URL.
if let nsUrl = URL(string: url) {
let show = Programme()
show.pid = nsUrl.lastPathComponent
show.tvNetwork = "BBC"
show.radio = true
// Program title is buried in the page HTML.
completion([show])
}
return
} else if url.hasPrefix("https://www.bbc.co.uk/programmes/") {
// Search the page to see if it is an episode or a series page. If we don't find the PID inside
// a bbcProgrammes element, it's a series page and we can't use it (though we might want to try
// adding it with pid-recursive)
guard let htmlPage = try? HTML(html: pageSource, encoding: .utf8) else {
return
}
var showList = [Programme]()
var infoDicts: [JSON] = []
for showInfo in htmlPage.xpath("//script[@type='application/ld+json']") {
guard let content = showInfo.content, !content.isEmpty else {
continue
}
let infoJSON = JSON(parseJSON: content)
if infoJSON["@type"].exists() {
infoDicts.append(infoJSON)
} else {
let graphBlocks = infoJSON["@graph"].arrayValue
for block in graphBlocks {
if block["@type"].exists() {
infoDicts.append(block)
}
}
}
}
// Search all of the show infos for something we know about.
for infoDict in infoDicts {
let contentType = infoDict["@type"]
if contentType == "BreadcrumbList" {
continue
}
switch contentType {
case "TVEpisode", "@TVEpisode", "@RadioEpisode", "RadioEpisode", "Clip":
let show = Programme()
show.pid = infoDict["identifier"].stringValue
show.tvNetwork = "BBC"
show.seriesName = infoDict["partOfSeries"]["name"].stringValue
show.episodeName = infoDict["name"].stringValue
show.url = infoDict["url"].stringValue
show.desc = infoDict["description"].stringValue
show.showName = !show.seriesName.isEmpty ? show.seriesName : show.episodeName
if ["@RadioEpisode", "RadioEpisode"].contains(contentType.stringValue) {
show.radio = true
}
showList.append(show)
break
default:
continue
}
}
if showList.isEmpty {
showList = searchForPIDs(url: url)
}
completion(showList)
// } else if url.hasPrefix("https://www.itv.com/hub/") {
// let show = ITVMetadataExtractor.getShowMetadata(htmlPageContent: pageSource)
// completion([show])
} else if url.hasPrefix("https://player.stv.tv/episode/") {
let show = STVMetadataExtractor.getShowMetadata(html: pageSource)
if show.count == 0 {
let invalidPage = NSAlert()
invalidPage.addButton(withTitle: "OK")
invalidPage.messageText = "Protected content"
invalidPage.informativeText = "The selected program is DRM protected, so it cannot be retrieved with Get iPlayer Automator."
invalidPage.alertStyle = .warning
invalidPage.runModal()
} else {
completion(show)
}
} else {
let invalidPage = NSAlert()
invalidPage.addButton(withTitle: "OK")
invalidPage.messageText = "Programme Page Not Found"
invalidPage.informativeText = "Please ensure the frontmost browser tab is open to an iPlayer episode page or ITV Hub episode page."
invalidPage.alertStyle = .warning
invalidPage.runModal()
}
}
@objc open class func getCurrentWebpage(completion: ([Programme]) -> Void) {
//Get Default Browser
guard let browser = UserDefaults.standard.string(forKey: "DefaultBrowser") else {
return
}
//Prepare Alert in Case the Browser isn't Open
let browserNotOpen = NSAlert()
browserNotOpen.addButton(withTitle: "OK")
browserNotOpen.messageText = "\(browser) is not open."
browserNotOpen.informativeText = "Please ensure your browser is running and has at least one window open."
browserNotOpen.alertStyle = .warning
//Get URL
switch (browser) {
case "Safari":
var safariRunning: SafariApplication? = nil
let safariTechPreview = SBApplication(bundleIdentifier: "com.apple.SafariTechnologyPreview")
if safariTechPreview?.isRunning ?? false {
safariRunning = safariTechPreview
} else {
let safariDefault = SBApplication(bundleIdentifier: "com.apple.Safari")
if safariDefault?.isRunning ?? false {
safariRunning = safariDefault
}
}
guard let safari = safariRunning, let safariWindows = safari.windows?().compactMap({ $0 as? SafariWindow }) else {
browserNotOpen.runModal()
return
}
let orderedWindows = safariWindows.sorted { $0.index! < $1.index! }
if let frontWindow = orderedWindows.first,
let tab = frontWindow.currentTab,
let url = tab.URL,
let name = tab.name,
let source = tab.source {
extractMetadata(url: url, tabTitle: name, pageSource: source, completion: completion)
}
break
case "Chrome", "Microsoft Edge", "Vivaldi", "Brave":
// All WebKit browsers have the same AppleScript support.
// We just need to find the right bundle ID.
let mapping = [
"Chrome" : "com.google.Chrome",
"Microsoft Edge" : "com.microsoft.edgemac",
"Vivaldi" : "com.vivaldi.Vivaldi",
"Brave" : "com.brave.Browser"]
guard let bundleID = mapping[browser], let chrome : ChromeApplication = SBApplication(bundleIdentifier: bundleID), chrome.isRunning, let chromeWindows = chrome.windows?().compactMap({ $0 as? ChromeWindow }) else {
browserNotOpen.runModal()
return
}
let orderedWindows = chromeWindows.sorted { $0.index! < $1.index! }
if let frontWindow = orderedWindows.first,
let tab = frontWindow.activeTab,
let url = tab.URL,
let title = tab.title,
let source = tab.executeJavascript?("document.documentElement.outerHTML") as? String {
extractMetadata(url: url, tabTitle: title, pageSource: source, completion: completion)
}
break
default:
let unsupportedBrowser = NSAlert()
unsupportedBrowser.messageText = "Uh, something went horribly wrong."
unsupportedBrowser.addButton(withTitle: "OK")
unsupportedBrowser.informativeText = "Get iPlayer Automator only works with Safari and Chrome. We shouldn't be here; please file a bug."
unsupportedBrowser.runModal()
}
}
private class func searchForPIDs(url: String) -> [Programme] {
let task = Process()
let pipe = Pipe()
let errorPipe = Pipe();
task.launchPath = AppController.shared().perlBinaryPath
let args = [
AppController.shared().getiPlayerPath,
GetiPlayerArguments.sharedController().noWarningArg,
GetiPlayerArguments.sharedController().cacheExpiryArg,
"--pid-recursive-list",
url,
GetiPlayerArguments.sharedController().profileDirArg
]
for arg in args {
DDLogVerbose("\(arg)");
}
task.arguments = args
task.standardOutput = pipe
task.standardError = errorPipe
var envVariableDictionary = [String : String]()
envVariableDictionary["HOME"] = NSString("~").expandingTildeInPath
envVariableDictionary["PERL_UNICODE"] = "AS"
envVariableDictionary["PATH"] = AppController.shared().perlEnvironmentPath
task.environment = envVariableDictionary
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
var foundPrograms = [Programme]()
if let stringData = String(data: data, encoding: .utf8) {
let lines = stringData.components(separatedBy: .newlines)
for line in lines {
if line.isEmpty || line.hasPrefix("Episodes:") || line.hasPrefix("INFO:") {
continue
}
let program = Programme()
let outputParts = line.components(separatedBy:"|")
program.episodeName = outputParts[0].trimmingCharacters(in: .whitespaces)
program.tvNetwork = outputParts[1].trimmingCharacters(in: .whitespaces)
program.pid = outputParts[2].trimmingCharacters(in: .whitespaces)
foundPrograms.append(program)
}
}
return foundPrograms
}
}