Skip to content

Instantly share code, notes, and snippets.

@johnvoorhees
Last active January 3, 2016 01:59
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/8392903 to your computer and use it in GitHub Desktop.
Save johnvoorhees/8392903 to your computer and use it in GitHub Desktop.
ComEd Electricity Price Tracker that can be tied to Panic's Status Board iPad app to track real versus actual electricity prices for ComEd, the Chicago-area electric utility.
//
// main.m
// Status Board Widget Predicted and Actual ComEd Prices
//
// Created by John Voorhees.
// Copyright (c) 2013 John Voorhees. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//create date string to append to URL string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [NSDate date];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
//NSLog(@"formattedDateString: %@", formattedDateString);
//today's prices string
NSString *fullURL = [NSString stringWithFormat:@"http://rrtp.comed.com/rrtp/ServletFeed?type=pricingtabledual&date=%@", formattedDateString];
//NSLog(@"URL is %@", fullURL);
//Get contents of fullURL
NSURL *hourlyPriceURL = [NSURL URLWithString:fullURL];
NSError *error = nil;
NSString *contents = [[NSString alloc] initWithContentsOfURL:hourlyPriceURL encoding:NSUTF8StringEncoding error:&error];
//NSLog(@"data from web site: %@", contents);
//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:@"\n"];
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,"];
//NSLog(@"New string: %@", contents);
//csv-ready string
NSString *csvContents = [NSString stringWithFormat:@"Electricity Prices,Predicted,Actual\n%@", contents];
//NSLog(@"%@", csvContents);
[csvContents writeToFile:@"/Users/voorheesfamily/DropBox/dailyPrice.csv" atomically:NO encoding:NSUTF8StringEncoding error:&error];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment