yahoo-stock-client
Version:
Module to fetch stock information via yahoo's stock api. Returns data in csv format.
155 lines (131 loc) • 4.42 kB
JavaScript
(function() {
var ArrayStream, Promise, StockClient, formats, querystring, request, stream, util;
Promise = require('bluebird');
request = Promise.promisifyAll(require('request'));
formats = require('./formats.js');
querystring = require('querystring');
stream = require('stream');
util = require('util');
ArrayStream = (function() {
function ArrayStream(arr) {
this.arr = arr;
stream.Readable.call(this, {
objectMode: true
});
this._init = false;
}
util.inherits(ArrayStream, stream.Readable);
ArrayStream.prototype.init = function(arr) {
this._init = true;
if (arr != null) {
this.arr = arr;
}
return this._read();
};
ArrayStream.prototype._read = function() {
if (!this._init) {
return;
}
if (this.arr.length > 0) {
return this.push(this.arr.shift());
} else {
return this.push(null);
}
};
return ArrayStream;
})();
StockClient = (function() {
function StockClient(formatOpts, config) {
var symbol, _i, _len, _ref;
if (config == null) {
config = {};
}
if (formatOpts instanceof Array) {
this._formatOpts = formatOpts;
}
if (typeof formatOpts === 'string') {
this._formatOpts = formats.Categories[formatOpts];
}
if (this._formatOpts == null) {
throw Error('FormatOpts must be Array or Category String. Got: ' + formatOpts);
}
this._config = config;
this._formatString = "";
_ref = formats.formatsToFormatSymbols(this._formatOpts);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
symbol = _ref[_i];
this._formatString += symbol;
}
}
StockClient.prototype._yahooUrl = "http://finance.yahoo.com/d/quote.csv";
StockClient.prototype._historicalUrl = 'http://ichart.finance.yahoo.com/table.csv?';
StockClient.prototype.getHistorical = function(symbol, cb) {
var arrStream, queryObj, symbolStr;
if (typeof symbol === 'string') {
symbolStr = symbol;
}
queryObj = {
s: symbol
};
if ((this._config.Stream != null) && this._config.Stream) {
arrStream = new ArrayStream([]);
request.getAsync(this._historicalUrl + querystring.stringify(queryObj)).then((function() {
var arrS;
arrS = arrStream;
return function(data) {
var dat;
dat = data[1].replace(/\r/g, '').split('\n');
dat.pop();
return arrS.init(dat);
};
})());
return arrStream;
}
if ((this._config.Async != null) && this._config.Async) {
return request.getAsync(this._historicalUrl + querystring.stringify(queryObj));
}
return request.get(this._historicalUrl + querystring.stringify(queryObj), cb);
};
StockClient.prototype.getData = function(symbol, cb) {
var arrStream, stock, symbolStr, _i, _len;
if (typeof symbol === 'string') {
symbolStr = symbol;
}
if (symbol instanceof Array) {
symbolStr = "";
for (_i = 0, _len = symbol.length; _i < _len; _i++) {
stock = symbol[_i];
symbolStr += stock + '+';
}
symbolStr = symbolStr.slice(0, -1);
}
if ((this._config.Stream != null) && this._config.Stream) {
arrStream = new ArrayStream([]);
request.getAsync(this._yahooUrl + '?s=' + symbolStr + '&f=' + this._formatString).then((function() {
var arrS;
arrS = arrStream;
return function(data) {
var dat;
dat = data[1].replace(/\r/g, '').split('\n');
dat.pop();
return arrS.init(dat);
};
})());
return arrStream;
}
if ((this._config.Async != null) && this._config.Async) {
return request.getAsync(this._yahooUrl + '?s=' + symbolStr + '&f=' + this._formatString);
}
return request.get(this._yahooUrl + '?s=' + symbolStr + '&f=' + this._formatString, cb);
};
return StockClient;
})();
/*
formatOpts is either a string or array
if its a string it is taken to be a category
else its taken to be an array of format options
*/
exports.createClient = function(formatOpts, config) {
return new StockClient(formatOpts, config);
};
}).call(this);