/
comparebrands.js
270 lines (219 loc) · 7.35 KB
/
comparebrands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//Set up communication with web page
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
messages = [],
sockets = []
status = 'Waiting';
app.use( express.static(__dirname + '/public'));
server.listen(3333);
// Include the library
var DataSift = require('datasift-node');
var JQuery = require('jquery');
//include configuration file
var config = require('./config/config')
//Define brand and tags to filter for
var brand1 = 'Adidas'
var tag1 = 'allin'
var brand2 = 'Nike'
var tag2 = 'justdoit'
var filter = 'tag.brand "' + brand1 + '" {interaction.content contains "' + brand1 + '" OR interaction.hashtags contains "' + tag1 + '" OR interaction.mentions in "' + brand1 +'"} \
tag.brand "' + brand2 + '" {interaction.content contains_any "' + brand2 + '" OR interaction.hashtags contains "' + tag2 + '" OR interaction.mentions in "' + brand2 +'"} \
\
return { \
(interaction.content contains_any "' + brand1 + ', ' + brand2 + '" OR interaction.hashtags contains_any "' + tag1 + ', ' + tag2 + '" OR interaction.mentions in "' + brand1 + ', ' + brand2 + '") AND language.tag contains "en" \
}';
var dataSiftUserName = 'sam';//config.dsUser
var dataSiftToken = config.dsToken;
// Create a client
var ds = new DataSift(dataSiftUserName, dataSiftToken);
var count = 0;
var max = 20;
var info =[]
var adidas = 0;
var nike = 0;
var total = 0;
var newYork = 0;
var losAngeles = 0;
var chicago = 0;
var otherLocation = 0;
//ny geo[0], la geo[1], chicago geo[2], other geo[3]
var geoAdidas = [];
var geoNike = [];
var ccount = 0;
for (i=0; i<=3; i++){
geoAdidas[i]=0;
geoNike[i]=0;
}
// Compiles a stream from a CSDL definition:
function compileFilter(csdl) {
ds.compile({ 'csdl': csdl }, function (err, response) {
if (err)
console.log(err);
else
{
console.log("Filter hash: " + response.hash);
connect(response.hash); // Connects to DataSift
}
});
}
// 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();
}
io.sockets.on('connection', function (socket) {
sockets.push(socket);
socket.emit('messages-available', messages);
socket.on('add-message', function (data) {
messages.push(data);
count = 0;
max = data.message;
brand1 = data.b1;
brand2 = data.b2;
tag1 = data.b1ht;
tag2 = data.b2ht;
if(data.dsName==1)
{dataSiftUserName = config.dsUser;}
else
{dataSiftUserName = data.dsName;}
if(data.dsToken==1)
{dataSiftToken = config.dsToken;}
else
{dataSiftToken = data.dsToken;}
filter = 'tag.brand "' + brand1 + '" {interaction.content contains "' + brand1 + '" OR interaction.hashtags contains "' + tag1 + '"} \
tag.brand "' + brand2 + '" {interaction.content contains_any "' + brand2 + '" OR interaction.hashtags contains "' + tag2 + '"} \
\
return { \
(interaction.content contains_any "' + brand1 + ', ' + brand2 + '" OR interaction.hashtags contains_any "' + tag1 + ', ' + tag2 + '") AND language.tag contains "en" \
}';
ds = new DataSift(dataSiftUserName, dataSiftToken);
console.log('there was a new message. max is now ' + max);
console.log(max*3)
compileFilter(filter);
ccount++;
console.log('loop:' + ccount)
sockets.forEach(function (socket) {
socket.emit('message-added', data);
});
});
});
// Initiate our script by comiling the filter, which in turn will connect us to DataSift
//compileFilter(filter);
//send data to database
var dbURL = config.couchURL;
var dbPort = config.couchPort;
var dbAuth = config.couchAuth;
var dbUser = config.couchUser;
var dbPassword = config.couchPassword;
var dbDatabase = config.dbname;
var cradle = require('cradle');
var db = new(cradle.Connection)(dbURL, dbPort,
{auth: {username: dbUser, password: dbPassword}}).database(dbDatabase);
function sendRecord(singleItem){
db.save(singleItem,
function (err, res) {
if (err) {
// Handle error
console.log(' SAVE ERROR: Could not save record!!');
}
else {
console.log(' Sucessful Save');
}
});
}
function sendData(dataArray){
for (i = 0; i < max; i++){
db.save(info[i],
function (err, res) {
if (err) {
// Handle error
console.log(' SAVE ERROR: Could not save record!!');
} else {
// Handle success
console.log(' SUCESSFUL SAVE');
}
})};
}
function javascript_abort()
{
throw new Error('You successfully saved ' + count + ' records. Goodbye');
}
function broadcast_info(broadcast_name, broadcast_data)
{
io.sockets.emit(broadcast_name, broadcast_data)
}