Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var request = require('request');
function main(message) {
return new Promise(function(resolve, reject) {
// create API request
var now = new Date();
var tagged = message.tags.join(';');
var url = 'https://api.stackexchange.com/2.2/search';
var r = {
method: 'get',
url: url,
qs: {
key: message.apikey,
site: 'stackoverflow',
fromdate: Math.floor((now.getTime()/1000)-(1*24*60*60)),
order: 'asc',
sort: 'activity',
tagged: tagged,
filter: 'withbody'
},
json: true,
gzip: true
};
request(r, function(err, response, body) {
if (err) {
return reject(err);
}
if (response.statusCode != 200) {
console.log(body);
throw(new Error('status code not OK (got a ' + response.statusCode + ')'));
}
if (!body.items) {
throw(new Error('missing items in response'));
}
resolve({ items: body.items });
});
});
}
module.exports = main;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment