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
/
ITVDownload.m
594 lines (545 loc) · 25.4 KB
/
ITVDownload.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//
// ITVDownload.m
// Get_iPlayer GUI
//
// Created by Thomas Willson on 12/16/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "ITVDownload.h"
#import "ASIHTTPRequest.h"
#import "NSString+HTML.h"
#import "ITVMediaFileEntry.h"
@implementation ITVDownload
- (id)init
{
if (!(self = [super init])) return nil;
return self;
}
- (id)description
{
return [NSString stringWithFormat:@"ITV Download (ID=%@)", [show pid]];
}
- (id)initTest:(Programme *)tempShow proxy:(HTTPProxy *)aProxy
{
if (!(self = [super init])) return nil;
proxy=aProxy;
show=tempShow;
attemptNumber=1;
nc = [NSNotificationCenter defaultCenter];
defaultsPrefix = @"ITV_";
running=TRUE;
formatList = @[[[TVFormat alloc] init],[[TVFormat alloc] init]];
[formatList[0] setFormat:@"Flash - Standard"];
[formatList[1] setFormat:@"Flash - High"];
isTest=true;
[tempShow printLongDescription];
[self launchMetaRequest];
return self;
}
- (id)initWithProgramme:(Programme *)tempShow itvFormats:(NSArray *)itvFormatList proxy:(HTTPProxy *)aProxy logController:(LogController *)logger
{
if (!(self = [super initWithLogController:logger])) return nil;
proxy = aProxy;
show = tempShow;
attemptNumber=1;
nc = [NSNotificationCenter defaultCenter];
defaultsPrefix = @"ITV_";
running=TRUE;
if (![itvFormatList count]) {
NSLog(@"ERROR: ITV Format List is empty");
[self addToLog:@"ERROR: ITV Format List is empty"];
[show setReasonForFailure:@"ITVFormatListEmpty"];
[show setComplete:@YES];
[show setSuccessful:@NO];
[show setValue:@"Download Failed" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
return self;
}
[self setCurrentProgress:[NSString stringWithFormat:@"Retrieving Programme Metadata... -- %@",[show showName]]];
[self setPercentage:102];
[tempShow setValue:@"Initialising..." forKey:@"status"];
formatList = [itvFormatList copy];
[self addToLog:[NSString stringWithFormat:@"Downloading %@",[show showName]]];
[self addToLog:@"INFO: Preparing Request for Auth Info" noTag:YES];
[tempShow printLongDescription];
[self launchMetaRequest];
return self;
}
- (void)launchMetaRequest
{
errorCache = [[NSMutableString alloc] initWithString:@""];
processErrorCache = [NSTimer scheduledTimerWithTimeInterval:.25 target:self selector:@selector(processError) userInfo:nil repeats:YES];
NSString *soapBody = nil;
if ([show url] && [[show url] rangeOfString:@"Filter=" options:NSCaseInsensitiveSearch].location == NSNotFound) {
[show setRealPID:[show pid]];
soapBody = @"Body2";
downloadParams[@"UseCurrentWebPage"] = @YES;
}
else
{
NSString *pid = nil;
NSScanner *scanner = [NSScanner scannerWithString:[show url]];
[scanner scanUpToString:@"Filter=" intoString:nil];
[scanner scanString:@"Filter=" intoString:nil];
[scanner scanUpToString:@"kljkjj" intoString:&pid];
if (!pid)
{
NSLog(@"ERROR: GiA cannot interpret the ITV URL: %@", [show url]);
[self addToLog:[NSString stringWithFormat:@"ERROR: GiA cannot interpret the ITV URL: %@", [show url]]];
[show setReasonForFailure:@"MetadataProcessing"];
[show setComplete:@YES];
[show setSuccessful:@NO];
[show setValue:@"Download Failed" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
return;
}
[show setRealPID:pid];
soapBody = @"Body";
}
NSString *body;
if (!isTest)
body = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:soapBody ofType:nil]]
encoding:NSUTF8StringEncoding];
else
//body = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSProcessInfo processInfo] environment] objectForKey:@"REQUEST_LOC"]] encoding:NSUTF8StringEncoding];
body = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:@"/Applications/Get iPlayer Automator.app/Contents/Resources/Body"] encoding:NSUTF8StringEncoding];
body = [body stringByReplacingOccurrencesOfString:@"!!!ID!!!" withString:[show realPID]];
NSURL *requestURL = [NSURL URLWithString:@"http://mercury.itv.com/PlaylistService.svc"];
NSLog(@"DEBUG: Metadata URL: %@",requestURL);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Metadata URL: %@", requestURL] noTag:YES];
[currentRequest clearDelegatesAndCancel];
currentRequest = [ASIHTTPRequest requestWithURL:requestURL];
[currentRequest addRequestHeader:@"Referer" value:@"http://www.itv.com/mercury/Mercury_VideoPlayer.swf?v=1.5.309/[[DYNAMIC]]/2"];
[currentRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
[currentRequest addRequestHeader:@"SOAPAction" value:@"\"http://tempuri.org/PlaylistService/GetPlaylist\""];
[currentRequest setRequestMethod:@"POST"];
[currentRequest setPostBody:[NSMutableData dataWithData:[body dataUsingEncoding:NSUTF8StringEncoding]]];
[currentRequest setDelegate:self];
[currentRequest setDidFailSelector:@selector(metaRequestFinished:)];
[currentRequest setDidFinishSelector:@selector(metaRequestFinished:)];
[currentRequest setTimeOutSeconds:10];
[currentRequest setNumberOfTimesToRetryOnTimeout:3];
[currentRequest addRequestHeader:@"Accept" value:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"];
if (proxy)
{
[currentRequest setProxyType:proxy.type];
[currentRequest setProxyHost:proxy.host];
if (proxy.port)
[currentRequest setProxyPort:(int)proxy.port];
if (proxy.user) {
[currentRequest setProxyUsername:proxy.user];
[currentRequest setProxyPassword:proxy.password];
}
}
NSLog(@"INFO: Requesting Metadata.");
[self addToLog:@"INFO: Requesting Metadata." noTag:YES];
[currentRequest startAsynchronous];
}
-(void)metaRequestFinished:(ASIHTTPRequest *)request
{
if (!running)
return;
NSLog(@"DEBUG: Metadata response status code: %d", [request responseStatusCode]);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Metadata response status code: %d", [request responseStatusCode]] noTag:YES];
NSString *responseString = [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding];
NSLog(@"DEBUG: Metadata response: %@",responseString);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Metadata response: %@", responseString] noTag:YES];
NSError *error = [request error];
if ([request responseStatusCode] == 0)
{
NSLog(@"ERROR: No response received (probably a proxy issue): %@", (error ? [error localizedDescription] : @"Unknown error"));
[self addToLog:[NSString stringWithFormat:@"ERROR: No response received (probably a proxy issue): %@", (error ? [error localizedDescription] : @"Unknown error")]];
[show setSuccessful:@NO];
[show setComplete:@YES];
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"Proxy"] isEqualTo:@"Provided"])
[show setReasonForFailure:@"Provided_Proxy"];
else if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"Proxy"] isEqualTo:@"Custom"])
[show setReasonForFailure:@"Custom_Proxy"];
else
[show setReasonForFailure:@"Internet_Connection"];
[show setValue:@"Failed: Bad Proxy" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
[self addToLog:@"Download Failed" noTag:NO];
return;
}
else if ([responseString length] > 0 && [responseString rangeOfString:@"InvalidGeoRegion" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog(@"ERROR: Access denied to users outside UK.");
[self addToLog:@"ERROR: Access denied to users outside UK."];
[show setSuccessful:@NO];
[show setComplete:@YES];
[show setReasonForFailure:@"Outside_UK"];
[show setValue:@"Failed: Outside UK" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
[self addToLog:@"Download Failed" noTag:NO];
return;
}
else if ([request responseStatusCode] != 200 || [responseString length] == 0)
{
NSLog(@"ERROR: Could not retrieve programme metadata: %@", (error ? [error localizedDescription] : @"Unknown error"));
[self addToLog:[NSString stringWithFormat:@"ERROR: Could not retrieve programme metadata: %@", (error ? [error localizedDescription] : @"Unknown error")]];
[show setSuccessful:@NO];
[show setComplete:@YES];
[show setValue:@"Download Failed" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
[self addToLog:@"Download Failed" noTag:NO];
return;
}
responseString = [responseString stringByDecodingHTMLEntities];
NSScanner *scanner = [NSScanner scannerWithString:responseString];
if (downloadParams[@"UseCurrentWebPage"])
{
//Reset to numeric PID if originated from current web page
NSString *pid = nil;
[scanner scanUpToString:@"<Vodcrid>crid://itv.com/" intoString:nil];
[scanner scanString:@"<Vodcrid>crid://itv.com/" intoString:nil];
[scanner scanUpToString:@"</Vodcrid>" intoString:&pid];
[show setRealPID:pid];
}
//Retrieve Series Name
NSString *seriesName = nil;
[scanner scanUpToString:@"<ProgrammeTitle>" intoString:nil];
[scanner scanString:@"<ProgrammeTitle>" intoString:nil];
[scanner scanUpToString:@"</ProgrammeTitle>" intoString:&seriesName];
[show setSeriesName:seriesName];
//Init date formatter
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
//Retrieve Transmission Date
NSString *dateString = nil;
[scanner scanUpToString:@"<TransmissionDate>" intoString:nil];
[scanner scanString:@"<TransmissionDate>" intoString:nil];
[scanner scanUpToString:@"</TransmissionDate>" intoString:&dateString];
[dateFormat setDateFormat:@"dd LLLL yyyy"];
[show setDateAired:[dateFormat dateFromString:dateString]];
//Retrieve Episode Name
NSString *episodeName = nil;
[scanner scanUpToString:@"<EpisodeTitle" intoString:nil];
if (![scanner scanString:@"<EpisodeTitle/>" intoString:nil])
{
[scanner scanString:@"<EpisodeTitle>" intoString:nil];
[scanner scanUpToString:@"</EpisodeTitle>" intoString:&episodeName];
if (!episodeName) episodeName=@"(No Episode Name)";
[show setEpisodeName:episodeName];
}
else
[show setEpisodeName:@"(No Episode Name)"];
//Retrieve Episode Number
NSInteger episodeNumber = [show episode];
[scanner scanUpToString:@"<EpisodeNumber" intoString:nil];
if (![scanner scanString:@"<EpisodeNumber/>" intoString:nil])
{
[scanner scanString:@"<EpisodeNumber>" intoString:nil];
[scanner scanInteger:&episodeNumber];
}
[show setEpisode:episodeNumber];
//Retrieve Thumbnail URL
[scanner scanUpToString:@"<PosterFrame>" intoString:nil];
[scanner scanUpToString:@"CDATA" intoString:nil];
[scanner scanString:@"CDATA[" intoString:nil];
NSString *url;
[scanner scanUpToString:@"]]" intoString:&url];
thumbnailURL=url;
url=nil;
//Increase thumbnail size to 640x360
NSInteger thumbWidth = 0;
NSScanner *thumbScanner = [NSScanner scannerWithString:thumbnailURL];
[thumbScanner scanUpToString:@"?w=" intoString:nil];
[thumbScanner scanString:@"?w=" intoString:nil];
[thumbScanner scanInteger:&thumbWidth];
if (thumbWidth != 0 && thumbWidth < 640)
{
NSRange thumbSizeRange = [thumbnailURL rangeOfString:@"?w=" options:NSCaseInsensitiveSearch];
if (thumbSizeRange.location != NSNotFound)
{
thumbSizeRange.length = [thumbnailURL length] - thumbSizeRange.location;
thumbnailURL = [thumbnailURL stringByReplacingCharactersInRange:thumbSizeRange withString:@"?w=640&h=360"];
NSLog(@"DEBUG: Thumbnail URL changed: %@", thumbnailURL);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Thumbnail URL changed: %@", thumbnailURL] noTag:YES];
}
}
//Retrieve Subtitle URL
[scanner scanUpToString:@"<ClosedCaptioning" intoString:nil];
if(![scanner scanString:@"<ClosedCaptioningURIs/>" intoString:nil])
{
[scanner scanUpToString:@"CDATA[" intoString:nil];
[scanner scanString:@"CDATA[" intoString:nil];
NSString *url;
[scanner scanUpToString:@"]]" intoString:&url];
subtitleURL=url;
url=nil;
}
//Retrieve Auth URL
NSString *authURL = nil;
[scanner scanUpToString:@"rtmpe://" intoString:nil];
[scanner scanUpToString:@"\"" intoString:&authURL];
NSLog(@"DEBUG: Metadata processed: seriesName=%@ dateString=%@ episodeName=%@ episodeNumber=%ld thumbnailURL=%@ subtitleURL=%@ authURL=%@",
seriesName, dateString, episodeName, episodeNumber, thumbnailURL, subtitleURL, authURL);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Metadata processed: seriesName=%@ dateString=%@ episodeName=%@ episodeNumber=%ld thumbnailURL=%@ subtitleURL=%@ authURL=%@",
seriesName, dateString, episodeName, episodeNumber, thumbnailURL, subtitleURL, authURL] noTag:YES];
NSLog(@"DEBUG: Retrieving Playpath");
if (verbose)
[self addToLog:@"DEBUG: Retrieving Playpath" noTag:YES];
//Retrieve PlayPath
NSString *playPath = nil;
NSMutableArray *itvRateArray = nil;
NSMutableArray *bitrateArray = nil;
@try {
NSArray *formatKeys = @[@"Flash - Very Low",@"Flash - Low",@"Flash - Standard",@"Flash - High", @"Flash - Very High", @"Flash - HD"];
NSArray *itvRateObjects = @[@"400",@"600",@"800",@"1200", @"1500", @"1800"];
NSArray *bitrateObjects = @[@"400000",@"600000",@"800000",@"1200000",@"1500000",@"1800000"];
NSDictionary *itvRateDic = [NSDictionary dictionaryWithObjects:itvRateObjects forKeys:formatKeys];
NSDictionary *bitrateDic = [NSDictionary dictionaryWithObjects:bitrateObjects forKeys:formatKeys];
itvRateArray = [[NSMutableArray alloc] init];
bitrateArray = [[NSMutableArray alloc] init];
for (TVFormat *format in formatList) {
NSString *mode;
if ((mode = itvRateDic[format.format]))
[itvRateArray addObject:mode];
}
for (TVFormat *format in formatList) {
NSString *mode;
if ((mode = bitrateDic[format.format]))
[bitrateArray addObject:mode];
}
}
@catch (NSException *exception)
{
NSLog(@"ERROR: %@: %@", [exception name], [exception description]);
[self addToLog:[NSString stringWithFormat:@"ERROR: %@: %@", [exception name], [exception description]]];
[show setComplete:@YES];
[show setSuccessful:@NO];
[show setValue:@"Download Failed" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
return;
}
NSLog(@"DEBUG: Parsing MediaFile entries");
if (verbose)
[self addToLog:@"DEBUG: Parsing MediaFile entries" noTag:YES];
NSMutableArray *mediaEntries = [[NSMutableArray alloc] init];
NSUInteger beforeMediaFiles = [scanner scanLocation];
while ([scanner scanUpToString:@"MediaFile delivery" intoString:nil]) {
NSString *url = nil, *bitrate = nil, *itvRate = nil;
ITVMediaFileEntry *entry = [[ITVMediaFileEntry alloc] init];
[scanner scanUpToString:@"bitrate=" intoString:nil];
[scanner scanString:@"bitrate=\"" intoString:nil];
[scanner scanUpToString:@"\"" intoString:&bitrate];
[scanner scanUpToString:@"CDATA" intoString:nil];
[scanner scanString:@"CDATA[" intoString:nil];
NSUInteger location = [scanner scanLocation];
[scanner scanUpToString:@"]]" intoString:&url];
[scanner setScanLocation:location];
[scanner scanUpToString:@"_PC01" intoString:nil];
[scanner scanString:@"_PC01" intoString:nil];
[scanner scanUpToString:@"_" intoString:&itvRate];
if (scanner.atEnd) {
[scanner setScanLocation:location];
[scanner scanUpToString:@"_itv" intoString:nil];
[scanner scanString:@"_itv" intoString:nil];
[scanner scanUpToString:@"_" intoString:&itvRate];
}
[entry setBitrate:bitrate];
[entry setUrl:url];
[entry setItvRate:itvRate];
[mediaEntries addObject:entry];
NSLog(@"DEBUG: ITVMediaFileEntry: bitrate=%@ itvRate=%@ url=%@", bitrate, itvRate, url);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: ITVMediaFileEntry: bitrate=%@ itvRate=%@ url=%@", bitrate, itvRate, url] noTag:YES];
}
NSLog(@"DEBUG: Searching for itvRate match");
if (verbose)
[self addToLog:@"DEBUG: Searching for itvRate match" noTag:YES];
BOOL foundIt=FALSE;
for (NSString *rate in itvRateArray) {
for (ITVMediaFileEntry *entry in mediaEntries) {
if ([[entry itvRate] isEqualToString:rate]) {
foundIt=TRUE;
playPath=[entry url];
NSLog(@"DEBUG: foundIt (itvRate): rate=%@ url=%@", rate, playPath);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: foundIt (itvRate): rate=%@ url=%@", rate, playPath] noTag:YES];
break;
}
}
if (foundIt) break;
}
if (!foundIt)
{
NSLog(@"DEBUG: Searching for bitrate match");
if (verbose)
[self addToLog:@"DEBUG: Searching for bitrate match" noTag:YES];
for (NSString *rate in bitrateArray) {
for (ITVMediaFileEntry *entry in mediaEntries) {
if ([[entry bitrate] isEqualToString:rate]) {
foundIt=TRUE;
playPath=[entry url];
NSLog(@"DEBUG: foundIt (bitrate): rate=%@ url=%@", rate, playPath);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: foundIt (bitrate): rate=%@ url=%@", rate, playPath] noTag:YES];
break;
}
}
if (foundIt) break;
}
}
if (!foundIt) {
NSLog(@"ERROR: None of the modes in your download format list are available for this show. Try adding more modes if possible.");
[self addToLog:@"ERROR: None of the modes in your download format list are available for this show. Try adding more modes if possible."];
show.reasonForFailure = @"NoSpecifiedFormatAvailableITV";
[show setComplete:@YES];
[show setSuccessful:@NO];
[show setValue:@"Download Failed" forKey:@"status"];
[nc postNotificationName:@"DownloadFinished" object:show];
return;
}
else {
NSLog(@"DEBUG: playPath = %@",playPath);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: playPath = %@", playPath] noTag:YES];
}
NSInteger seriesNumber = [show season];
for (ITVMediaFileEntry *entry in mediaEntries) {
NSScanner *mescanner = [NSScanner scannerWithString:[entry url]];
[mescanner scanUpToString:@"(series-" intoString:nil];
[mescanner scanString:@"(series-" intoString:nil];
if ([mescanner scanInteger:&seriesNumber])
break;
}
[show setSeason:seriesNumber];
NSLog(@"DEBUG: seriesNumber=%ld", seriesNumber);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: seriesNumber=%ld", seriesNumber] noTag:YES];
[scanner setScanLocation:beforeMediaFiles];
[scanner scanUpToString:@"proggenre=films" intoString:nil];
if ([scanner scanString:@"proggenre=films" intoString:nil]) {
isFilm = YES;
}
NSLog(@"DEBUG: isFilm = %d",isFilm);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: isFilm = %d", isFilm] noTag:YES];
downloadParams[@"authURL"] = authURL;
downloadParams[@"playPath"] = playPath;
//Proxy Test Stuff
if (isTest)
{
[nc postNotificationName:@"MetadataSuccessful" object:nil];
return;
}
NSLog(@"INFO: Metadata processed.");
[self addToLog:@"INFO: Metadata processed." noTag:YES];
NSURL *dataURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.itv.com/_app/Dynamic/CatchUpData.ashx?ViewType=5&Filter=%@",[show realPID]]];
NSLog(@"DEBUG: Programme data URL: %@",dataURL);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Programme data URL: %@", dataURL] noTag:YES];
[currentRequest clearDelegatesAndCancel];
currentRequest = [ASIHTTPRequest requestWithURL:dataURL];
[currentRequest setDidFailSelector:@selector(dataRequestFinished:)];
[currentRequest setDidFinishSelector:@selector(dataRequestFinished:)];
[currentRequest setTimeOutSeconds:10];
[currentRequest setNumberOfTimesToRetryOnTimeout:3];
[currentRequest setDelegate:self];
[currentRequest addRequestHeader:@"Accept" value:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"];
NSLog(@"INFO: Requesting programme data.");
[self addToLog:@"INFO: Requesting programme data." noTag:YES];
[currentRequest startAsynchronous];
}
-(void)dataRequestFinished:(ASIHTTPRequest *)request
{
if (!running)
return;
NSScanner *scanner = nil;
NSLog(@"DEBUG: Programme data response status code: %d", [request responseStatusCode]);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Programme data response status code: %d", [request responseStatusCode]] noTag:YES];
NSString *responseString = [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding];
NSLog(@"DEBUG: Programme data response: %@", responseString);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Programme data response: %@", responseString] noTag:YES];
NSError *error = [request error];
NSString *description = nil, *showname = nil, *senum = nil, *epnum = nil, *epname = nil, *temp_showname = nil;
if ([request responseStatusCode] == 200 && [responseString length] > 0)
{
scanner = [NSScanner scannerWithString:responseString];
[scanner scanUpToString:@"<h2>" intoString:nil];
[scanner scanString:@"<h2>" intoString:nil];
[scanner scanUpToString:@"</h2>" intoString:&temp_showname];
[scanner scanUpToString:@"<p>" intoString:nil];
[scanner scanString:@"<p>" intoString:nil];
[scanner scanUpToString:@"</p>" intoString:&description];
temp_showname = [temp_showname stringByConvertingHTMLToPlainText];
description = [description stringByConvertingHTMLToPlainText];
}
else
{
NSLog(@"WARNING: Programme data request failed. Tagging will be incomplete.");
[self addToLog:[NSString stringWithFormat:@"WARNING: Programme data request failed. Tagging will be incomplete."] noTag:YES];
NSLog(@"DEBUG: Programme data response error: %@", (error ? [error localizedDescription] : @"Unknown error"));
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Programme data response error: %@", (error ? [error localizedDescription] : @"Unknown error")] noTag:YES];
}
//Fix Showname
if (!temp_showname)
temp_showname = [show seriesName];
showname = temp_showname;
if ([show season])
senum = [NSString stringWithFormat:@"Series %ld", [show season]];
if ([show episode])
epnum = [NSString stringWithFormat:@"Episode %ld", [show episode]];
epname = [show episodeName];
if (!epname || [epname isEqualToString:@"(No Episode Name)"])
{
//Air date as backup
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
epname = [dateFormat stringFromDate:[show dateAired]];
}
if (senum) {
if (epnum) {
showname = [NSString stringWithFormat:@"%@ - %@ %@", showname, senum, epnum];
}
else {
showname = [NSString stringWithFormat:@"%@ - %@", showname, senum];
}
}
else if (epnum) {
showname = [NSString stringWithFormat:@"%@ - %@", showname, epnum];
}
if (epname && ![epname isEqualToString:temp_showname] && ![epname isEqualToString:epnum]) {
showname = [NSString stringWithFormat:@"%@ - %@", showname, epname];
}
[show setShowName:showname];
if (!description)
description = @"(No Description)";
[show setDesc:description];
NSLog(@"DEBUG: Programme data processed: showname=%@ temp_showname=%@ senum=%@ epnum=%@ epname=%@ description=%@", showname, temp_showname, senum, epnum, epname, description);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: Programme data processed: showname=%@ temp_showname=%@ senum=%@ epnum=%@ epname=%@ description=%@",
showname, temp_showname, senum, epnum, epname, description] noTag:YES];
NSLog(@"INFO: Program data processed.");
[self addToLog:@"INFO: Program data processed." noTag:YES];
//Create Download Path
[self createDownloadPath];
NSString *swfplayer = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"%@SWFURL", defaultsPrefix]];
if (!swfplayer) {
swfplayer = @"http://www.itv.com/mediaplayer/ITVMediaPlayer.swf?v=11.20.654";
}
NSMutableArray *args = [NSMutableArray arrayWithObjects:
@"-r",downloadParams[@"authURL"],
@"-W",swfplayer,
@"-y",downloadParams[@"playPath"],
@"-o",downloadPath,
nil];
if (verbose)
[args addObject:@"--verbose"];
NSLog(@"DEBUG: RTMPDump args: %@",args);
if (verbose)
[self addToLog:[NSString stringWithFormat:@"DEBUG: RTMPDump args: %@", args] noTag:YES];
[self launchRTMPDumpWithArgs:args];
}
@end