Back to Parent

Node.js code to analyze Twitter climate and publish Particle event
// The initial code (which was then modified after line 35)

// came from:

// https://github.com/heroku/node-js-getting-started.git


var express = require('express');
var app = express();
var http = require('http');
var querystring = require('querystring');
var request = require('request');
var Particle = require('particle-api-js');
var particle = new Particle();

// Global Variables

var token;
var numPositive = 0;
var numNegative = 0;
var numNeutral = 0;
var total = 0;


app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/', function(request, response) {
  response.render('pages/index');
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

var nconf = require('nconf');
var Twit = require('twit');
var _ = require('lodash');

// Make sure to have a separate file in the working

// directory called 'config.json' containing info

// about the Twitter consumer key, etc

nconf.file({ file: 'config.json' }).env();

var T = new Twit({
  consumer_key: nconf.get('TWITTER_CONSUMER_KEY'),
  consumer_secret: nconf.get('TWITTER_CONSUMER_SECRET'),
  access_token: nconf.get('TWITTER_ACCESS_TOKEN'),
  access_token_secret: nconf.get('TWITTER_ACCESS_TOKEN_SECRET')
});

// Every 30 minutes, pull statuses from the list of people

// a specific twitter handle follows, and then analyze

// sentiment of those statuses and calculate which type of

// status, positive, neutral, or negative, occurs the most

setInterval(function(){
  var options = {
    screen_name: <twitter handle>,   // Can replace with any twitter handle

    skip_status: false,
    include_user_entities: false
  };

  T.get('friends/list', options, function(err, data) {
    for (var i = 0; i < (data.users).length; i++) {
    total = 0;
    numPositive = 0;
    numNegative = 0;
    numNeutral = 0;
    console.log(data.users[i].screen_name);
    console.log(data.users[i].status.text);
    request.post({url:'http://text-processing.com/api/sentiment/', form:{text:data.users[i].status.text}}, function(error, response, body) {
	    if (!error && response.statusCode == 200) {
		total += 1;
		console.log(body);
		var json = JSON.parse(body);
		console.log(json.label);
		if((json.label).localeCompare('pos') == 0)
		{
		    numPositive += 1;
		}
		if((json.label).localeCompare('neg') == 0)
		{
		    numNegative += 1;
		}
		if((json.label).localeCompare('neutral') == 0)
		{
		    numNeutral += 1;

		}
		console.log(total);
		console.log(numPositive/total);
		console.log(numNegative/total);
		console.log(numNeutral/total);
	    }});
  };
});
}, 1800000);

// Every 30 minutes publish an event to Particle cloud 

setInterval(function(){
particle.login({username: <insert username and email here>, password: <insert password here> }).then(
  function(data) {
    token = data.body.access_token;
    console.log('Logged in successfully\n');
    var result;
    if(numPositive > numNegative){
	result = "pos";
    } else {
	if(numNegative > numPositive){
	  result = "neg";
	} else {
	  result = "neut";
	}
    }
    result = "neg";
    var publishEventPr = particle.publishEvent({name: 'twitter', data:result, auth:token});

    publishEventPr.then(
      function(data) {
        if(data.body.ok) {console.log('Event published successfull')}},
      function(err) {
        console.log('Failed to publish event: ' + err)
       }
     );
  },
  function (err) {
    console.log('Could not log in.', err);
  }
);
}, 1800000);
Monisha Gopal (2017) Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0