-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
113 lines (92 loc) · 3.11 KB
/
main.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 Twit = require('twit'),
t = new Twit( require('./config.json')),
express = require('express'),
app = express(),
server = require('http').createServer(app),
morgan = require('morgan'),
_ = require('lodash');
/************************************************************************************************************************
* Server Setup
************************************************************************************************************************/
var port = process.env.PORT || 3456;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
app.use(morgan('dev'));
app.use(express.static(__dirname + '/public'));
app.use(function logErrors (err, req, res, next) {
console.error(err.stack);
next(err);
});
app.use(function clientErrorHandler (err, req, res, next) {
if (req.xhr) {
res.status(500).send({ error: 'Something blew up!' });
} else {
next(err);
}
});
app.use(function errorHandler (err, req, res, next) {
res.status(500);
res.render('error', { error: err });
});
/************************************************************************************************************************
* Loading config
************************************************************************************************************************/
/*config.wall = _.map(config.wall, function(hash) {
return hash.toLowerCase();
});
config.battle = _.map(config.battle, function(hash) {
return hash.toLowerCase();
});
var tracking = _.union(config.wall, config.battle),
battleCount = _.zipObject(config.battle, _.range(0, config.battle.length, 0));
console.log(battleCount);
_.forEach(tracking, function(hash) {
t.track(hash);
});*/
/************************************************************************************************************************
* App
************************************************************************************************************************/
app.get('/', function (req, res) {
var word = {
pizza: 0,
banana: 0
};
var query = '';
_.forEach(word, function(count, hash) {
if(query) {
query += ' OR ';
}
query += hash
});
t.get('search/tweets', { q: query, count: 100 }, function(err, data, response) {
if(err) {
res.send('Error');
return;
}
var sheet = [];
var head = [];
head.push('word');
head.push("push");
sheet.push(head);
for (var i = 0; i < data.statuses.length; i++)
{
var tweet = data.statuses[i];
var lowText = tweet.text.toLowerCase();
_.forEach(word, function(count, hash) {
if(lowText.indexOf(hash) !== -1) {
word[hash] += 1;
}
});
}
_.forEach(word, function(count, hash) {
var row = [];
row.push(hash);
row.push(count);
sheet.push(row);
});
var all = [];
all.push(sheet);
res.send(JSON.stringify(all));
});
});