-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from pat310/26topRelated
26top related
- Loading branch information
Showing
9 changed files
with
230 additions
and
59 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
var rp = require('request-promise'); | ||
var createObj = require(__dirname + '/../resources/callbacks.js'); | ||
var checkErrors = require(__dirname + '/../resources/errorHandling.js'); | ||
var parseHtml = require(__dirname + '/../resources/htmlParser.js').parseHtml; | ||
|
||
module.exports = function request(keywords, timePeriod, geo, cbFunc){ | ||
var obj = createObj(arguments, request); | ||
if(!obj.keywords) delete obj.keywords; | ||
|
||
var error = checkErrors(obj); | ||
if(error instanceof Error) return Promise.reject(obj.cbFunc(error)); | ||
|
||
return Promise.all(promiseArr(obj.keywords || [''], obj.timePeriod, obj.geo)) | ||
.then(function(results){ | ||
return obj.cbFunc(null, results); | ||
}) | ||
.catch(function(err){ | ||
return Promise.reject(obj.cbFunc(err)); | ||
}); | ||
}; | ||
|
||
function promiseArr(keywords, timePeriod, country){ | ||
return keywords.map(function(keyword){ | ||
return rp(`http://www.google.com/trends/fetchComponent?hl=en-US&q=${keyword}&geo=${country}&cid=RISING_QUERIES_0_0&${timePeriod}`) | ||
.then(function(htmlStrings){ | ||
return parseHtml(htmlStrings); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.