-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
executable file
·196 lines (163 loc) · 6.54 KB
/
main.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
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const childProcess = require('child_process');
const path = require('path');
const bhttp = require('bhttp');
const cheerio = require('cheerio');
const colors = require('colors');
const moment = require('moment');
const yaml = require('js-yaml');
const mvAsync = Promise.promisify(require('mv'));
const mkdirpAsync = Promise.promisify(require('mkdirp'));
const session = bhttp.session();
const config = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8'));
config.captureDirectory = config.captureDirectory || 'capture';
config.completeDirectory = config.completeDirectory || 'complete';
config.modelScanInterval = config.modelScanInterval || 60;
config.minFileSizeMb = config.minFileSizeMb || 5;
config.debug = !!config.debug;
config.rtmpDebug = !!config.rtmpDebug;
config.models = Array.isArray(config.models) ? config.models : [];
config.dateFormat = config.dateFormat || 'YYYYMMDD-HHmmss';
config.createModelDirectory = !!config.createModelDirectory;
const captureDirectory = path.resolve(__dirname, config.captureDirectory);
const completeDirectory = path.resolve(__dirname, config.completeDirectory);
const minFileSize = config.minFileSizeMb * 1048576;
let captures = [];
function printMsg(...args) {
console.log.apply(console, [colors.gray(`[${moment().format('MM/DD/YYYY - HH:mm:ss')}]`), ...args]);
}
const printErrorMsg = printMsg.bind(printMsg, colors.red('[ERROR]'));
const printDebugMsg = config.debug ? printMsg.bind(printMsg, colors.yellow('[DEBUG]')) : () => {};
function captureModel(params) {
const { model, streamServer, playpath } = params;
return Promise
.try(() => {
printMsg(colors.green(model), 'is online, starting rtmpdump process');
const filename = `${model}_${moment().format(config.dateFormat)}.flv`;
const spawnArguments = [
'--live',
config.rtmpDebug ? '' : '--quiet',
'--rtmp', `rtmp://${streamServer}:1935/webrtc`,
'--playpath', `${playpath}_aac`,
'--flv', path.join(captureDirectory, filename),
];
printDebugMsg('spawnArguments:', spawnArguments);
const proc = childProcess.spawn('rtmpdump', spawnArguments);
proc.stdout.on('data', (data) => {
printMsg(data.toString());
});
proc.stderr.on('data', (data) => {
printMsg(data.toString());
});
proc.on('close', () => {
printMsg(colors.green(model), 'stopped streaming');
captures = captures.filter(c => c.model !== model);
const src = path.join(captureDirectory, filename);
const dst = config.createModelDirectory
? path.join(completeDirectory, model, filename)
: path.join(completeDirectory, filename);
fs.statAsync(src)
// if the file is big enough we keep it otherwise we delete it
.then(stats => (stats.size <= minFileSize
? fs.unlinkAsync(src)
: mvAsync(src, dst, { mkdirp: true })
))
.catch((err) => {
if (err.code !== 'ENOENT') {
printErrorMsg(colors.red(`[${model}]`), err.toString());
}
});
});
if (proc.pid) {
captures.push({
model,
filename,
proc,
checkAfter: moment().unix() + 60, // we are gonna check the process after 60 seconds
size: 0,
});
}
})
.catch((err) => {
printErrorMsg(colors.red(`[${model.model}]`), err.toString());
});
}
function checkCapture(capture) {
if (!capture.checkAfter || capture.checkAfter > moment().unix()) {
// if this is not the time to check the process then we resolve immediately
printDebugMsg(colors.green(capture.model), '- OK');
return null;
}
printDebugMsg(colors.green(capture.model), 'should be checked');
return fs
.statAsync(path.join(captureDirectory, capture.filename))
.then((stats) => {
// we check the process after 60 seconds since the its start,
// then we check it every 10 minutes,
// if the size of the file has not changed over the time, we kill the process
if (stats.size - capture.size > 0) {
printDebugMsg(colors.green(capture.model), '- OK');
capture.checkAfter = moment().unix() + 600; // 10 minutes
capture.size = stats.size;
} else if (capture.model) {
// we assume that onClose will do all the cleaning for us
printErrorMsg(colors.red(`[${capture.model}]`), 'Process is dead');
capture.childProcess.kill();
}
})
.catch((err) => {
if (err.code === 'ENOENT') {
// do nothing, file does not exists,
} else {
printErrorMsg(colors.red(`[${capture.model}]`), err.toString());
}
});
}
function mainLoop() {
printDebugMsg('Start searching for new models');
return Promise
.try(() => session.get('https://showup.tv'))
.then(response => cheerio.load(response.body))
.then(($) => {
const onlineModels = [];
$('li[transcoderaddr][streamid]').each((i, e) => {
// printDebugMsg(e);
const $e = $(e);
onlineModels.push({
model: $e.find('.stream__meta h4').text(),
streamServer: $e.attr('transcoderaddr'),
playpath: $e.attr('streamid'),
});
});
return onlineModels;
})
.then((onlineModels) => {
const configModels = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8')).models;
return onlineModels.filter(o => configModels.find(m => m === o.model));
})
.then(modelsToCapture => modelsToCapture.filter(m => !captures.find(p => p.model === m.model)))
.then(modelsToCapture => Promise.all(modelsToCapture.map(captureModel)))
.then(() => Promise.all(captures.map(checkCapture)))
.catch(printErrorMsg)
.finally(() => {
captures.forEach((c) => {
printDebugMsg(colors.grey(c.proc.pid.toString().padEnd(12, ' ')), colors.grey(c.checkAfter), colors.grey(c.filename));
});
printMsg('Done, will search for new models in', config.modelScanInterval, 'second(s).');
setTimeout(mainLoop, config.modelScanInterval * 1000);
});
}
Promise
.try(() => session.get('https://showup.tv/site/accept_rules?ref=https://showup.tv/'))
.then(() => session.post('https://showup.tv/site/accept_rules?ref=https://showup.tv/', {
decision: true,
}, {
referer: 'http://showup.tv/site/accept_rules?ref=http://showup.tv/site/log_in',
}))
.then(() => mkdirpAsync(captureDirectory))
.then(() => mkdirpAsync(completeDirectory))
.then(() => mainLoop())
.catch((err) => {
printErrorMsg(err.toString());
});