5 // Created by Nathan Oates on Thu Dec 16 2004.
6 // Copyright (c) 2004 Nathan Oates. All rights reserved.
12 @implementation XMLToEPG
14 - (NSMutableArray*) importXML: (NSURL*) xmlFile {
15 NSData *xmlData = [NSData dataWithContentsOfURL:xmlFile];
16 XMLTree *tree = [[XMLTree alloc] initWithData:xmlData];
17 NSMutableArray *importedSeqs = [NSMutableArray arrayWithCapacity:1];
19 NSLog(@"tree is null");
21 XMLTree *tvTree = [tree descendentNamed:@"tv"];
22 int numOfChildren = [tvTree count];
24 while (i < numOfChildren) {
25 XMLTree *currSeq = [tvTree childAtIndex:i];
26 if ([[currSeq name] isEqualToString:@"channel"]) {
28 } else { // it's a programme
29 NSString *title = [[currSeq descendentNamed:@"title"] description];
30 NSString *rating = [[currSeq descendentNamed:@"rating"] description];
31 NSString *cat = [[currSeq descendentNamed:@"category"] description];
32 NSString *desc = [[currSeq descendentNamed:@"desc"] description];
33 NSString *rawChannel = [currSeq attributeNamed:@"channel"];
34 NSString *rawStart = [currSeq attributeNamed:@"start"];
35 NSString *rawStop = [currSeq attributeNamed:@"stop"];
36 NSString *subt = [[currSeq descendentNamed:@"sub-title"] description];
38 if (cat == nil) cat = @"";
40 sub = [NSMutableString stringWithString:@" ["];
42 sub = [NSMutableString stringWithString:subt];
43 [sub appendString:@" ["];
45 [sub appendString:cat];
46 [sub appendString:@"]"];
47 if (desc == nil) desc = @"";
48 if (title == nil) title = @"";
49 if (rating == nil) rating = @"";
50 NSString *record = @"N";
51 NSMutableDictionary *total = [NSMutableDictionary dictionaryWithObjectsAndKeys:
58 rawChannel, @"rawChannel",
59 rawStart, @"rawStart",
60 rawStop, @"rawStop", nil];
61 [importedSeqs addObject:total];
69 - (void) fixData: (NSMutableArray*) rawData {
70 NSEnumerator* e = [rawData objectEnumerator];
72 while (currentSelected = [e nextObject]) {
73 NSString* dateString = [currentSelected objectForKey:@"rawStart"];
74 NSCalendarDate *startDate = [self stringToDate:dateString];
75 [currentSelected setObject:startDate forKey:@"startDate"];
76 dateString = [currentSelected objectForKey:@"rawStop"];
77 NSCalendarDate *stopDate = [self stringToDate:dateString];
78 [currentSelected setObject:stopDate forKey:@"stopDate"];
80 [stopDate years:NULL months:NULL days:NULL hours:NULL minutes:&duration seconds:NULL sinceDate:startDate];
81 [currentSelected setObject:[NSNumber numberWithInt:duration] forKey:@"duration"];
83 NSString *rawChannel = [currentSelected objectForKey:@"rawChannel"];
84 NSString *chanName = @"";
85 NSArray *tempName = [[rawChannel componentsSeparatedByString:@"."] subarrayWithRange:(NSRange) {1,2}];
86 if ([[tempName objectAtIndex:1] isEqualToString:@"2"]) chanName = @"ABC";
87 else if ([[tempName objectAtIndex:1] isEqualToString:@"7"]) chanName = @"Seven";
88 else if ([[tempName objectAtIndex:1] isEqualToString:@"9"]) chanName = @"Nine";
89 else if ([[tempName objectAtIndex:1] isEqualToString:@"10"]) chanName = @"Ten";
90 else if ([[tempName objectAtIndex:1] isEqualToString:@"SBS"]) chanName = @"SBS";
91 [currentSelected setObject:chanName forKey:@"chanName"];
92 NSString *locationChannel = [tempName componentsJoinedByString:@"."];
93 [currentSelected setObject:locationChannel forKey:@"locChan"];
94 // if ([rawChannel ]) serviceID = 545;
95 // else if ([rawChannel isEqualTo:@"freesd.Sydney.7.d1.com.au"]) serviceID = 1313;
96 // else if ([rawChannel isEqualTo:@"freesd.Sydney.10.d1.com.au"]) serviceID = 1569;
97 // else if ([rawChannel isEqualTo:@"freesd.Sydney.9.d1.com.au"]) serviceID = 1;
98 // else if ([rawChannel isEqualTo:@"freesd.Sydney.SBS.d1.com.au"]) serviceID = 769;
99 // [currentSelected setObject:[NSNumber numberWithInt:serviceID] forKey:@"serviceID"];
101 // NSLog([currentSelected description]);
105 - (void) exportFixedXML:(NSMutableArray*) fixedData toFile:(NSString*) path {
107 [[NSData dataWithBytes:&dir length:1] writeToFile:path atomically:NO]; // write 0x00 to initialize (overwritten later)
108 NSFileHandle* outFile = [NSFileHandle fileHandleForWritingAtPath:path];
109 NSEnumerator* e = [fixedData objectEnumerator];
111 while (currentSelected = [e nextObject]) { //construct a line with the right info
112 NSMutableString *buildString = [NSMutableString stringWithString:[currentSelected objectForKey:@"chanName"]];
113 [buildString appendString:@"\t"];
114 NSCalendarDate *startDate = [currentSelected objectForKey:@"startDate"];
115 [buildString appendString:[startDate descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M" timeZone:[NSTimeZone timeZoneWithName:@"GMT"] locale:nil]];
116 [buildString appendString:@"\t"];
117 [buildString appendString:[[currentSelected objectForKey:@"duration"] description]];
118 [buildString appendString:@"\t"];
119 [buildString appendString:[currentSelected objectForKey:@"title"]];
120 [buildString appendString:@"\t"];
121 [buildString appendString:[currentSelected objectForKey:@"subtitle"]];
122 [buildString appendString:@"\t"];
123 [buildString appendString:[currentSelected objectForKey:@"description"]];
124 [buildString appendString:@"\t"];
125 [buildString appendString:[currentSelected objectForKey:@"rating"]];
126 [buildString appendString:@"\t"];
127 [buildString appendString:[currentSelected objectForKey:@"record"]];
128 [buildString appendString:@"\n"];
129 [outFile writeData:[buildString dataUsingEncoding:NSASCIIStringEncoding]];
136 - (NSCalendarDate*) stringToDate:(NSString*) inString {
137 int year = [[inString substringWithRange:(NSRange) {0,4}] intValue];
138 int month = [[inString substringWithRange:(NSRange) {4,2}] intValue];
139 int day = [[inString substringWithRange:(NSRange) {6,2}] intValue];
140 int hour = [[inString substringWithRange:(NSRange) {8,2}] intValue];
141 int min = [[inString substringWithRange:(NSRange) {10,2}] intValue];
142 int tzsecs = [[inString substringWithRange:(NSRange) {16,2}] intValue] * 60 * 60 + [[inString substringWithRange:(NSRange) {18,2}] intValue] * 60;
143 return [NSCalendarDate dateWithYear:year month:month day:day hour:hour minute:min second:0 timeZone:[NSTimeZone timeZoneForSecondsFromGMT:tzsecs]];