Skip to content

Instantly share code, notes, and snippets.

@kylehotchkiss
Created January 29, 2017 05:20
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 kylehotchkiss/3e1a043af1f86e097a2d98959eec2a18 to your computer and use it in GitHub Desktop.
Save kylehotchkiss/3e1a043af1f86e097a2d98959eec2a18 to your computer and use it in GitHub Desktop.
var moment = require('moment');
var request = require('request');
var key = '';
var secret = '';
var token = '';
// get key
/*request.post({
json: true,
url: 'https://api.twitter.com/oauth2/token?grant_type=client_credentials',
headers: {
'User-Agent': 'Trump Feels v0.1',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Authorization': 'Basic ' + new Buffer(key + ':' + secret).toString('base64')
}
}, function( error, res, body ) {*/
//})
var getTrumpTweets = function( query, callback ) {
request.get({
json: true,
url: 'https://api.twitter.com/1.1/search/tweets.json',
qs: {
count: 100,
q: query// '@realDonaldTrump :( since:2017-1-28'
},
headers: {
'User-Agent': 'Trump Feels v0.1',
'Authorization': 'Bearer ' + token
}
}, function( error, res, body ) {
callback( false, body.statuses );
});
}
getTrumpTweets('@realDonaldTrump :(', function( error, sadTweets ) {
getTrumpTweets('@realDonaldTrump :)', function( error, happyTweets ) {
var countPC = 0;
var happyString = happyTweets[happyTweets.length - 1].created_at.replace("+0000 ", "") + " UTC";
var happyDate = new Date(happyString);
var sadString = sadTweets[sadTweets.length - 1].created_at.replace("+0000 ", "") + " UTC";
var sadDate = new Date(sadString);
var happyDiff = moment().diff(happyDate, 'seconds');
var sadDiff = moment().diff(sadDate, 'seconds');
console.log( happyDiff, sadDiff );
if ( sadDiff > happyDiff ) {
// more people are positive
for ( var i in sadTweets ) {
var time = new Date(sadTweets[i].created_at.replace("+0000 ", "") + " UTC");
if ( happyDate > time ) {
countPC++;
}
}
console.log( (100 - countPC) + '% more positive' );
} else {
// more people are happy
for ( var i in happyTweets ) {
var time = new Date(happyTweets[i].created_at.replace("+0000 ", "") + " UTC");
if ( sadDate > time ) {
countPC++;
}
}
console.log( (100 - countPC) + '% more negative' );
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment