Skip to content

Instantly share code, notes, and snippets.

@johnvoorhees
Created January 20, 2014 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnvoorhees/8526053 to your computer and use it in GitHub Desktop.
Save johnvoorhees/8526053 to your computer and use it in GitHub Desktop.
Objective-C code for pulling daily predicted and actual electricity price data from ComEd.
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
int day = 1;
int month = 1;
int year = 2007;
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:day];
[comps setMonth:month];
[comps setYear:year];
//build calendar
NSCalendar *gregorian1 = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *startDate = [gregorian1 dateFromComponents:comps];
//end date
NSDate *todayDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:startDate];
NSInteger theDay = [todayComponents day];
NSInteger theMonth = [todayComponents month];
NSInteger theYear = [todayComponents year];
// now build a NSDate object for yourDate using these components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:theDay];
[components setMonth:theMonth];
[components setYear:theYear];
NSDate *thisDate = [gregorian dateFromComponents:components];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit)
fromDate:startDate
toDate:todayDate
options:0];
NSMutableString *contentsMutableCopy = [[NSMutableString alloc] init];
[contentsMutableCopy insertString: @"Date,Time,Predicted,Actual\n" atIndex:0];
//NSLog(@"Days between dates: %ld", [differenceComponents day]);
int n=0;
for(n=0; n <= [differenceComponents day]; n++)
{
// now build a NSDate object for the next day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:n];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"yyyyMMdd"];
NSString *formattedDateString = [DateFormatter stringFromDate:nextDate];
//today's prices string
NSString *fullURL = [NSString stringWithFormat:@"http://rrtp.comed.com/rrtp/ServletFeed?type=pricingtabledual&date=%@", formattedDateString];
//Get contents of fullURL
NSURL *hourlyPriceURL = [NSURL URLWithString:fullURL];
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:hourlyPriceURL encoding:NSUTF8StringEncoding error:&error];
//strip out html from contents of URL contents
contents = [contents stringByReplacingOccurrencesOfString:@"&cent;</td><td>" withString:@","];
contents = [contents stringByReplacingOccurrencesOfString:@"&cent;</td></tr><tr>" withString:@"\n ,"];
contents = [contents stringByReplacingOccurrencesOfString:@"<tr>" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@"<td>" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@"</tr>" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@"</td>" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@"<small>" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@"</small>" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@";" withString:@""];
contents = [contents stringByReplacingOccurrencesOfString:@"M" withString:@"M,"];
contents = [contents stringByReplacingOccurrencesOfString:@"&cent" withString:@""];
NSString *combo = [formattedDateString stringByAppendingString:@","];
NSString *comboDatePrice = [combo stringByAppendingString:contents];
//csv-ready string
[contentsMutableCopy appendString: comboDatePrice];
NSLog(@"comboDatePrice=%@",comboDatePrice);
}
NSError *error;
[contentsMutableCopy writeToFile:@"/Users/johnvoorhees/DropBox/electricityPrices.csv" atomically:NO encoding:NSUTF8StringEncoding error:&error];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment