-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyser.js
113 lines (104 loc) · 3.7 KB
/
analyser.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
var dumper = require("./dumper");
var fs = require("fs");
var indico = require("indico.io");
var HOURS_TO_MILLIS = 1000 * 60 * 60;
indico.apiKey = process.env.INDICO_KEY;
function mergeFBMsg(userData, data) {
var out = [];
var lastSender = null;
var userInitiated = data.length == 0? false: data[0].author == "fbid:" + userData.uids[0];
var cur = null;
for (var i = 0; i < data.length; i++) {
var m = data[i];
var newStart = i != 0 && (m.timestamp - data[i-1].timestamp) > 5*HOURS_TO_MILLIS;
if (m.author != lastSender || newStart) {
// new run
if (cur) {
cur.end = data[i-1].timestamp;
if (cur.body != '') out.push(cur);
}
if (newStart) {
out.push({"start": m.author == "fbid:" + userData.uids[0]? 0: 1, "end": 0, "body": m.author})
}
cur = {"start": m.timestamp, "body": m.body};
lastSender = m.author;
} else {
cur.body += "\n" + m.body;
}
}
if (cur) {
cur.end = data[i-1].timestamp;
out.push(cur);
}
return {
"data": out, "userInitiated": userInitiated
};
}
function sentimentFBMsg(userData, options, callback) {
var outdata = new Array(options.data.length);
sentimentFBMsgBatch(options, callback, outdata);
}
function sentimentFBMsgBatch(options, callback, outdata) {
// Batch file processing
var indata = options.data.map(function(a) {
return a.body.replace(/\?/g, ".").replace(/^([^:]+):\/\/([-\w._]+)(\/[-\w._]\?(.+)?)?$/ig, " ");
});
console.log("indico start " + new Date().getTime());
indico.sentimentHQ(indata).then(function(res) {
console.log("indico end " + new Date().getTime());
for (var i = 0; i < options.data.length; ++i) {
outdata[i] = [
options.data[i].start,
options.data[i].end,
res[i],
options.data[i].body
]
}
callback(null, outdata, options.userInitiated);
}).catch(function(res){
console.log(res);
callback(res, [], false);
});
}
// callback: (error, data, userInitiated)
function getMergedFBMsg(userData, limit, offset, callback) {
getMergedFBMsgImpl_(userData, limit, offset, callback, 2);
}
function getMergedFBMsgImpl_(userData, limit, offset, callback, tries) {
var newData = {
cookie: userData.Cookie,
conversation_id: userData.uids[1]
}
for (var i in userData) newData[i] = userData[i];
console.log("facebook start " + new Date().getTime());
dumper.dumpFBMsg(newData, limit, offset, function(fail, data) {
console.log("facebook end " + new Date().getTime());
if (fail) {
if (tries == 0) {
callback(fail, [], false);
} else {
console.log(fail);
getMergedFBMsgImpl_(userData, limit, offset, callback, tries - 1);
}
return;
}
var merged = mergeFBMsg(userData, data);
sentimentFBMsg(userData, merged, callback);
})
}
exports.getMergedFBMsg = getMergedFBMsg;
function testFromFile(customCallback) {
var buf = fs.readFileSync("out.json", {encoding: "UTF-8"});
var data = JSON.parse(buf);
var userData = {uids: ["100000145591601"]};
var merged = mergeFBMsg(userData, data);
//console.log(merged);
//return;
sentimentFBMsg(userData, merged, customCallback? customCallback: function(error, data, userInitiated) {
console.log(error);
console.log(data);
console.log(userInitiated);
});
}
exports.testFromFile = testFromFile;
//testFromFile();