Created
July 2, 2014 11:10
-
-
Save GenralDesigns/62129d0ed577f69df19b to your computer and use it in GitHub Desktop.
Objective-C CSV Exchange Rate Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)createExchangeRateCSVFile { | |
//Put your output path here: | |
NSString *outputPath = @"/Users/Milo/Desktop/ExchangeRates.csv"; | |
NSArray *currencyCodes = [NSLocale ISOCurrencyCodes]; | |
NSMutableArray *array = [NSMutableArray array]; | |
for (NSString *currencyCode in currencyCodes) { | |
@autoreleasepool { | |
//For monitoring progress, as first letter goes towards z, you'll know its | |
//getting closer to being finished | |
printf("%s\n", [currencyCode UTF8String]); | |
//For first 250 Currencies: | |
NSMutableString *urlInputString = [@"" mutableCopy]; | |
//Get url input | |
for (int i = 0; i < 250; i++) { | |
if (i != 249) { | |
[urlInputString appendFormat:@"\"%@%@\",", currencyCode, currencyCodes[i]]; | |
} else { | |
[urlInputString appendFormat:@"\"%@%@\"", currencyCode, currencyCodes[i]]; | |
} | |
} | |
NSString *yahooURL = [NSString stringWithFormat:@"https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in (%@)&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=", urlInputString]; | |
yahooURL = [yahooURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
//Query yahoo | |
NSData *resultData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yahooURL]] returningResponse:nil error:nil]; | |
NSDictionary *JSONResponse = [NSJSONSerialization JSONObjectWithData:resultData options:0 error:nil]; | |
NSArray *rates = JSONResponse[@"query"][@"results"][@"rate"]; | |
//Add result to array | |
for (NSDictionary *rate in rates) { | |
//Make sure there is an exchange rate | |
if (![rate[@"Rate"] isEqualToString:@"0.00"]) { | |
[array addObject:rate[@"id"]]; | |
[array addObject:rate[@"Rate"]]; | |
} | |
} | |
//For remaining | |
NSMutableString *urlInputString1 = [@"" mutableCopy]; | |
for (int i = 250; i < currencyCodes.count; i++) { | |
if (i != currencyCodes.count - 1) { | |
[urlInputString1 appendFormat:@"\"%@%@\",", currencyCode, currencyCodes[i]]; | |
} else { | |
[urlInputString1 appendFormat:@"\"%@%@\"", currencyCode, currencyCodes[i]]; | |
} | |
} | |
NSString *yahooURL1 = [NSString stringWithFormat:@"https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in (%@)&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=", urlInputString1]; | |
yahooURL1 = [yahooURL1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
NSData *resultData1 = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yahooURL1]] returningResponse:nil error:nil]; | |
NSDictionary *JSONResponse1 = [NSJSONSerialization JSONObjectWithData:resultData1 options:0 error:nil]; | |
NSArray *rates1 = JSONResponse1[@"query"][@"results"][@"rate"]; | |
for (NSDictionary *rate in rates1) { | |
//Again, make sure the rate exists | |
if (![rate[@"Rate"] isEqualToString:@"0.00"]) { | |
[array addObject:rate[@"id"]]; | |
[array addObject:rate[@"Rate"]]; | |
} | |
} | |
} | |
} | |
NSString *outputString = [array componentsJoinedByString:@","]; | |
[outputString writeToFile:outputPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment