-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (98 loc) · 2.42 KB
/
app.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 express = require('express');
var app = express();
var path = require('path');
var Twit = require('twit');
var Galileo = require("galileo-io");
var board = new Galileo();
var pin = 9;
var trend = "GalileoGirls"
var boardAvailable = true;
var currentTweets = [];
var latestId = 0;
var T = new Twit({
consumer_key: ''
, consumer_secret: ''
, access_token: ''
, access_token_secret: ''
})
app.use(express.static(path.join(__dirname, 'public')));
var server = app.listen(80, function() {
console.log('Listening on port %d', server.address().port);
});
app.get('/tweets', function(req, res){
res.send(currentTweets);
});
function setupBoard() {
if(boardAvailable) {
board.on("ready", function() {
this.pinMode(pin, this.MODES.OUTPUT);
queryTwitter(trend, pin);
});
} else {
queryTwitter(trend, pin);
}
}
function queryTwitter(trendMessage, pin) {
console.log("Processing trend " + trendMessage + " " +new Date());
if(trendMessage) {
T.get('search/tweets', { q: trendMessage, count: 100, since_id: latestId }, function(err, data) {
if(err) {
console.log(err);
setTimeout(function () {
queryTwitter(trend, pin)
}, 5000);
}
else if(data.statuses && data.statuses.length > 0) {
getTweetList(data.statuses)
processTrend(currentTweets, pin);
}
});
}
}
function processTweetData(tweetData) {
var tweet = {};
tweet.created = tweetData.created_at;
tweet.message = tweetData.text;
tweet.user = tweetData.user.screen_name;
tweet.profile = tweetData.user.profile_image_url;
tweet.id = parseInt(tweetData.id_str);
return tweet;
}
function getTweetList(tweetList) {
currentTweets = [];
for(var i = 0; i < tweetList.length; i++) {
var tweet = processTweetData(tweetList[i]);
currentTweets.push(tweet);
setHighestId(tweet.id);
}
}
function processTrend(tweeters, pin) {
if(tweeters.length > 1) {
executePin(pin);
}
setTimeout(function () {
queryTwitter(trend, pin)
}, 50000);
}
function executePin(pin) {
if(boardAvailable) {
setTimeout(function () {
flash(pin, 0, 0)
}, 300);
}
}
function flash(pin, increment, value) {
console.log(value + " " + increment);
board.digitalWrite(pin, value);
if(increment < 50) {
setTimeout(function() {
increment += 1;
flash(pin, increment, (value ^= 1));
}, 500);
}
}
function setHighestId(currentId) {
if(currentId > latestId)
latestId = currentId;
}
setupBoard();