This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
GetCurrentWebpage.m
319 lines (310 loc) · 14.2 KB
/
GetCurrentWebpage.m
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
//
// GetCurrentWebpageController.m
// Get_iPlayer GUI
//
// Created by Thomas Willson on 8/3/14.
//
//
#import "GetCurrentWebpage.h"
@implementation GetCurrentWebpage
+ (Programme *)getCurrentWebpage:(LogController *)logger
{
NSString *newShowName=nil;
//Get Default Browser
NSString *browser = [[NSUserDefaults standardUserDefaults] objectForKey:@"DefaultBrowser"];
//Prepare Pointer for URL
NSString *url = nil;
NSString *source = nil;
//Prepare Alert in Case the Browser isn't Open
NSAlert *browserNotOpen = [[NSAlert alloc] init];
[browserNotOpen addButtonWithTitle:@"OK"];
[browserNotOpen setMessageText:[NSString stringWithFormat:@"%@ is not open.", browser]];
[browserNotOpen setInformativeText:@"Please ensure your browser is running and has at least one window open."];
[browserNotOpen setAlertStyle:NSWarningAlertStyle];
//Get URL
if ([browser isEqualToString:@"Safari"])
{
BOOL foundURL=NO;
SafariApplication *Safari = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];
if ([Safari isRunning])
{
@try
{
SBElementArray *windows = [Safari windows];
if ([@([windows count]) intValue])
{
for (SafariWindow *window in windows)
{
SafariTab *tab = [window currentTab];
if ([[tab URL] hasPrefix:@"http://www.bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"http://bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"http://bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"http://www.bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"http://bbc.co.uk/sport"] ||
[[tab URL] hasPrefix:@"https://www.bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"https://bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"https://bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"https://www.bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"https://bbc.co.uk/sport"])
{
url = [NSString stringWithString:[tab URL]];
NSScanner *nameScanner = [NSScanner scannerWithString:[tab name]];
[nameScanner scanString:@"BBC iPlayer - " intoString:nil];
[nameScanner scanString:@"BBC Sport - " intoString:nil];
[nameScanner scanUpToString:@"kjklgfdjfgkdlj" intoString:&newShowName];
foundURL=YES;
}
else if ([[tab URL] hasPrefix:@"http://www.bbc.co.uk/programmes/"] ||
[[tab URL] hasPrefix:@"https://www.bbc.co.uk/programmes/"]) {
url = [NSString stringWithString:[tab URL]];
NSScanner *nameScanner = [NSScanner scannerWithString:[tab name]];
[nameScanner scanUpToString:@"- " intoString:nil];
[nameScanner scanString:@"- " intoString:nil];
[nameScanner scanUpToString:@"kjklgfdjfgkdlj" intoString:&newShowName];
foundURL=YES;
source = [tab source];
}
else if ([[tab URL] hasPrefix:@"http://www.itv.com/hub/"] ||
[[tab URL] hasPrefix:@"https://www.itv.com/hub/"])
{
url = [NSString stringWithString:[tab URL]];
source = [tab source];
newShowName = [[tab name] stringByReplacingOccurrencesOfString:@" - The ITV Hub" withString:@""];
foundURL=YES;
}
}
if (foundURL==NO)
{
url = [NSString stringWithString:[[windows[0] currentTab] URL]];
//Might be incorrect
}
}
else
{
[browserNotOpen runModal];
return nil;
}
}
@catch (NSException *e)
{
[browserNotOpen runModal];
return nil;
}
}
else
{
[browserNotOpen runModal];
return nil;
}
}
else if ([browser isEqualToString:@"Chrome"])
{
BOOL foundURL=NO;
ChromeApplication *Chrome = [SBApplication applicationWithBundleIdentifier:@"com.google.Chrome"];
if ([Chrome isRunning])
{
@try
{
SBElementArray *windows = [Chrome windows];
if ([@([windows count]) intValue])
{
for (ChromeWindow *window in windows)
{
ChromeTab *tab = [window activeTab];
if ([[tab URL] hasPrefix:@"http://www.bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"http://bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"http://bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"http://www.bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"http://bbc.co.uk/sport"] ||
[[tab URL] hasPrefix:@"https://www.bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"https://bbc.co.uk/iplayer/episode/"] ||
[[tab URL] hasPrefix:@"https://bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"https://www.bbc.co.uk/iplayer/console/"] ||
[[tab URL] hasPrefix:@"https://bbc.co.uk/sport"])
{
url = [NSString stringWithString:[tab URL]];
NSScanner *nameScanner = [NSScanner scannerWithString:[tab title]];
[nameScanner scanString:@"BBC iPlayer - " intoString:nil];
[nameScanner scanString:@"BBC Sport - " intoString:nil];
[nameScanner scanUpToString:@"kjklgfdjfgkdlj" intoString:&newShowName];
foundURL=YES;
}
else if ([[tab URL] hasPrefix:@"http://www.bbc.co.uk/programmes/"] ||
[[tab URL] hasPrefix:@"https://www.bbc.co.uk/programmes/"]) {
url = [NSString stringWithString:[tab URL]];
NSScanner *nameScanner = [NSScanner scannerWithString:[tab title]];
[nameScanner scanUpToString:@"- " intoString:nil];
[nameScanner scanString:@"- " intoString:nil];
[nameScanner scanUpToString:@"kjklgfdjfgkdlj" intoString:&newShowName];
foundURL=YES;
source = [tab executeJavascript:@"document.documentElement.outerHTML"];
}
else if ([[tab URL] hasPrefix:@"http://www.itv.com/hub/"] ||
[[tab URL] hasPrefix:@"https://www.itv.com/hub/"])
{
url = [NSString stringWithString:[tab URL]];
source = [tab executeJavascript:@"document.documentElement.outerHTML"];
newShowName = [[tab title] stringByReplacingOccurrencesOfString:@" - The ITV Hub" withString:@""];
foundURL=YES;
}
}
if (foundURL==NO)
{
url = [NSString stringWithString:[[windows[0] activeTab] URL]];
//Might be incorrect
}
}
else
{
[browserNotOpen runModal];
return nil;
}
}
@catch (NSException *e)
{
[browserNotOpen runModal];
return nil;
}
}
else
{
[browserNotOpen runModal];
return nil;
}
}
else
{
[[NSAlert alertWithMessageText:@"Get iPlayer Automator currently only supports Safari and Chrome." defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Please change your preferred browser in the preferences and try again."] runModal];
return nil;
}
//Process URL
if([url hasPrefix:@"http://www.bbc.co.uk/iplayer/episode/"] || [url hasPrefix:@"http://beta.bbc.co.uk/iplayer/episode"] ||
[url hasPrefix:@"https://www.bbc.co.uk/iplayer/episode/"] || [url hasPrefix:@"https://beta.bbc.co.uk/iplayer/episode"])
{
NSString *pid = nil;
NSScanner *urlScanner = [[NSScanner alloc] initWithString:url];
[urlScanner scanUpToString:@"/episode/" intoString:nil];
if ([urlScanner isAtEnd]) {
[urlScanner setScanLocation:0];
[urlScanner scanUpToString:@"/console/" intoString:nil];
}
[urlScanner scanString:@"/" intoString:nil];
[urlScanner scanUpToString:@"/" intoString:nil];
[urlScanner scanString:@"/" intoString:nil];
[urlScanner scanUpToString:@"/" intoString:&pid];
Programme *newProg = [[Programme alloc] initWithLogController:logger];
[newProg setValue:pid forKey:@"pid"];
if (newShowName) [newProg setShowName:newShowName];
// newProg.status = @"Processing...";
// [newProg performSelectorInBackground:@selector(getName) withObject:nil];
return newProg;
}
else if([url hasPrefix:@"http://www.bbc.co.uk/programmes/"])
{
NSString *pid = nil;
NSScanner *urlScanner = [[NSScanner alloc] initWithString:url];
[urlScanner scanUpToString:@"/programmes/" intoString:nil];
[urlScanner scanString:@"/" intoString:nil];
[urlScanner scanUpToString:@"/" intoString:nil];
[urlScanner scanString:@"/" intoString:nil];
[urlScanner scanUpToString:@"#" intoString:&pid];
NSScanner *scanner = [NSScanner scannerWithString:source];
[scanner scanUpToString:[NSString stringWithFormat:@"bbcProgrammes.programme = { pid : '%@', type : 'episode' }", pid] intoString:nil];
if ([scanner isAtEnd]) {
[scanner setScanLocation:0];
[scanner scanUpToString:[NSString stringWithFormat:@"bbcProgrammes.programme = { pid : '%@', type : 'clip' }", pid] intoString:nil];
}
if ([scanner isAtEnd]) {
NSAlert *invalidPage = [[NSAlert alloc] init];
[invalidPage addButtonWithTitle:@"OK"];
[invalidPage setMessageText:[NSString stringWithFormat:@"Invalid Page: %@",url]];
[invalidPage setInformativeText:@"Please ensure the frontmost browser tab is open to an iPlayer episode page or programme clip page."];
[invalidPage setAlertStyle:NSWarningAlertStyle];
[invalidPage runModal];
return nil;
}
Programme *newProg = [[Programme alloc] init];
[newProg setValue:pid forKey:@"pid"];
if (newShowName) [newProg setShowName:newShowName];
// newProg.status = @"Processing...";
// [newProg performSelectorInBackground:@selector(getName) withObject:nil];
return newProg;
}
else if ([url hasPrefix:@"http://www.bbc.co.uk/sport/olympics/2012/live-video/"] ||
[url hasPrefix:@"https://www.bbc.co.uk/sport/olympics/2012/live-video/"])
{
NSString *pid = nil;
NSScanner *urlScanner = [NSScanner scannerWithString:url];
[urlScanner scanString:@"http://www.bbc.co.uk/sport/olympics/2012/live-video/" intoString:nil];
[urlScanner scanUpToString:@"kfejklfjklj" intoString:&pid];
return [[Programme alloc] initWithInfo:nil pid:pid programmeName:newShowName network:@"BBC Sport" logController:logger];
}
else if ([url hasPrefix:@"http://www.itv.com/hub/"] || [url hasPrefix:@"https://www.itv.com/hub/"])
{
NSString *progname = nil, *productionId = nil, *title = nil, *desc = nil;
NSInteger seriesnum = 0, episodenum = 0;
progname = newShowName;
NSScanner *scanner = [NSScanner scannerWithString:source];
[scanner scanUpToString:@"<meta property=\"og:title\" content=\"" intoString:nil];
[scanner scanString:@"<meta property=\"og:title\" content=\"" intoString:nil];
[scanner scanUpToString:@"\"" intoString:&title];
if (title) progname = [title stringByDecodingHTMLEntities];
[scanner scanUpToString:@"<meta property=\"og:description\" content=\"" intoString:nil];
[scanner scanString:@"<meta property=\"og:description\" content=\"" intoString:nil];
[scanner scanUpToString:@"\"" intoString:&desc];
[scanner scanUpToString:@"&productionId=" intoString:nil];
[scanner scanString:@"&productionId=" intoString:nil];
[scanner scanUpToString:@"\"" intoString:&productionId];
if (!progname || !productionId) {
NSAlert *invalidPage = [[NSAlert alloc] init];
[invalidPage addButtonWithTitle:@"OK"];
[invalidPage setMessageText:[NSString stringWithFormat:@"Invalid Page: %@",url]];
[invalidPage setInformativeText:@"Please ensure the frontmost browser tab is open to an ITV Hub episode page."];
[invalidPage setAlertStyle:NSWarningAlertStyle];
[invalidPage runModal];
return nil;
}
NSString *pid = [productionId stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *showName = [NSString stringWithFormat:@"%@ - %@", progname, pid];
Programme *newProg = [[Programme alloc] init];
[newProg setPid:pid];
[newProg setShowName:showName];
[newProg setTvNetwork:@"ITV"];
[newProg setProcessedPID:@YES];
[newProg setUrl:url];
scanner = [NSScanner scannerWithString:title];
[scanner scanUpToString:@"Series " intoString:nil];
[scanner scanString:@"Series " intoString:nil];
[scanner scanInteger:&seriesnum];
[scanner setScanLocation:0];
[scanner scanUpToString:@"Episode " intoString:nil];
[scanner scanString:@"Episode " intoString:nil];
[scanner scanInteger:&episodenum];
scanner = [NSScanner scannerWithString:desc];
if ( seriesnum == 0 ) {
[scanner scanUpToString:@"Series " intoString:nil];
[scanner scanString:@"Series " intoString:nil];
[scanner scanInteger:&seriesnum];
}
if ( episodenum == 0 ) {
[scanner setScanLocation:0];
[scanner scanUpToString:@"Episode " intoString:nil];
[scanner scanString:@"Episode " intoString:nil];
[scanner scanInteger:&episodenum];
}
[newProg setSeason:seriesnum];
[newProg setEpisode:episodenum];
return newProg;
}
else
{
NSAlert *invalidPage = [[NSAlert alloc] init];
[invalidPage addButtonWithTitle:@"OK"];
[invalidPage setMessageText:[NSString stringWithFormat:@"Invalid Page: %@",url]];
[invalidPage setInformativeText:@"Please ensure the frontmost browser tab is open to an iPlayer episode page or ITV Hub episode page."];
[invalidPage setAlertStyle:NSWarningAlertStyle];
[invalidPage runModal];
return nil;
}
}
@end