Skip to content

Instantly share code, notes, and snippets.

@buremba
Last active August 29, 2017 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buremba/0d200af6bf02544035e0a1f2c9651dd2 to your computer and use it in GitHub Desktop.
Save buremba/0d200af6bf02544035e0a1f2c9651dd2 to your computer and use it in GitHub Desktop.
var https = require('https');
var data = __REPLACE_JSON__;
var counter = data.length;
var result = {data: {}, error: {}};
data.forEach(function (keyword) {
var options = {
host: 'trends.google.com',
port: 443,
path: '/trends/api/autocomplete/' + encodeURIComponent(keyword) + '?hl=en-EN'
};
https.get(options, function (resp) {
var body = '';
resp.on('data', function (d) {
body += d;
});
resp.on('end', function () {
counter -= 1;
var category_value = JSON.parse(body.substring(6)).default.topics;
result.data[keyword] = (category_value && category_value.length > 0) ? category_value[0] : null;
if (counter == 0) {
console.log(JSON.stringify(result));
}
});
}).on("error", function (e) {
counter -= 1;
result.data[keyword] = "Error:" + e.message;
if (counter == 0) {
console.log(JSON.stringify(result));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment