-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (137 loc) · 4.06 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
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
var Twit = require('twit')
var fs = require("fs");
var Color = require("color")
var express = require('express');
var app = express();
console.log("\n *START* \n");
/* ==========================================
* Temporary data holder
* ========================================== */
var data = [
{
id: '1A1',
color: Color("blue")
},
{
id: '1B1',
color: Color("red")
}
]
/* ==========================================
* Twitter parser
* ========================================== */
var config = JSON.parse(fs.readFileSync("oauth.json"));
var Twit = new Twit(config)
//
// Filter the twitter public stream by the hashtag '#spectrumuc'.
//
var stream = Twit.stream('statuses/filter', { track: '#spectrumuc' })
// Register a callback for each streamed tweet
stream.on('tweet', newTweet);
function newTweet(tweet) {
var res = tweet.text.split(" ");
var submission = {
id: undefined,
color: undefined
};
// check color is a valid CSS color
try {
submission.color = Color(res[2]);
} catch (exception) {
// error parsing color so we're done with this
console.log("error parsing color, not a color");
return;
}
// Get entry for that id
var entry = data.find(function(element, index, array) {
if(res[1].toUpperCase() === element.id){
return true
}
return false;
});
if (entry) {
// if there is an entry for that entry, modify it
entry.color = submission.color;
console.log("Updated: " + JSON.stringify(entry));
} else {
// id doesn't exist so we're done here
console.log("error validating id, not a registered id");
return;
}
}
/* ==========================================
* GET api/:id/color requests
* ========================================== */
app.get('/api/:id/color', function (req, res) {
// Todo build in a time limit so we don't get DDOSed as easily
// Get entry with that id
var entry = data.find(function(element, index, array) {
if(req.params.id.toUpperCase() === element.id){
return true
}
return false;
});
if (entry){
// Return the id and color
res.end(JSON.stringify(entry));
} else {
// Error the response
res.status(404);
res.end("four OH four");
}
});
/* ==========================================
* GET /:id/color requests
* This just gives back a colored webpage
* ========================================== */
app.get('/:id/color', function (req, res) {
// Todo build in a time limit so we don't get DDOSed as easily
// Get entry with that id
var entry = data.find(function(element, index, array) {
if(req.params.id.toUpperCase() === element.id){
return true
}
return false;
});
if (entry){
var html = `
<html>
<head>
<style>
html {
background-color:` + entry.color.hexString() + `;
color: lightsteelblue;
font-family: "Helvertica Neue", sans-serif;
}
</style>
<title>SpectrumUC:</title>
</head>
<body><h1>SpectrumUC: ` + entry.id + `</h1></body>
</html>`
// Return the id and color
res.end(html);
} else {
// Error the response
res.status(404);
res.end("four OH four");
}
})
/* ==========================================
* I'm a teapot
* ========================================== */
app.get('/brew', function (req, res) {
res.status('418');
res.end("I'm a teapot");
});
/* ==========================================
* Return 404 for everything else
* ========================================== */
app.get('/*', function (req, res) {
res.status('404');
res.end("four OH four");
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("APP Listening on http://%s:%s", host, port)
});