-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (87 loc) · 3.36 KB
/
index.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
require('dotenv').config({silent: true});
if (! process.env.FIREBASE_AUTH_TOKEN) {
console.log('You must supply FIREBASE_AUTH_TOKEN to run this script.');
process.exit(1);
}
const Firebase = require('firebase');
const fbRef = new Firebase('https://nfl-liveupdate.firebaseIO.com/');
const Twitter = require('twitter');
const twitterClient = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
var childData = {};
fbRef.authWithCustomToken(process.env.FIREBASE_AUTH_TOKEN, function(err, res) {
if (err) {
console.log(err);
process.exit(1);
} else {
console.log('Listening for updates...');
fbRef.on('child_removed', function(childSnap) {
delete childData[childSnap.key()];
});
fbRef.on('child_added', function(childSnap) {
childData[childSnap.key()] = {};
childSnap.ref().on('value', function(snap) {
var val = snap.val();
var oldVal = childData[snap.key()];
if (! val || ! oldVal) return;
childData[snap.key()] = val;
var status = '';
var quarterStatus = '';
var justScoredStatus = '';
var scoreDiff = 0;
var scoreStr = val.away_team + ' ' + val.away_score
+ ', ' + val.home_team + ' ' + val.home_score;
var statusHeader = '#NFL LiveUpdate:\n';
if ('quarter' in oldVal
&& oldVal.quarter != 'F'
&& val.quarter != oldVal.quarter
) {
switch (val.quarter) {
case '1': quarterStatus = 'The ' + val.away_team + ' vs ' + val.home_team
+ ' game is now underway!'; scoreStr = ''; break;
case '2': quarterStatus = 'At the end of 1, the score is '; break;
case 'H': quarterStatus = 'At halftime, the score is '; break;
case '4': quarterStatus = 'After 3, the score is '; break;
case 'O': quarterStatus = 'At the end of regulation, the score is '; break;
case 'F': quarterStatus = 'The final score is '; break;
}
if (quarterStatus) {
quarterStatus += scoreStr;
}
}
if ('away_score' in oldVal
&& 'home_score' in oldVal
) {
if (val.away_score > oldVal.away_score) {
scoreDiff = val.away_score - oldVal.away_score;
justScoredStatus = val.away_team + ' just scored ' + scoreDiff + '!\n';
} else if (val.home_score > oldVal.home_score) {
scoreDiff = val.home_score - oldVal.home_score;
justScoredStatus = val.home_team + ' just scored ' + scoreDiff + '!\n';
}
}
if (justScoredStatus || quarterStatus) {
status = statusHeader;
if (justScoredStatus && quarterStatus) {
status += justScoredStatus + quarterStatus;
} else if (justScoredStatus) {
status += justScoredStatus + 'The new score is ' + scoreStr;
} else if (quarterStatus) {
status += quarterStatus;
}
twitterClient.post('statuses/update', {status: status}, function(error, tweet){
if (error) {
console.log(error);
} else {
console.log(tweet);
}
});
}
});
});
}
});