Last active
March 2, 2017 23:04
-
-
Save tigarcia/6c78510bba0e117c1560a912c971782b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install --save twitter dotenv sentiment | |
require('dotenv').config(); | |
const Twitter = require('twitter'); | |
const sentiment = require('sentiment'); | |
const rpio = require('rpio'); | |
const Promise = require('bluebird'); | |
function delay(ms) { | |
var deferred = Promise.pending(); | |
setTimeout(function() { deferred.resolve(); }, ms); | |
return deferred.promise; | |
} | |
const red = 15; | |
const green = 11; | |
rpio.open(red, rpio.OUTPUT, rpio.LOW); | |
rpio.open(green, rpio.OUTPUT, rpio.LOW); | |
const client = new Twitter({ | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY, | |
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET | |
}); | |
var tweets = []; | |
var busy = false; | |
client.stream('statuses/filter', {track: 'donald trump'}, function(stream) { | |
stream.on('data', function(event) { | |
// console.log(event && event.text); | |
if (event) { | |
tweets.push(event.text); | |
if (tweets.length > 5 && !busy) { | |
busy = true; | |
var avg = tweets.reduce((acc, tweet) => { | |
return acc + sentiment(tweet).score; | |
}, 0) / tweets.length; | |
console.log("Sentiment: ", avg); | |
console.log(JSON.stringify(tweets)); | |
var pin = red; | |
if (avg >= 0) { | |
pin = green; | |
} | |
tweets = []; | |
rpio.open(pin, rpio.OUTPUT, rpio.HIGH); | |
delay(2000) | |
.then(() => { | |
rpio.open(pin, rpio.OUTPUT, rpio.LOW); | |
return delay(500); | |
}) | |
.then(() => busy = false); | |
} | |
} | |
}); | |
stream.on('error', function(error) { | |
throw error; | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment