-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
418 lines (389 loc) · 10.4 KB
/
index.ts
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import * as express from "express";
import * as express_graphql from "express-graphql";
import { createWriteStream, existsSync, mkdir, readFileSync, writeFileSync } from "fs";
import { createServer } from "http";
import { format, join, parse, resolve } from "path";
import * as request from "request";
import { CueSheet } from "./helpers/cue_sheet";
import { createCompliantCueSheets, splitCueSheet } from "./helpers/recording_processor";
import { getPastRecordings, getRecordedSongs, getRecordingCover, update_reader } from "./helpers/recording_reader";
import { SCHEMA } from "./helpers/schema";
import { format_seconds, log_error, print, resolve_after_get } from "./helpers/shared_functions";
import {
IApiObject,
IServerObject,
IUpdateDataObject,
} from "./helpers/types";
const sane_fs = require("sanitize-filename");
const cors = require("cors");
let app: any; // express app
let http_server: any;
let polling_interval_id: any;
let api_uri: string = "https://r-a-d.io/api";
let server_uri: string = "https://stream.r-a-d.io/status-json.xsl";
let stream_uri: string = "https://relay0.r-a-d.io/main.mp3";
let poll_interval: number = 5000;
let server: IServerObject = {
audio_format: "",
bitrate: 0,
sample_rate: 0,
server_description: "",
server_name: "",
};
let api: IApiObject = {
current_time: 0,
dj_name: "",
dj_pic: "",
end_time: 0,
listeners: 0,
lp: [],
np: "",
start_time: 0,
};
let current_song: number = 1;
let stream_request: any;
let raw_data_folder: string = "";
let export_folder: string = join(".", "recordings_folder");
let rec_start: number | null;
let force_stop: boolean = true;
let last_rec: boolean = false;
let excluded_djs: string[] = ["Hanyuu-sama"];
const split_character: string = " - ";
let config_file = "config.json";
let auto_save = false;
let cueSheet: CueSheet;
let cueSplit = false;
function save_config() {
const config = {
config: {
api_uri,
server_uri,
stream_uri,
poll_interval,
excluded_djs,
export_folder,
},
};
writeFileSync(config_file, JSON.stringify(config));
}
function song_change() {
let start = 0;
if (rec_start) {
start = api.start_time - rec_start;
start = Math.max(0, start);
}
if (api.np.split(split_character).length === 2) {
cueSheet.add_song(api.np.split(split_character)[1], start, api.np.split(split_character)[0]);
} else {
cueSheet.add_song(api.np, start);
}
print(current_song + ". " + sane_fs(api.np) + " ::" + format_seconds(start) + "::");
current_song += 1;
}
function get_dj_pic(dj_folder: string) {
const dj_pic_url = api_uri + "/dj-image/" + api.dj_pic;
const dot_split = api.dj_pic.split(".");
const recover_path = format({
base: `cover.${dot_split[dot_split.length - 1]}`,
dir: dj_folder,
});
request(dj_pic_url)
.pipe(createWriteStream(recover_path))
.on("close", () => {
// pass
});
}
function start_streaming(recording_dir: string) {
print(`Starting stream recording: ${stream_uri}`);
print(`Creating new fs stream: ${recording_dir}`);
stream_request = request
.get(stream_uri)
.on("error", (err: Error) => {
log_error(err);
teardown().then(() => dj_change());
})
.on("complete", () => {
print(`Stream request completed, restarting.`);
teardown().then(() => dj_change());
})
.pipe(
createWriteStream(
format({
base: "raw_recording.mp3",
dir: recording_dir,
}),
{ flags: "w" },
).on("error", (err: Error) => log_error(err)),
);
}
function teardown() {
return new Promise<void>((res) => {
if (stream_request != null) {
stream_request.destroy();
stream_request = null;
}
if (last_rec) {
if (cueSplit) {
createCompliantCueSheets(raw_data_folder);
} else {
splitCueSheet(raw_data_folder);
}
last_rec = false;
}
current_song = 1;
rec_start = null;
res();
});
}
function dj_change() {
// Skip if excluded (ex: hanyuu), or stopped.
if (excluded_djs.includes(api.dj_name) || force_stop) {
if (!force_stop) {
print(`Excluded DJ ${api.dj_name} detected, skipping.`);
}
return teardown();
}
return new Promise<void>((res) => {
print(api.dj_name + " has taken over.");
const timestamp = sane_fs(`${Math.floor(Date.now() / 1000)}`);
const output_folder = `${timestamp} ${api.dj_name}`;
const dj_folder = join(export_folder, output_folder);
raw_data_folder = dj_folder;
mkdir(dj_folder, (err) => {
if (err && err.code !== "EEXIST") {
log_error(err);
throw err;
}
print("Setting up the stream");
rec_start = api.current_time;
last_rec = true;
start_streaming(dj_folder);
cueSheet = new CueSheet(api.dj_name, timestamp, join(dj_folder, "proto.cue"));
song_change();
get_dj_pic(dj_folder);
res();
});
current_song = 1;
});
}
function poll_api() {
resolve_after_get(api_uri).then((results: { main: any }) => {
try {
if (!results) {
print("Empty api results object.")
return;
}
let old_np;
let old_dj;
if (Object.keys(api).length !== 0) {
old_np = api.np;
old_dj = api.dj_name;
} else {
old_dj = "";
}
api = {
current_time: results.main.current,
dj_name: results.main.dj.djname,
dj_pic: results.main.dj.djimage,
end_time: results.main.end_time,
listeners: results.main.listeners,
lp: results.main.lp,
np: results.main.np,
start_time: results.main.start_time,
};
if (force_stop) {
return;
}
if (api.dj_name !== old_dj) {
teardown().then(() => dj_change());
} else if (api.np !== old_np) {
if (!excluded_djs.includes(api.dj_name)) {
song_change();
}
}
} catch (err) {
log_error(err);
}
});
}
function poll_server() {
resolve_after_get(server_uri).then((results: { icestats: any }) => {
try {
const stats = results.icestats.source[0];
const stream = results.icestats.source[1];
server = {
audio_format: stats.server_type,
bitrate: stats.bitrate,
sample_rate: stats.samplerate,
server_description: stream.server_description,
server_name: stream.server_name,
};
} catch (err) {
log_error(err);
}
});
}
function start_server() {
app = express();
app.use(cors()); // For graphql over http
app.use(
"/graphql",
express_graphql({
graphiql: true,
rootValue: root,
schema: SCHEMA,
}),
);
app.listen(4000, () => print("Express GraphQL Server Now Running On localhost:4000/graphql"));
http_server = createServer(app);
app.use(express.static(resolve(export_folder)));
http_server.listen(8080);
print("HTTP server running on localhost:8080");
}
function start_polling() {
force_stop = false;
poll_api();
poll_server();
setTimeout(() => {
polling_interval_id = setInterval(poll_api, poll_interval);
}, 1000);
}
const getApi = () => {
return api;
};
const getServer = () => {
return server;
};
const isValidDj = () => {
const valid = {
force_stop,
valid_dj: !excluded_djs.includes(api.dj_name),
};
return valid;
};
const getMiscData = () => {
return {
dj_image_link: api_uri + "/dj-image/" + api.dj_pic,
rec_start,
};
};
const getConfigData = () => {
return {
api_uri,
server_uri,
stream_uri,
poll_interval,
excluded_djs,
export_folder,
};
};
const updateConfig = (data: IUpdateDataObject) => {
print(data);
api_uri = data.config.api_uri;
server_uri = data.config.server_uri;
stream_uri = data.config.stream_uri;
poll_interval = data.config.poll_interval;
excluded_djs = data.config.excluded_djs;
let new_export_path: string;
if (data.config.export_folder === "") {
new_export_path = format(parse("."));
} else {
new_export_path = format(parse(data.config.export_folder));
}
if (export_folder !== new_export_path) {
teardown().then(() => {
mkdir(new_export_path, (err) => {
if (err && err.code !== "EEXIST") {
log_error(err);
throw err;
}
export_folder = new_export_path;
update_reader(export_folder);
app.use(express.static(resolve(export_folder)));
if (auto_save) {
save_config();
}
dj_change();
});
});
} else {
if (auto_save) {
save_config();
}
teardown().then(() => dj_change());
}
return "Changed";
};
const streamAction = (data: { action: string }) => {
print(data);
if (data.action === "stop") {
force_stop = true;
} else if (data.action === "start") {
if (force_stop) {
force_stop = false;
}
}
teardown().then(() => dj_change());
return true;
};
const printLog = (msg: string) => {
print(msg);
return msg;
};
const root = {
api: getApi,
server: getServer,
valid: isValidDj,
misc: getMiscData,
config: getConfigData,
updateConfig,
past_recordings: getPastRecordings,
recording_cover: getRecordingCover,
full_recording: getRecordedSongs,
printLog,
streamAction,
dj_change,
};
export function stop_everything() {
app.close();
http_server.close();
clearInterval(polling_interval_id);
}
export function initial_start(options: { config: string; default: boolean; auto: boolean; cueSplit: boolean }) {
print("Starting Nora v1.2.0");
let config;
config_file = options.config ? options.config : "config.json";
if (options.config && existsSync(options.config)) {
config = JSON.parse(readFileSync(options.config, "utf-8")).config;
} else if (options.default) {
if (existsSync(config_file)) {
config = JSON.parse(readFileSync(config_file, "utf-8")).config;
} else {
save_config();
}
}
cueSplit = options.cueSplit || false;
if (config) {
api_uri = config.api_uri;
server_uri = config.server_uri;
stream_uri = config.stream_uri;
poll_interval = config.poll_interval;
excluded_djs = config.excluded_djs;
cueSplit = config.cueSplit || cueSplit;
if (config.export_folder === "") {
export_folder = format(parse("."));
} else {
export_folder = format(parse(config.export_folder));
}
}
auto_save = options.auto;
mkdir(export_folder, (err) => {
if (err && err.code !== "EEXIST") {
log_error(err);
throw err;
}
});
update_reader(export_folder);
start_server();
start_polling();
}