Use TopfieldUSBEcode for received values too.
[MacTF.git] / XMLToEPG.m
blob4182b8083011ff18d7a16f446836f0102c9af0e7
1 //
2 //  XMLToEPG.m
3 //  MacTF
4 //
5 //  Created by Nathan Oates on Thu Dec 16 2004.
6 //  Copyright (c) 2004 Nathan Oates. All rights reserved.
7 //
9 #import "XMLToEPG.h"
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];
18         if (!tree) {
19                 NSLog(@"tree is null");
20         } else {
21                 XMLTree *tvTree = [tree descendentNamed:@"tv"];
22                 int numOfChildren = [tvTree count];
23                 int i = 0;
24                 while (i < numOfChildren) {
25                         XMLTree *currSeq = [tvTree childAtIndex:i];
26                         if ([[currSeq name] isEqualToString:@"channel"]) {
27                                 //handle 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];
37                                 NSMutableString *sub;
38                                 if (cat == nil) cat = @"";
39                                 if (subt == nil)
40                                         sub = [NSMutableString stringWithString:@" ["];
41                                 else {
42                                         sub = [NSMutableString stringWithString:subt];
43                                         [sub appendString:@" ["];
44                                 }
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:
52                                         title, @"title",
53                                         rating, @"rating",
54                                         cat, @"category",
55                                         sub, @"subtitle",
56                                         desc, @"description",
57                                         record, @"record",
58                                         rawChannel, @"rawChannel",
59                                         rawStart, @"rawStart",
60                                         rawStop, @"rawStop", nil];
61                                 [importedSeqs addObject:total];
62                         }
63                                 i++;                    
64                 }
65         }
66         return importedSeqs;
69 - (void) fixData: (NSMutableArray*) rawData {
70         NSEnumerator* e = [rawData objectEnumerator];
71         id currentSelected;
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"];
79                 int duration;
80                 [stopDate years:NULL months:NULL days:NULL hours:NULL minutes:&duration seconds:NULL sinceDate:startDate];
81                 [currentSelected setObject:[NSNumber numberWithInt:duration] forKey:@"duration"];
82                 // now fix channel
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"];
100         
101         //      NSLog([currentSelected description]);
102         }
105 - (void) exportFixedXML:(NSMutableArray*) fixedData toFile:(NSString*) path {
106         char dir = 0x00;
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];
110         id currentSelected;
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]];
130         }
131         [outFile closeFile];
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]];
146 @end