New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

google-finance

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-finance - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

lib/constants.js

6

index.js

@@ -1,7 +0,1 @@

/*
* index.js
*/
'use strict';
exports = module.exports = require('./lib');

216

lib/index.js

@@ -1,8 +0,212 @@

/*
* lib/index.js
*/
var assert = require('assert');
var os = require('os');
var util = require('util');
'use strict';
var _ = require('lodash');
var S = require('string');
var moment = require('moment');
var Promise = require('bluebird');
// Public API
exports = module.exports = {};
var _constants = require('./constants');
var _utils = require('./utils');
function _sanitizeCompanyNews(options) {
assert(_.isPlainObject(options),
'"options" must be a plain object.');
assert(!_.isUndefined(options.symbol) || !_.isUndefined(options.symbols),
'Either "options.symbol" or "options.symbols" must be defined.');
assert(_.isUndefined(options.symbol) || _.isUndefined(options.symbols),
'Either "options.symbol" or "options.symbols" must be undefined.');
assert(_.isUndefined(options.error) || _.isBoolean(options.error),
'"options.error" must be a boolean value');
if (!_.isUndefined(options.symbol)) {
assert((_.isString(options.symbol) && !_.isEmpty(options.symbol)),
'"options.symbol" must be a non-empty string.');
} else {
assert((_.isArray(options.symbols) && !_.isEmpty(options.symbols)),
'"options.symbols" must be a non-empty string array.');
}
}
function _sanitizeHistorical(options) {
assert(_.isPlainObject(options),
'"options" must be a plain object.');
assert(!_.isUndefined(options.symbol) || !_.isUndefined(options.symbols),
'Either "options.symbol" or "options.symbols" must be defined.');
assert(_.isUndefined(options.symbol) || _.isUndefined(options.symbols),
'Either "options.symbol" or "options.symbols" must be undefined.');
assert(_.isUndefined(options.error) || _.isBoolean(options.error),
'"options.error" must be a boolean value');
if (!_.isUndefined(options.symbol)) {
assert((_.isString(options.symbol) && !_.isEmpty(options.symbol)),
'"options.symbol" must be a non-empty string.');
} else {
assert((_.isArray(options.symbols) && !_.isEmpty(options.symbols)),
'"options.symbols" must be a non-empty string array.');
}
if (_.isString(options.from) && !_.isEmpty(options.from)) {
options.from = moment(options.from);
assert(options.from.isValid(), '"options.from" must be a valid date string.');
} else {
assert(_.isDate(options.from) || _.isUndefined(options.from) || _.isNull(options.from),
'"options.from" must be a date or undefined/null.');
if (_.isDate(options.from)) {
options.from = moment(options.from);
}
}
if (_.isString(options.to) && !_.isEmpty(options.to)) {
options.to = moment(options.to);
assert(options.to.isValid(), '"options.to" must be a valid date string.');
} else {
assert(_.isDate(options.to) || _.isUndefined(options.to) || _.isNull(options.to),
'"options.to" must be a date or undefined/null.');
if (_.isDate(options.to)) {
options.to = moment(options.to);
}
}
if (!options.from) {
options.from = moment('1900-01-01');
}
if (!options.to) {
options.to = moment({ hour: 0 });
}
assert((!options.from && !options.to) || !options.from.isAfter(options.to),
'"options.to" must be be greater than or equal to "options.from".');
}
function _transformCompanyNews(symbol, data) {
return _(data)
.sortBy(function (item) {
return item.date;
})
.map(function (item) {
var splits = item.guid.split(':');
return {
id: splits[splits.length - 1],
symbol: symbol,
title: S(item.title).stripTags().trim().decodeHTMLEntities().s,
description: S(item.description).stripTags().trim().decodeHTMLEntities().s,
summary: S(item.summary).stripTags().trim().decodeHTMLEntities().s,
date: item.date,
link: item.link
};
})
.value();
}
function _transformHistorical(symbol, data) {
var headings = data.shift();
return _(data)
.reverse()
.map(function (line) {
var result = {};
headings.forEach(function (heading, i) {
var value = line[i];
if (_.contains(['Volume'], heading)) {
value = _utils.toInt(value, null);
} else if (_.contains(['Open', 'High', 'Low', 'Close'], heading)) {
value = _utils.toFloat(value, null);
} else if (_.contains(['Date'], heading)) {
value = _utils.toDate(value, null);
if (value && !moment(value).isValid()) {
value = null;
}
}
result[_utils.camelize(heading)] = value;
});
result.symbol = symbol;
return result;
})
.value();
}
function companyNews(options, cb) {
if (_.isUndefined(options)) { options = {}; }
_sanitizeCompanyNews(options);
var symbols = options.symbols || _.flatten([options.symbol]);
return Promise.resolve(symbols)
.map(function (symbol) {
return Promise.resolve()
.then(function () {
return _utils.downloadRSS(_constants.COMPANY_NEWS_URL, {
output: 'rss',
q: symbol
});
})
.then(function (data) {
return _transformCompanyNews(symbol, data);
})
.catch(function (err) {
if (options.error) {
throw err;
} else {
return [];
}
});
}, {concurrency: os.cpus().length})
.then(function (result) {
if (options.symbols) {
return _.zipObject(symbols, result);
} else {
return result[0];
}
})
.catch(function (err) {
throw new Error(util.format('Failed to download data (%s)', err.message));
})
.nodeify(cb);
}
function historical(options, cb) {
if (_.isUndefined(options)) { options = {}; }
_sanitizeHistorical(options);
var symbols = options.symbols || _.flatten([options.symbol]);
return Promise.resolve(symbols)
.map(function (symbol) {
return Promise.resolve()
.then(function () {
return _utils.download(_constants.HISTORICAL_URL, {
q: symbol,
startdate: options.from.format('YYYY-MM-DD'),
enddate: options.to.format('YYYY-MM-DD'),
output: 'csv'
});
})
.then(_utils.parseCSV)
.then(function (data) {
return _transformHistorical(symbol, data);
})
.catch(function (err) {
if (options.error) {
throw err;
} else {
return [];
}
});
}, {concurrency: os.cpus().length})
.then(function (result) {
if (options.symbols) {
return _.zipObject(symbols, result);
} else {
return result[0];
}
})
.catch(function (err) {
throw new Error(util.format('Failed to download data (%s)', err.message));
})
.nodeify(cb);
}
exports.companyNews = companyNews;
exports.historical = historical;
{
"name": "google-finance",
"description": "Google Finance client library for Node.js",
"version": "0.1.0",
"description": "Google Finance company news and historical quotes data downloader written in Node.js",
"version": "0.1.1",
"license": "MIT",

@@ -18,6 +18,10 @@ "author": {

"finance",
"financial",
"data",
"stock",
"portfolio",
"market",
"data",
"price",
"quote",
"historical",
"eod",
"end-of-day",
"client",

@@ -27,12 +31,17 @@ "library"

"dependencies": {
"lodash": "~2.2.1",
"moment": "~2.4.0",
"request": "~2.27.0"
"bluebird": "^2.3.2",
"debug": "^2.0.0",
"feedparser": "^0.19.2",
"lodash": "^2.4.1",
"moment": "^2.8.3",
"request": "^2.44.0",
"string": "^1.9.1"
},
"devDependencies": {
"colors": "~0.6.2",
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.7.0",
"grunt-contrib-watch": "~0.5.3",
"matchdep": "~0.3.0"
"colors": "^0.6.2",
"grunt": "^0.4.5",
"grunt-concurrent": "^1.0.0",
"grunt-contrib-jshint": "^0.7.2",
"grunt-contrib-watch": "^0.5.3",
"load-grunt-tasks": "^0.6.0"
},

@@ -39,0 +48,0 @@ "scripts": {

# google-finance
`google-finance` is [Google Finance](https://www.google.com/finance) client library for [Node.js](http://nodejs.org/).
`google-finance` is [Google Finance](http://finance.google.com/) company news and historical quotes data downloader written in [Node.js](http://nodejs.org/).
The library handles fetching, parsing, and cleaning of CSV data and returns JSON result that is convenient and easy to work with. Both callback (last parameter) and promises (using [Bluebird](https://github.com/petkaantonov/bluebird)) styles are supported.
## Installation

@@ -11,2 +13,212 @@

## Usage
```js
var googleFinance = require('google-finance');
googleFinance.companyNews({
symbol: 'NASDAQ:AAPL'
}, function (err, news) {
//...
});
googleFinance.historical({
symbol: 'NASDAQ:AAPL',
from: '2014-01-01',
to: '2014-12-31'
}, function (err, quotes) {
//...
});
```
* [See more comprehensive examples here.](https://github.com/pilwon/node-google-finance/tree/master/examples)
## API
### Download Company News Data (single symbol)
```js
googleFinance.companyNews({
symbol: SYMBOL
}, function (err, news) {
/*
[
{
"id": "52778621773856",
"symbol": "NASDAQ:AAPL",
"title": "After Apple Inc. dodged the iPhone 6 Plus BendGate bullet, detractors wounded ...",
"description": "After Apple Inc. dodged the iPhone 6 Plus BendGate bullet, detractors wounded ...\n Apple Insider - Sep 27, 2014 \nWith new, larger iPhone 6 models now hitting the market, one Samsung executive reportedly acknowledged that \"the positive reaction from consumers to those two Apple devices\" forced the company to rush its Note 4 launch ahead of schedule, adding \"it's ...\nWill Apple's 'Bendgate' Give Samsung, BlackBerry An Opening? - Seeking Alpha (registration)",
"summary": "After Apple Inc. dodged the iPhone 6 Plus BendGate bullet, detractors wounded ...\n Apple Insider - Sep 27, 2014 \nWith new, larger iPhone 6 models now hitting the market, one Samsung executive reportedly acknowledged that \"the positive reaction from consumers to those two Apple devices\" forced the company to rush its Note 4 launch ahead of schedule, adding \"it's ...\nWill Apple's 'Bendgate' Give Samsung, BlackBerry An Opening? - Seeking Alpha (registration)",
"date": "2014-09-27T06:23:41.000Z",
"link": "http://appleinsider.com/articles/14/09/27/after-apple-inc-dodged-the-iphone-6-plus-bendgate-bullet-detractors-wounded-by-ricochet"
}
...
{
"id": "//www.fool.com/investing/general/2014/10/01/the-death-of-apple-incs-i.aspx",
"symbol": "NASDAQ:AAPL",
"title": "The Death of Apple, Inc's \"i\" Branding",
"description": "The Death of Apple, Inc's \"i\" Branding\n Motley Fool - 27 minutes ago \nIn a recent blog post, he notes that neither of the two new products Apple unveiled earlier this month carried the iBranding, with the company instead opting for \"Apple Pay \"and \"Apple Watch.\" Segall believes that the only explanation for this is that ...",
"summary": "The Death of Apple, Inc's \"i\" Branding\n Motley Fool - 27 minutes ago \nIn a recent blog post, he notes that neither of the two new products Apple unveiled earlier this month carried the iBranding, with the company instead opting for \"Apple Pay \"and \"Apple Watch.\" Segall believes that the only explanation for this is that ...",
"date": "2014-10-01T21:45:00.000Z",
"link": "http://www.fool.com/investing/general/2014/10/01/the-death-of-apple-incs-i.aspx"
}
]
*/
});
```
### Download Company News Data (multiple symbols)
```js
googleFinance.companyNews({
symbols: [SYMBOL1, SYMBOL2]
}, function (err, news) {
/*
{
"NASDAQ:AAPL": [
{
"id": "52778621773856",
"symbol": "NASDAQ:AAPL",
"title": "After Apple Inc. dodged the iPhone 6 Plus BendGate bullet, detractors wounded ...",
"description": "After Apple Inc. dodged the iPhone 6 Plus BendGate bullet, detractors wounded ...\n Apple Insider - Sep 27, 2014 \nWith new, larger iPhone 6 models now hitting the market, one Samsung executive reportedly acknowledged that \"the positive reaction from consumers to those two Apple devices\" forced the company to rush its Note 4 launch ahead of schedule, adding \"it's ...\nWill Apple's 'Bendgate' Give Samsung, BlackBerry An Opening? - Seeking Alpha (registration)",
"summary": "After Apple Inc. dodged the iPhone 6 Plus BendGate bullet, detractors wounded ...\n Apple Insider - Sep 27, 2014 \nWith new, larger iPhone 6 models now hitting the market, one Samsung executive reportedly acknowledged that \"the positive reaction from consumers to those two Apple devices\" forced the company to rush its Note 4 launch ahead of schedule, adding \"it's ...\nWill Apple's 'Bendgate' Give Samsung, BlackBerry An Opening? - Seeking Alpha (registration)",
"date": "2014-09-27T06:23:41.000Z",
"link": "http://appleinsider.com/articles/14/09/27/after-apple-inc-dodged-the-iphone-6-plus-bendgate-bullet-detractors-wounded-by-ricochet"
}
...
{
"id": "//www.fool.com/investing/general/2014/10/01/the-death-of-apple-incs-i.aspx",
"symbol": "NASDAQ:AAPL",
"title": "The Death of Apple, Inc's \"i\" Branding",
"description": "The Death of Apple, Inc's \"i\" Branding\n Motley Fool - 29 minutes ago \nIn a recent blog post, he notes that neither of the two new products Apple unveiled earlier this month carried the iBranding, with the company instead opting for \"Apple Pay \"and \"Apple Watch.\" Segall believes that the only explanation for this is that ...",
"summary": "The Death of Apple, Inc's \"i\" Branding\n Motley Fool - 29 minutes ago \nIn a recent blog post, he notes that neither of the two new products Apple unveiled earlier this month carried the iBranding, with the company instead opting for \"Apple Pay \"and \"Apple Watch.\" Segall believes that the only explanation for this is that ...",
"date": "2014-10-01T21:45:00.000Z",
"link": "http://www.fool.com/investing/general/2014/10/01/the-death-of-apple-incs-i.aspx"
}
],
"NYSE:TWTR": [
{
"id": "//www.benzinga.com/tech/14/09/4854209/why-is-twitter-inc-borrowing-more-money",
"symbol": "NYSE:TWTR",
"title": "Why Is Twitter Inc Borrowing More Money?",
"description": "Why Is Twitter Inc Borrowing More Money?\n Benzinga - Sep 16, 2014 \nTwitter Inc (NYSE: TWTR) is borrowing $1.5 billion to expand the business, but is it wise for the company to take on new debt?",
"summary": "Why Is Twitter Inc Borrowing More Money?\n Benzinga - Sep 16, 2014 \nTwitter Inc (NYSE: TWTR) is borrowing $1.5 billion to expand the business, but is it wise for the company to take on new debt?",
"date": "2014-09-16T21:22:30.000Z",
"link": "http://www.benzinga.com/tech/14/09/4854209/why-is-twitter-inc-borrowing-more-money"
}
...
{
"id": "52778622630300",
"symbol": "NYSE:TWTR",
"title": "Making Twitter Inc (TWTR) A Friendly Place",
"description": "Making Twitter Inc (TWTR) A Friendly Place\n ETF Daily News - 5 hours ago \ntwitter Discussing the problems Twitter Inc (NYSE:TWTR) faces within the social media network, with author of “Hatching Twitter” Nick Bilton.\nDrama at Twitter Inc (TWTR) Has Subsided Under The New Management: Nick ... - Insider Monkey (blog)",
"summary": "Making Twitter Inc (TWTR) A Friendly Place\n ETF Daily News - 5 hours ago \ntwitter Discussing the problems Twitter Inc (NYSE:TWTR) faces within the social media network, with author of “Hatching Twitter” Nick Bilton.\nDrama at Twitter Inc (TWTR) Has Subsided Under The New Management: Nick ... - Insider Monkey (blog)",
"date": "2014-10-01T16:18:45.000Z",
"link": "http://etfdailynews.com/2014/10/01/making-twitter-inc-twtr-a-friendly-place/"
}
]
}
*/
});
```
### Download Historical Data (single symbol)
```js
googleFinance.historical({
symbol: SYMBOL,
from: START_DATE,
to: END_DATE
}, function (err, quotes) {
/*
[
{
"date": "2014-01-02T05:00:00.000Z",
"open": 79.38,
"high": 79.58,
"low": 78.86,
"close": 79.02,
"volume": 58791957,
"symbol": "NASDAQ:AAPL"
}
...
{
"date": "2014-10-01T04:00:00.000Z",
"open": 100.59,
"high": 100.69,
"low": 98.7,
"close": 99.18,
"volume": 51304344,
"symbol": "NASDAQ:AAPL"
}
]
*/
});
```
### Download Historical Data (multiple symbols)
```js
googleFinance.historical({
symbols: [SYMBOL1, SYMBOL2],
from: START_DATE,
to: END_DATE
}, function (err, result) {
/*
{
"NASDAQ:GOOGL": [
{
"date": "2014-01-02T05:00:00.000Z",
"open": 558.29,
"high": 559.43,
"low": 554.68,
"close": 557.12,
"volume": 1822719,
"symbol": "NASDAQ:GOOGL"
}
...
{
"date": "2014-10-01T04:00:00.000Z",
"open": 586.8,
"high": 588.72,
"low": 578.02,
"close": 579.63,
"volume": 1444055,
"symbol": "NASDAQ:GOOGL"
}
],
"NASDAQ:YHOO": [
{
"date": "2014-01-02T05:00:00.000Z",
"open": 40.37,
"high": 40.49,
"low": 39.31,
"close": 39.59,
"volume": 21514650,
"symbol": "NASDAQ:YHOO"
}
...
{
"date": "2014-10-01T04:00:00.000Z",
"open": 40.66,
"high": 41.24,
"low": 40.11,
"close": 40.32,
"volume": 35130364,
"symbol": "NASDAQ:YHOO"
}
],
...
}
*/
});
```
## Credits
See the [contributors](https://github.com/pilwon/node-google-finance/graphs/contributors).
## License

@@ -17,3 +229,3 @@

Copyright (c) 2013 Pilwon Huh
Copyright (c) 2014 Pilwon Huh

@@ -38,1 +250,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

</pre>
[![Analytics](https://ga-beacon.appspot.com/UA-47034562-6/node-google-finance/readme?pixel)](https://github.com/pilwon/node-google-finance)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc