This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.js
192 lines (147 loc) · 4.51 KB
/
twitter.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
// ***************** TWITTER *****************
'use strict';
var Twit = require('twit');
var T = new Twit({
consumer_key: 'pWrTXzDGNOas9CPrhrFnA'
, consumer_secret: '0KHCWuqEd9A0O6wo4DU0eLASDUHgF3hUVxcKEANFrnY'
, access_token: '1534996477-JGs24bFNaqfgp1jlGv8plLivV4osuVqLsBY6WWt'
, access_token_secret: 'nyF8FDymyYXvrtxRfVlKOAHKRGuKZGIpTXmZAv5JJCkyD'
});
// **********************************************
var languagesWeAreLookingAt = [
'es',
'de',
'en',
'fr',
'ru',
'ja',
'it',
'ar',
'tr',
'pt'
];
var languageCodes = {
tr: 'turkish',
ru: 'russian',
pt: 'portuguese',
nl: 'dutch',
ja: 'japanese',
it: 'italian',
fr: 'french',
es: 'spanish',
en: 'english',
de: 'german',
ar: 'arabic'
};
/* ***************** DATABASE *****************
- this should be moved to its own file.
*/
var mongoose = require('mongoose');
var uristring =
process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/averageTwitter';
var Schema = mongoose.Schema;
mongoose.connect(uristring, function (err) {
if (err) {
console.log('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log('Succeeded connected to: ' + uristring);
}
});
var ResultSchema = new Schema({
dateCreated: { type: Date, default: Date.now, expires: 31 },
lang: String
});
var Results = mongoose.model('Result', ResultSchema);
function isALanguageWereInterestedIn(languageCode) {
var found = false;
for (var i = languagesWeAreLookingAt.length - 1; i >= 0; i--) {
if (languagesWeAreLookingAt[i] === languageCode) {
found = true;
break;
}
}
if (found) {
return true;
} else {
return false;
}
}
function addNewElement(newElement, callback) {
if (newElement !== 'und' &&
newElement && typeof
newElement !== 'undefined' &&
isALanguageWereInterestedIn(newElement)) {
isALanguageWereInterestedIn(newElement);
var result = new Results();
result.dateCreated = new Date();
result.lang = newElement;
result.save(function (e) {
if (e) {
console.log('error saving tweet');
} else {
if (typeof callback !== 'undefined') {
callback(newElement);
}
}
});
}
}
for (var i = languagesWeAreLookingAt.length - 1; i >= 0; i--) {
addNewElement(languagesWeAreLookingAt[i]);
}
// **********************************************
var getSummary = function (callback) { // -- this entire funtion needs to be more better muchly
Results.aggregate(
{'$group': { _id: '$lang', count: { $sum: 1}}},
{ $sort : { _id : 1} },
function (err, summary) {
if (err) {
console.log('couldnt get group summary', err);
return err;
} else {
console.log(summary);
}
if (summary) {
for (var l = summary.length - 1; l >= 0; l--) {
summary[l].lang = summary[l]._id;
delete summary[l]._id;
}
var total = 0.0;
for (var k = summary.length - 1; k >= 0; k--) {
total += summary[k].count;
}
var results = [];
for (var i = languagesWeAreLookingAt.length - 1; i >= 0; i--) {
var found = false;
for (var j = summary.length - 1; j >= 0; j--) {
if (summary[j].lang === languagesWeAreLookingAt[i]) {
results.push({
count: ((summary[j].count / total) * 100).toFixed(1),
lang: languageCodes[languagesWeAreLookingAt[i]],
code: languagesWeAreLookingAt[i]
});
found = true;
}
}
if (!found) {
results.push({count: 0, lang: languageCodes[languagesWeAreLookingAt[i]], code: languagesWeAreLookingAt[i]});
}
}
console.log(results);
callback(results);
} else {
console.log('no group summary');
return err;
}
});
};
var getTweets = function () {
var stream = T.stream('statuses/sample');
stream.on('tweet', function (tweet) {
addNewElement(tweet.lang);
});
};
module.exports.getTweets = getTweets;
module.exports.getSummary = getSummary;