-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathapp.js
244 lines (213 loc) · 6.71 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env node
// built-in requirements
const fs = require("fs");
const path = require("path");
// external dependencies
const express = require("express");
const request = require("request");
const Log = require("log");
const log = new Log("info");
// construct express app
const app = express();
const defaultGame = "fortnite";
const supportedGames = {
"fortnite": "Fortnite",
"pubg": "PLAYERUNKNOWN'S+BATTLEGROUNDS",
"blackout": "Call%20of%20Duty%3A%20Black%20Ops%204",
};
// setup global list
const defaultStream = {
"stream_name": "foo",
"alive": 100,
"updated": (new Date()).toJSON(),
"stream_url": "https://example.com",
};
const currentStream = {};
const allStreams = {};
Object.keys(supportedGames).forEach(function(game) {
allStreams[game] = [defaultStream];
currentStream[game] = [defaultStream];
});
/**
* Ensures given filesystem directory if it does not exist.
* @param {string} dirPath - Relative or absolute path to
* desired directory.
*/
function ensureDir(dirPath) {
const parts = dirPath.split(path.sep);
const mkdirSync = function(dirPath) {
try {
fs.mkdirSync(dirPath);
} catch (err) {
if (err.code !== "EEXIST") throw err;
}
};
for (let i = 1; i <= parts.length; i++) {
mkdirSync(path.join.apply(null, parts.slice(0, i)));
}
}
/**
* Gets list of PUBG streams from Twitch API v5.
* @callback {object} body - JSON object from Twitch API call. Contains list
* of English language PUBG streams and their associated metadata.
* @param {string} game - The game to pull streams for.
* @param {requestCallback} callback - The callback that handles the response.
*/
function listStreams(game, callback) {
const clientID = process.env.clientID;
let allowlist = process.env.ROTISSERIE_ALLOWLIST;
let blocklist = process.env.ROTISSERIE_BLOCKLIST;
const gameURL = "https://api.twitch.tv/kraken/streams?game="+supportedGames[game]+"&language=en&stream_type=live&limit=50";
const options = {
url: gameURL,
headers: {
"Client-ID": clientID,
"Accept": "application/vnd.twitchtv.v5+json",
},
};
request(options, function(error, response, body) {
if (body === undefined || body === null) {
log.error("No response from Twitch.");
if (error) {
log.error(" " + error);
}
return Array([]);
}
if (error) log.error("error:", error);
log.info("statusCode:", response.statusCode);
bodyJSON = JSON.parse(body);
allAgesStreams = bodyJSON.streams.filter(function(d) {
return d.channel.mature === false;
});
if (allowlist !== null && allowlist !== undefined) {
allowlist = allowlist.split(" ");
allAgesStreams = allAgesStreams.filter(function(d) {
return allowlist.includes(d.channel.name);
});
}
if (blocklist !== null && blocklist !== undefined) {
blocklist = blocklist.split(" ");
allAgesStreams = allAgesStreams.filter(function(d) {
return !blocklist.includes(d.channel.name);
});
}
usernameList = allAgesStreams.map(function(d) {
return d.channel["display_name"];
});
log.info(usernameList);
return callback(usernameList);
});
}
/**
* Runner for listing streams and firing up a worker for each of those streams
* to handle the stream processing.
* @param {string} game - The game to pull streams for.
* @param {string} cropsDir - path to directory containing cropped thumbnails
* containing the number of players alive.
*/
function updateStreamsList(game, cropsDir) {
// get list of twitch streams and record each one
listStreams(game, function(response) {
const streamsList = response;
log.info("Got " + streamsList.length + " streams for " + game);
const array = [];
const newAllStreams = [];
for (const stream in streamsList) {
const streamName = streamsList[stream];
formData = {
"stream": streamName,
};
const requestOptions = {
url: "http://" + process.env.ROTISSERIE_OCR_SERVICE_HOST + ":" + process.env.ROTISSERIE_OCR_SERVICE_PORT + "/process_" + game,
form: formData,
};
request.post(requestOptions, function(err, httpResponse, body) {
if (err) {
console.error("upload failed");
} else {
const parsed = JSON.parse(body);
const object = {};
object.name = streamName;
object.alive = parsed.number;
array.push(object);
}
});
}
setTimeout(function() {
array.sort(function(a, b) {
return a.alive - b.alive;
});
if (array.length > 0) {
log.info(array);
log.info("lowest stream: " + array[0].name);
currentStream[game] = streamToObject(array[0]);
for (const idx in array) {
newAllStreams.push(streamToObject(array[idx]));
}
allStreams[game] = newAllStreams;
} else {
log.error("Empty array, not switching");
}
}, 25000);
});
}
/**
Sets webpage to stream with lowest number of players alive, determined by
* getLowestStream.
* @param {object} stream - object containing name of string and number of
* players alive.
* @return {object} object - stream object containing stream metadata.
*/
function streamToObject(stream) {
object = {};
object["stream_name"] = stream.name;
object["alive"] = stream.alive;
object["stream_url"] = "https://player.twitch.tv/?channel=" + stream.name;
object["updated"] = (new Date()).toJSON();
return object;
}
/**
* Runs logic to get lowest stream and starts express app server.
*/
function main() {
const clipsDir = "./streams/clips/";
const thumbnailsDir = "./streams/thumbnails/";
const cropsDir = "./streams/crops/";
ensureDir(clipsDir);
ensureDir(thumbnailsDir);
ensureDir(cropsDir);
Object.keys(supportedGames).forEach(function(game) {
// continue searching for lowest stream every 30 seconds.
updateStreamsList(game, cropsDir);
setInterval(function() {
updateStreamsList(game, cropsDir);
}, 30000);
});
app.set("view engine", "html");
app.engine("html", require("hbs").__express);
// serve index.html
app.get("/:game?", function(req, res) {
game = req.params.game;
log.info(game);
if (!game) {
game = defaultGame;
}
res.render("index", {"game": game});
});
// serve current leader stream object
app.get("/current/:game", function(req, res) {
game = req.params.game;
res.json(currentStream[game]);
});
// serve all stream objects
app.get("/all/:game", function(req, res) {
game = req.params.game;
res.json(allStreams[game]);
});
app.use("/static", express.static("public"));
// start http server and log success
app.listen(3000, function() {
log.info("Example app listening on port 3000!");
});
}
main();