-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
137 lines (121 loc) · 3.79 KB
/
engine.js
File metadata and controls
137 lines (121 loc) · 3.79 KB
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
var twitter = require("mtwitter");
var sentiment = require('sentiment');
var Parse = require('parse').Parse;
var location = "";
mTwit = new twitter({
consumer_key: 'RG7hca16EGx7OtAQ8JXRfJ5I8',
consumer_secret: 'f8xIE16p4ol2AZhxGwITV3rdtQ1l2JifGOV1IhDs6zwRJJlV19',
access_token_key: '635348000-17rt0Ycpij1s20Zf9QWN9ASBgf5YK4O6zl9fl4I8',
access_token_secret: 'tmfQCg6NxC7JnnpLdh6H1vqb63dUos6xk00jS5zjY6sRX'
});
function setLocation(loc){
location = loc;
}
function getExistingSentimentData(res, day, callback, pretty){
// Create the Parse object.
var SentimentIndex = Parse.Object.extend("SentimentIndex");
var sentimentIndex = new SentimentIndex();
var saveRequired = true;
//query to see if todays index has already been calculated
var sentimentQuery = new Parse.Query(SentimentIndex);
sentimentQuery.equalTo("day", day);
sentimentQuery.include("objectId");
sentimentQuery.find({
success: function(existingEntry) {
if(existingEntry && existingEntry.length > 0){
console.log("EXISTING DAY IN PARSE: " + existingEntry[0]["id"]);
sentimentQuery.get(existingEntry[0]["id"], {
success: function(existingIndex) {
sentimentIndex = existingEntry[0];
saveRequired = false;
},
error: function(object, error) {
res.send(JSON.stringify(error));
}
});
}else{
sentimentIndex.set("day", day);
}
callback(res, day, sentimentIndex, saveRequired, pretty);
},
error: function(error) {
console.log("Error: " + error.code + " " + error.message);
res.send("Error: " + error.code + " " + error.message);
}
});
}
function getDaysRecentTweetSentiment(res, day, sentimentIndex, saveRequired, pretty){
//scrape twitter data, take a sample, and map reduce the values into a sentiment index.
mTwit.get('search/tweets',
{
q: "",
geocode: location,
count: "100",
result_type:"recent"
},
function(err, item) {
if(err){
console.warn("failed scrape: "+err);
console.log(item);
return;
}
//cull the twitter response into simple pairs.
var tweets = item.statuses.map(function(status){
return { id : status["id_str"] , text: status["text"] };
});
//take a sample of the tweets for the day,
var sample = {};
sample["tweets"] = tweets.slice(0, 4);
if(saveRequired){
//save the sample up to Parse
var sampleCount = sample.tweets.length;
for (var i = 0; i < sampleCount; i++) {
var TweetPair = Parse.Object.extend("TweetPair");
var tweetPair = new TweetPair();
tweetPair.set("userID", sample["tweets"][i].id);
tweetPair.set("text", sample["tweets"][i].text);
tweetPair.save();
sentimentIndex.add("Pairs", tweetPair);
}
}
var indexes = tweets.map(function(tweet){
var tweetSentiment = sentiment(tweet["text"]);
var score = tweetSentiment["score"];
console.log(score);
return score;
});
var indexCount = indexes.length;
var sum = 0;
for (var i = 0; i < indexCount; i++) {
sum += indexes[i];
}
console.log("RAW: "+sum);
sum = (sum / indexCount);
console.log("AVG: "+sum);
sum = ((sum +10)/20)*100;
console.log("%: "+sum);
//save the sentiment index to parse
sentimentIndex.set("sentimentIndex", sum);
sentimentIndex.save(null, {
success: function(result) {
//add the sentiment to the response json
if(pretty){
res.render('index',
{ index : sum,
date: day,
tweets: sample["tweets"] });
}else{
sample["sentimentIndex"] = sum;
res.contentType('application/json');
res.send(JSON.stringify(sample));
}
},
error: function(result, error) {
res.send(JSON.stringify(error));
}
});
});
}
exports.calculateTwitterSentiment = getDaysRecentTweetSentiment;
exports.getTwitterSentiment = getExistingSentimentData;
exports.setLocation = setLocation;