not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB

  1. const googt = require('google-trends-api');
  2. const DAY = 1000 * 60 * 60 * 24; // 1 day in milliseconds
  3. const YEAR = DAY * 365; // 1 year in milliseconds
  4. const GEO = 'US'; //the scope of the trends
  5. /**
  6. desc: helper function for getYearStats, gets list of popularity for days in
  7. the month.
  8. */
  9. function getMonthStats(word, month){
  10. return new Promise(function(resolve, reject){
  11. //set up query for 1 month length
  12. googt.interestOverTime({
  13. keyword:word,
  14. startTime:new Date(Date.now() - (YEAR * month/12)),
  15. endTime:new Date(Date.now() - (YEAR * (month-1)/12))
  16. }).then(function(results){
  17. //parse the json, fill return array w tuples of date + value
  18. var times = JSON.parse(results).default.timelineData;
  19. var ret = [];
  20. for(var i = 0; i < times.length; ++i){
  21. var tup = new Object();
  22. tup.time = times[i].formattedTime;
  23. tup.value = times[i].value[0];
  24. ret[i] = tup;
  25. }
  26. resolve(ret);
  27. });
  28. });
  29. }
  30. module.exports=
  31. {
  32. /*
  33. desc: returns an integer score for the word over the day
  34. */
  35. getPopularity: function(word)
  36. {
  37. //must be a promise since call to trends API is async
  38. return new Promise(function(resolve, reject){
  39. //specifies the keyword, time interval, granularity, and region
  40. googt.interestOverTime({keyword:word,
  41. startTime:new Date(Date.now() - DAY),granularTimeResolution:true,
  42. geo:GEO
  43. })
  44. .then(function(results){
  45. //turn into json object
  46. data = JSON.parse(results).default.timelineData;
  47. //add up values
  48. var total = 0;
  49. data.forEach(function(element){
  50. total += element.value[0];
  51. })
  52. //tell function to return
  53. console.log("********************" + total);
  54. //pl.selectWord2(total);
  55. resolve(total);
  56. }).catch(function(err){
  57. reject("Google Trends Query Failed");
  58. });
  59. });
  60. },
  61. /**
  62. desc: returns a list of tuples (date, value) representing word's
  63. popularity for the past year
  64. */
  65. /*
  66. getYearStats: async function(word){
  67. var ret = [];
  68. for (var i = 9; i > 0; --i) {
  69. await getMonthStats(word,i).then(function(data){
  70. ret = ret.concat(data);
  71. });
  72. console.log(i);
  73. }
  74. return ret;
  75. }
  76. */
  77. };