// ## Declare Methods // Connects to DataSift and starts streaming data: function connect(hash) { // Set up a 'connect' event handler, which will fire when a connection is established. When connected we compile our CSDL filter and subscribe to streaming data. ds.on('connect', function () { console.log('Connected to DataSift'); subscribe(filter); }); // Set up 'error' handler to alert us of any errors. For more details on possible errors see [http://dev.datasift.com/docs/resources/errors](http://dev.datasift.com/docs/resources/errors). ds.on('error', function (error) { console.log('ERROR: ' + error); }); // Set up 'delete' handler. Depending on the data sources you are using, you may need to delete this data to stay compliant. ds.on('delete', function (data) { console.log('Data deleted: ', data); // TODO: Do something useful with the data! }); // Set up 'interaction' handler - this receives our data! This is triggered each time we receive a new interaction - a piece of data on the live stream. ds.on('interaction', function (data) { console.log('Recieved data: ', data); }); // Now all handlers are set up, connect to DataSift! ds.connect(); }
// Connects to DataSift and starts streaming data: function connect(hash) { // Set up a 'connect' event handler, which will fire when a connection is established. When connected we compile our CSDL filter and subscribe to streaming data. ds.on('connect', function () { console.log('Connected to DataSift'); console.log('loop:' + ccount) ds.subscribe(hash); }); // Set up 'error' handler to alert us of any errors. For more details on possible errors see [http://dev.datasift.com/docs/resources/errors](http://dev.datasift.com/docs/resources/errors). ds.on('error', function (error) { console.log('ERROR: ' + error); }); // Set up 'delete' handler. Depending on the data sources you are using, you may need to delete this data to stay compliant. ds.on('delete', function (data) { console.log('Data deleted: ', data); // TODO: Do something useful with the data! }); // Set up 'interaction' handler - this receives our data! This is triggered each time we receive a new interaction - a piece of data on the live stream. ds.on('interaction', function (data) { if(count<max) { status = 'Demo running...'; } else { status = 'Demo finished'; } console.log(status); //console.log('Recieved data: ' + count, data); console.log('Recieved data: ' + count, data.hash); //info[count]=data; console.log('sending record' + count + 'to database') //try this var displayData = JSON.stringify(data); //console.log("This is the data: " + displayData); var dataRead = JSON.parse(displayData); console.log("This came from: " + dataRead.data.interaction.tag_tree.brand); sendRecord(data); count = count + 1; var brandTemp = dataRead.data.interaction.tag_tree.brand; //var locationTemp = dataRead.data.interaction.tag_tree.location; var authorTemp = dataRead.data.interaction.author.username; var textTemp = dataRead.data.interaction.content; var serviceTemp = dataRead.data.interaction.type //count brand mentions if (brandTemp == brand1){ adidas++; total++; } if (brandTemp == brand2){ nike++; total++; } console.log(brand1 +': ' + adidas + ' | ' + brand2 + ': ' + nike) if (count>=max){ ds.unsubscribe(hash); console.log('unsubcribed'); status = 'Demo finished: ' + max + ' items'; console.log(status) //javascript_abort(); } //send nike and adidas values to browser io.sockets.volatile.emit('tweet', { service: serviceTemp, user: authorTemp, text: textTemp, adidasPercent: (adidas/total)*100, nikePercent: (nike/total)*100, adidasTotal: adidas, nikeTotal: nike, totalTotal: total, jobStatus: status, webBrand1: brand1, webBrand2: brand2 }); }); // Now all handlers are set up, connect to DataSift! ds.connect(); }