From 7b786b24ccb501268f0d2890a9918ddc2af35413 Mon Sep 17 00:00:00 2001 From: ritfsaecdaq Date: Sat, 27 Jan 2018 14:47:32 -0500 Subject: [PATCH] added word scoring functionality --- googletrendsgame/server/trendsAPI.js | 34 ++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/googletrendsgame/server/trendsAPI.js b/googletrendsgame/server/trendsAPI.js index c3abe26..2cf9303 100644 --- a/googletrendsgame/server/trendsAPI.js +++ b/googletrendsgame/server/trendsAPI.js @@ -1,9 +1,35 @@ - - +const googt = require('google-trends-api'); +const Promise = require('promise'); +const DAY = 1000 * 60 * 60 * 24; module.exports= { + /* + desc: returns an integer score for the word over the day + */ getPopularity: function(word) { - return 0; + //must be a promise since call to trends API is async + return new Promise(function(resolve, reject){ + + //specifies the keyword, time interval, and granularity + googt.interestOverTime({keyword:word, + startTime:new Date(Date.now() - DAY),granularTimeResolution:true}) + .then(function(results){ + //turn into json object + data = JSON.parse(results).default.timelineData; + + //add up values + var total = 0; + data.forEach(function(element){ + console.log(element.formattedTime + " " + element.value[0]); + total += element.value[0]; + }) + + //tell function to return + resolve(total); + }).catch(function(err){ + reject("Google Trends Query Failed"); + }); + }); } - }; \ No newline at end of file + };