forked from RobertDNelson/Peggy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (123 loc) · 3.54 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
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
var fs = require('fs');
var board;
if (process.argv.indexOf('live') >= 0) {
console.log("Yep, this is LIVE.");
board = require('./Outputters/Board.js');
} else {
console.log("Starting up in DEV mode.");
board = require('./Outputters/DevBoard.js');
}
var MessageRequest = require('./models/message_request.js');
var express = require('express');
var api = express();
var fork = require('child_process').fork;
var modules = {};
var util = require('util');
var request = require('request');
api.set('view engine', 'jade');
api.use(express.static('public'));
board.connect();
// reset the board
for (var i = 5; i >= 0; i--) {
board.clear(i);
};
// API
api.all('/peggy/write', function (req, res) {
if (!req.query.board || !req.query.x || !req.query.y || !req.query.text) {
res.send(500, {error:'invalid request'});
return;
}
res.send(200);
res.end();
var today = new Date();
var isPirateDay = today.getMonth() == 8 && today.getDate() == 19;
// console.log("isPirateDay: " + isPirateDay);
if (isPirateDay && req.query.board != 4) {
var text = req.query.text.replace(/\"/g, "");
request("http://isithackday.com/arrpi.php?format=json&text=" + encodeURIComponent(text), function(err, resp, body) {
if (!err && resp.statusCode == 200) {
body = body.replace(/\\{3}/g, "");
// console.log("body: " + body);
var jsonBody = JSON.parse(body);
// console.log("jsonBody.translation.pirate: " + jsonBody.translation.pirate);
var message = new MessageRequest(req.query.board, req.query.x, req.query.y, jsonBody.translation.pirate);
board.write(message);
} else {
var message = new MessageRequest(req.query.board, req.query.x, req.query.y, req.query.text);
board.write(message);
}
});
} else {
var message = new MessageRequest(req.query.board, req.query.x, req.query.y, req.query.text);
board.write(message);
}
});
api.all('/peggy/dev', function(req, res) {
res.render('dev', {});
});
api.all('/peggy/boardhtml', function(req, res) {
var boardNumber = parseInt(req.query.board);
res.send(board.getHtml(boardNumber));
res.end();
});
api.all('/peggy/clear', function(req, res) {
if (!req.query.board || !req.query.board > 5) {
res.send(500, {error:'invalid request'});
return;
}
var boardNumber = parseInt(req.query.board);
board.clear(boardNumber);
res.send(200);
res.end();
});
api.all('/peggy/off', function(req, res) {
board.turnOff();
res.send(200);
res.end();
});
api.all('/peggy/on', function(req, res) {
// reset the board
board.turnOn();
res.send(200);
res.end();
});
api.all('/peggy/status', function(req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(200, { powerOn: board.isOn(), date: Date.now() });
res.end();
});
api.all('/peggy/twitter', function(req, res) {
if (!req.query.q) {
res.send(500, {error:'invalid request'});
return;
}
modules['twitterBoard.js'].send({ term: req.query.q });
board.clear(2);
res.send(200);
res.end();
});
api.all('/peggy/discoText', function(req, res) {
if (!req.query.text) {
res.send(500, {error:'invalid request'});
return;
}
modules['discoText.js'].send({ text: req.query.text });
board.clear(1);
res.send(200);
res.end();
});
api.all('/peggy/discoText/off', function(req, res) {
modules['discoText.js'].send({ text: '' });
board.clear(1);
res.send(200);
res.end();
});
api.listen(8080);
// Launch any modules
fs.readdir('./Modules', function(err, files) {
files.forEach(function(file, index, array) {
if (file.match(/\.js$/)) {
modules[file] = fork('./Modules/' + file, []);
}
});
});