Skip to content

Instantly share code, notes, and snippets.

@possan
Last active December 23, 2015 09:19
Show Gist options
  • Save possan/6613939 to your computer and use it in GitHub Desktop.
Save possan/6613939 to your computer and use it in GitHub Desktop.
var sys = require('util');
var rest = require('restler');
var fs = require('fs');
var database = {};
var list = [ 'AAPL', 'IBM', 'GE' ];
var done = 0;
function save_all_tickers() {
console.log('done, all data:', database);
fs.writeFile('output.json', JSON.stringify(database), function(done) {
console.log('file written, program will exit.');
});
}
function one_ticker_done(name, result) {
console.log('one_ticker_done', name, result);
if (result instanceof Error) {
console.log(name + ' failed, retrying...');
this.retry(5000);
} else {
console.log(name + ' got: ' + result);
var arr = result.split(',');
if (typeof(database[name]) == 'undefined') {
database[name] = arr[5]; // sixth number is the actual value
done ++;
if (done == list.length) {
// all done.
save_all_tickers();
}
}
}
}
function download_one_ticker(name) {
console.log('downloading ' + name + ' ...');
rest.get('http://finance.yahoo.com/d/quotes.csv?s=' + name + '&f=snl1d1t1ohgdr')
.on('complete', one_ticker_done.bind(this, name));
}
list.forEach(download_one_ticker);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment