Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

28add date range #33

Merged
merged 9 commits into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [v2.1.0]
> Oct 22, 2016

- `trendData` now accepts an optional `timePeriod` object that will return trend data based on a provided time period ([#33])

[v2.1.0]: https://github.com/pat310/google-trends-api/compare/v2.0.0...v2.1.0
[#33]: https://github.com/pat310/google-trends-api/pull/33

## [v2.0.0]
> Oct 22, 2016

Expand Down
196 changes: 112 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ var options = {
geo: 'country name',
date: 'yyyymm',
keywords: ['some', 'list', 'of', 'keywords'],
category: 'some category'
category: 'some category',
timePeriod: {
type: enumerated string 'hour', 'day', 'month', or 'year'
value: number
}
}

googleTrends.apiMethod(options)
Expand Down Expand Up @@ -122,7 +126,7 @@ The examples shown for each API method can be run by changing into the home `goo

### API Methods
The following API methods are available:
* [trendData](#trenddata): returns the historical trend data to a provided keyword or an array of keywords.
* [trendData](#trenddata): returns the historical trend data to a provided keyword or an array of keywords - optionally accepts a `timePeriod` object
* [topRelated](#toprelated): returns the top related keywords to a provided keyword or an array of keywords along with it's percentage of correlation.
* [hotTrends](#hottrends): returns the current top 20 trending searches for a given location.
* [hotTrendsDetail](#hottrendsdetail): same as the [hotTrends](#hottrends) results except with more detail such as links, publication date, approximate traffic, etc.
Expand All @@ -145,12 +149,13 @@ For each of the API methods, rather than providing the parameters to the functio
*Returns the historical trend data to a provided keyword or an array of keywords.*

#####Syntax
`googleTrends.trendData(['keywords'])`
`googleTrends.trendData(['keywords'], {type: 'string', value: number})`

* `['keywords']` - either an array of keywords as strings or a string with one keyword. If keywords is an array, the results will be returned in an array of the same order as the input. Entering a keyword is **required**.
* `{type: 'string', value: number}` - the `timePeriod` object that must be formatted with keys `type` (which is an enumerated string of either 'hour', 'day', 'month', or 'year') and `value` (which is a number). Entering a `timePeriod` is optional. If no `timePeriod` object is provided, by default `trendData` will return all past trend data available.

#####Example
The following example provides the historical trend data for 'OJ Simpson'. Optionally, the input could have been provided as `googleTrends.trendData({keywords: 'OJ Simpson'})`. Any other keys provided in the object will be ignore.
The following example provides the historical trend data for 'OJ Simpson'. Optionally, the input could have been provided as `googleTrends.trendData({keywords: 'OJ Simpson'})`.

######Input
```js
Expand All @@ -165,43 +170,27 @@ googleTrends.trendData('OJ Simpson')

######Output
```js
[
{
"query": "oj simpson",
"values": [{
"date": "Thu, 01 Jan 2004 06:00:00 GMT",
"value": 4
}, {
"date": "Sun, 01 Feb 2004 06:00:00 GMT",
"value": 3
}, {
"date": "Mon, 01 Mar 2004 06:00:00 GMT",
"value": 3
}, {
"date": "Thu, 01 Apr 2004 06:00:00 GMT",
"value": 3
}, {
"date": "Sat, 01 May 2004 05:00:00 GMT",
"value": 4
}, {
"date": "Tue, 01 Jun 2004 05:00:00 GMT",
"value": 7
}, {
"date": "Thu, 01 Jul 2004 05:00:00 GMT",
"value": 2
}, {
"date": "Sun, 01 Aug 2004 05:00:00 GMT",
"value": 2
},
...
}, {
"date": "Mon, 01 Aug 2016 05:00:00 GMT",
"value": 9
}
}
]
[ { query: 'oj simpson',
values:
[ { date: '2003-12-01T05:00:00.000Z', value: 4 },
{ date: '2004-01-01T05:00:00.000Z', value: 4 },
{ date: '2004-02-01T05:00:00.000Z', value: 3 },
{ date: '2004-03-01T05:00:00.000Z', value: 4 },
{ date: '2004-04-01T05:00:00.000Z', value: 5 },
{ date: '2004-05-01T04:00:00.000Z', value: 7 },
{ date: '2004-06-01T04:00:00.000Z', value: 2 },
{ date: '2004-07-01T04:00:00.000Z', value: 2 },
{ date: '2004-08-01T04:00:00.000Z', value: 2 },
{ date: '2004-09-01T04:00:00.000Z', value: 4 },
{ date: '2004-10-01T04:00:00.000Z', value: 4 },
{ date: '2004-11-01T05:00:00.000Z', value: 3 },
{ date: '2004-12-01T05:00:00.000Z', value: 3 },
... more items ] } ]
```

#####Example
The following example provides the historical trend data for 'swimming' and the 'olympics'. Optionally, the input could have been provided as `googleTrends.trendData({keywords: ['swimming', 'olympics']})`.

######Input
```js
googleTrends.trendData(['swimming', 'olympics'])
Expand All @@ -215,50 +204,89 @@ googleTrends.trendData(['swimming', 'olympics'])

######Output
```js
[
{
"query": "swimming",
"values": [{
"date": "Thu, 01 Jan 2004 06:00:00 GMT",
"value": 5
}, {
"date": "Sun, 01 Feb 2004 06:00:00 GMT",
"value": 5
}, {
"date": "Mon, 01 Mar 2004 06:00:00 GMT",
"value": 5
}, {
"date": "Thu, 01 Apr 2004 06:00:00 GMT",
"value": 5
}, {
...
}, {
"date": "Mon, 01 Aug 2016 05:00:00 GMT",
"value": 10
}]
},
{
"query": "olympics",
"values": [{
"date": "Thu, 01 Jan 2004 06:00:00 GMT",
"value": 2
}, {
"date": "Sun, 01 Feb 2004 06:00:00 GMT",
"value": 3
}, {
"date": "Mon, 01 Mar 2004 06:00:00 GMT",
"value": 3
}, {
"date": "Thu, 01 Apr 2004 06:00:00 GMT",
"value": 3
}, {
...
}, {
"date": "Mon, 01 Aug 2016 05:00:00 GMT",
"value": 100
}]
}
]
[ { query: 'swimming',
values:
[ { date: '2003-12-01T05:00:00.000Z', value: 7 },
{ date: '2004-01-01T05:00:00.000Z', value: 7 },
{ date: '2004-02-01T05:00:00.000Z', value: 7 },
{ date: '2004-03-01T05:00:00.000Z', value: 7 },
{ date: '2004-04-01T05:00:00.000Z', value: 8 },
{ date: '2004-05-01T04:00:00.000Z', value: 9 },
{ date: '2004-06-01T04:00:00.000Z', value: 10 },
{ date: '2004-07-01T04:00:00.000Z', value: 11 },
{ date: '2004-08-01T04:00:00.000Z', value: 7 },
{ date: '2004-09-01T04:00:00.000Z', value: 6 },
{ date: '2004-10-01T04:00:00.000Z', value: 5 },
{ date: '2004-11-01T05:00:00.000Z', value: 4 },
{ date: '2004-12-01T05:00:00.000Z', value: 6 },
{ date: '2005-01-01T05:00:00.000Z', value: 6 },
{ date: '2005-02-01T05:00:00.000Z', value: 6 },
... more items ] },
{ query: 'olympics',
values:
[ { date: '2003-12-01T05:00:00.000Z', value: 3 },
{ date: '2004-01-01T05:00:00.000Z', value: 4 },
{ date: '2004-02-01T05:00:00.000Z', value: 4 },
{ date: '2004-03-01T05:00:00.000Z', value: 4 },
{ date: '2004-04-01T05:00:00.000Z', value: 5 },
{ date: '2004-05-01T04:00:00.000Z', value: 5 },
{ date: '2004-06-01T04:00:00.000Z', value: 8 },
{ date: '2004-07-01T04:00:00.000Z', value: 66 },
{ date: '2004-08-01T04:00:00.000Z', value: 8 },
{ date: '2004-09-01T04:00:00.000Z', value: 3 },
{ date: '2004-10-01T04:00:00.000Z', value: 3 },
... more items ] } ]
```

#####Example
The following example provides the historical trend data for 'OJ Simpson' for the past 5 days. Optionally, the input could have been provided as `googleTrends.trendData('OJ Simpson', {type: 'day', value: 5})`.

######Input
```js
googleTrends.trendData({keywords: 'Oj Simpson', timePeriod: {type: 'day', value: 5}})
.then(function(results){
console.log(results);
})
.catch(function(err){
console.error(err);
});
```

######Output
**Note: Query was conducted on 10/22 so 5 days back leads to results starting at 10/17**
```js
[ { query: 'oj simpson',
values:
[ { date: '2016-10-17T21:00:00.000Z', value: 20 },
{ date: '2016-10-17T22:00:00.000Z', value: 18 },
{ date: '2016-10-17T23:00:00.000Z', value: 17 },
{ date: '2016-10-18T00:00:00.000Z', value: 23 },
{ date: '2016-10-18T01:00:00.000Z', value: 20 },
{ date: '2016-10-18T02:00:00.000Z', value: 22 },
{ date: '2016-10-18T03:00:00.000Z', value: 19 },
{ date: '2016-10-18T04:00:00.000Z', value: 15 },
{ date: '2016-10-18T05:00:00.000Z', value: 11 },
{ date: '2016-10-18T06:00:00.000Z', value: 8 },
{ date: '2016-10-18T07:00:00.000Z', value: 7 },
{ date: '2016-10-18T08:00:00.000Z', value: 7 },
{ date: '2016-10-18T09:00:00.000Z', value: 6 },
{ date: '2016-10-18T10:00:00.000Z', value: 7 },
{ date: '2016-10-18T11:00:00.000Z', value: 7 },
{ date: '2016-10-18T12:00:00.000Z', value: 10 },
{ date: '2016-10-18T13:00:00.000Z', value: 10 },
{ date: '2016-10-18T14:00:00.000Z', value: 10 },
{ date: '2016-10-18T15:00:00.000Z', value: 11 },
{ date: '2016-10-18T16:00:00.000Z', value: 13 },
{ date: '2016-10-18T17:00:00.000Z', value: 13 },
{ date: '2016-10-18T18:00:00.000Z', value: 18 },
{ date: '2016-10-18T19:00:00.000Z', value: 16 },
{ date: '2016-10-18T20:00:00.000Z', value: 16 },
{ date: '2016-10-18T21:00:00.000Z', value: 19 },
{ date: '2016-10-18T22:00:00.000Z', value: 33 },
{ date: '2016-10-18T23:00:00.000Z', value: 81 },
{ date: '2016-10-19T00:00:00.000Z', value: 100 },
{ date: '2016-10-19T01:00:00.000Z', value: 30 },
... more items ] } ]
```

[back to top](#introduction)
Expand Down Expand Up @@ -626,7 +654,7 @@ The following example provides the top charts for actors in January 2016 in the

######Input
```js
googleTrends.categoryTopCharts({'actors', '201601', 'US'})
googleTrends.categoryTopCharts('actors', '201601', 'US')
.then(function(results){
console.log(results);
})
Expand Down
15 changes: 12 additions & 3 deletions examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var util = require('util');
/* ~=~=~=~=~=~=~=~=~=~= EXAMPLE 1 =~=~=~=~=~=~=~=~=~=~ */
/* ~=~=~=~=~=~=~=~=~=~= trendData =~=~=~=~=~=~=~=~=~=~ */
// // Parameters: array of key words (required)
// // optionally as the first argument pass an object: {keywords: ['dog']}
// // optionally as the first argument pass an object instead: {keywords: ['dog']}
// googleTrends.trendData({keywords: 'OJ Simpson'})
// .then(function(trendData){
// console.log('here are the results', util.inspect(trendData, {showHidden: false, depth: null}));
Expand All @@ -17,8 +17,8 @@ var util = require('util');
// return err;
// });

// Trend data example with multiple keywords
// googleTrends.trendData({keywords: ['OJ Simpson', 'Michael Phelps']})
// // Trend data example with multiple keywords
// googleTrends.trendData(['swimming', 'olympics'])
// .then(function(trendData){
// console.log('here are the results', util.inspect(trendData, {showHidden: false, depth: null}));
// })
Expand All @@ -27,6 +27,15 @@ var util = require('util');
// return err;
// });

// // Trend data example with a time period provided (timePeriod is an optional parameter)
// googleTrends.trendData({keywords: 'OJ Simpson', timePeriod: {type: 'day', value: 5}})
// .then(function(trendData){
// console.log('here are the results', util.inspect(trendData, {showHidden: false, depth: null}));
// })
// .catch(function(err){
// console.log("there was an error", err);
// return err;
// });


/* ~=~=~=~=~=~=~=~=~=~= EXAMPLE 2 =~=~=~=~=~=~=~=~=~=~ */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "an API layer on top of google trends",
"main": "index.js",
"scripts": {
"test": "mocha",
"test": "mocha --timeout 15000",
"compile": "./node_modules/.bin/babel --presets es2015 -d lib/ src/",
"test-cov": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*",
"prepublish": "npm run compile",
Expand Down
5 changes: 4 additions & 1 deletion src/resources/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var timePeriodConverter = require(__dirname + '/../resources/timePeriodConverter.js');

function generic(err, results){
if(err) return err;
Expand All @@ -20,7 +21,7 @@ function parseArguments(args, func){
var parameters = getParamNames(func);

var returnObj = parameters.reduce(function(acc, curr, index){
if((typeof args[index] !== 'object' || Array.isArray(args[index])) && curr !== 'cbFunc') acc[curr] = typeof args[index] !== 'function' ? args[index] : undefined;
if((curr === 'timePeriod' || typeof args[index] !== 'object' || Array.isArray(args[index])) && curr !== 'cbFunc') acc[curr] = typeof args[index] !== 'function' ? args[index] : undefined;
if(typeof args[index] === 'function') acc.cbFunc = args[index];
return acc;
}, {});
Expand All @@ -30,6 +31,8 @@ function parseArguments(args, func){
returnObj.cbFunc = typeof returnObj.cbFunc === "function" ? returnObj.cbFunc : generic;

if(returnObj.keywords) returnObj.keywords = Array.isArray(returnObj.keywords) ? returnObj.keywords : [returnObj.keywords];

returnObj.timePeriod = timePeriodConverter(returnObj.timePeriod);

return returnObj;
}
Expand Down
2 changes: 1 addition & 1 deletion src/resources/errorHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const COUNTRY = require(__dirname + '/countryCodes.js');
const DATE = require(__dirname + '/dateValidate.js');


function checkErrors(obj){
var keys = Object.keys(obj);

Expand All @@ -20,6 +19,7 @@ function checkErrors(obj){
obj.countryDomain = COUNTRY.getDomain(obj.geo);
obj.countryCode = COUNTRY.getCode(obj.geo);

if(obj.timePeriod && obj.timePeriod instanceof Error) return obj.timePeriod;
}

function findKey(searchKey, keys){
Expand Down
2 changes: 1 addition & 1 deletion src/resources/htmlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function parseJSON(htmlString) {
}
for (let j = 0; j < parsedTrends.table.rows.length; j++) {
var data = parsedTrends.table.rows[j].c,
date = data[0].v;
date = new Date(data[0].f);
for (let k = 1; k < data.length; k++) {
if (data[k]) {
trendsData[k - 1].values.push({
Expand Down
18 changes: 18 additions & 0 deletions src/resources/timePeriodConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

module.exports = function(timePeriodObj){
if(!timePeriodObj) return '';
if(typeof timePeriodObj !== 'object') return new Error('timePeriod must be an object of type {type: enum, value: number}');

const enumTypes = {
hour: ['now', 'H'],
day: ['now', 'd'],
month: ['today', 'm'],
year: ['today', 'm']
};

if(typeof timePeriodObj.type !== 'string' || !enumTypes[timePeriodObj.type.toLowerCase()]) return new Error('type must be one of the specified enumerated types');
if(!timePeriodObj.value || typeof timePeriodObj.value !== 'number' ) return new Error('timePeriod value must be a number');

return `date=${enumTypes[timePeriodObj.type.toLowerCase()][0]} ${timePeriodObj.type.toLowerCase() === 'year' ? Math.round(timePeriodObj.value) * 12 : Math.round(timePeriodObj.value)}-${enumTypes[timePeriodObj.type.toLowerCase()][1]}`;
}
8 changes: 4 additions & 4 deletions src/utils/trendData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ var parseJSON = require(__dirname + '/../resources/htmlParser.js').parseJSON;
var groupKeywords = require(__dirname + '/../resources/trendDataHelper.js').groupKeywords;
var reduceArrayDimensions = require(__dirname + '/../resources/trendDataHelper.js').reduceArrayDimensions;

module.exports = function request(keywords, cbFunc){
module.exports = function request(keywords, timePeriod, cbFunc){
var obj = createObj(arguments, request);

var error = checkErrors(obj);
if(error instanceof Error) return Promise.reject(obj.cbFunc(error));

return Promise.all(promiseArr(obj.keywords))
return Promise.all(promiseArr(obj.keywords, obj.timePeriod))
.then(function(results){
return obj.cbFunc(null, reduceArrayDimensions(results));
})
Expand All @@ -22,9 +22,9 @@ module.exports = function request(keywords, cbFunc){
});
};

function promiseArr(keywords){
function promiseArr(keywords, timePeriod){
return groupKeywords(keywords).map(function(keyword, index, arr){
return rp(`http://www.google.com/trends/fetchComponent?q=${keyword}&cid=TIMESERIES_GRAPH_0&export=3`)
return rp(`http://www.google.com/trends/fetchComponent?q=${keyword}&cid=TIMESERIES_GRAPH_0&export=3&${timePeriod}`)
.then(function(htmlString){
return parseJSON(htmlString, arr[index].split(','));
});
Expand Down
Loading