-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain64.js
334 lines (270 loc) · 9.44 KB
/
main64.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
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
'use strict';
var Promise = require('bluebird');
var fs = require('fs');
var yaml = require('js-yaml');
var moment = require('moment');
var mkdirp = require('mkdirp');
var S = require('string');
var WebSocketClient = require('websocket').client;
var http = require('http');
var colors = require('colors');
var _ = require('underscore');
var childProcess = require('child_process');
var path = require('path');
function getCurrentDateTime() {
return moment().format('YYYYMMDD-HHmmss');
};
function printMsg(msg) {
console.log(colors.blue('[' + getCurrentDateTime() + ']'), msg);
}
function printErrorMsg(msg) {
console.log(colors.blue('[' + getCurrentDateTime() + ']'), colors.red('[ERROR]'), msg);
}
function printDebugMsg(msg) {
if (config.debug && msg) {
console.log(colors.blue('[' + getCurrentDateTime() + ']'), colors.yellow('[DEBUG]'), msg);
}
}
function getFileno() {
return new Promise(function(resolve, reject) {
var client = new WebSocketClient();
client.on('connectFailed', function(err) {
reject(err);
});
client.on('connect', function(connection) {
connection.on('error', function(err) {
reject(err);
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
var parts = /\{%22fileno%22:%22([0-9_]*)%22\}/.exec(message.utf8Data);
if (parts && parts[1]) {
// printDebugMsg('fileno = ' + parts[1]);
connection.close();
resolve(parts[1]);
}
}
});
connection.sendUTF("hello fcserver\n\0");
connection.sendUTF("1 0 0 20071025 0 guest:guest\n\0");
});
client.connect('ws://xchat20.myfreecams.com:8080/fcsl', '', 'http://xchat20.myfreecams.com:8080', {Cookie: ''});
}).timeout(30000); // 30 secs
}
function getOnlineModels(fileno) {
return new Promise(function(resolve, reject) {
var url = 'http://www.myfreecams.com/mfc2/php/mobj.php?f=' + fileno + '&s=xchat20';
// printDebugMsg(url);
http
.get(url, function(response) {
var rawHTML = '';
if (response.statusCode == 200) {
response.on('data', function(data) {
rawHTML += data;
});
response.on('end', function() {
try {
rawHTML = rawHTML.toString('utf8');
rawHTML = rawHTML.substring(rawHTML.indexOf('{'), rawHTML.indexOf("\n") - 1);
rawHTML = rawHTML.replace(/[^\x20-\x7E]+/g, '');
var data = JSON.parse(rawHTML);
var onlineModels = [];
for (var key in data) {
if (data.hasOwnProperty(key) && typeof data[key].nm != 'undefined' && typeof data[key].uid != 'undefined') {
onlineModels.push({
nm: data[key].nm,
uid: data[key].uid,
vs: data[key].vs,
camserv: data[key].u.camserv,
camscore: data[key].m.camscore,
new_model: data[key].m.new_model
});
}
}
printMsg(onlineModels.length + ' model(s) online');
resolve(onlineModels);
} catch (err) {
reject(err);
}
});
} else {
reject('Invalid response: ' + response.statusCode);
}
})
.on('error', function(err) {
reject(err);
});
}).timeout(30000); // 30 secs
}
function selectMyModels(onlineModels) {
return Promise
.try(function() {
printDebugMsg(config.models.length + ' model(s) in config');
var dirty = false;
var stats = fs.statSync('updates.yml');
if (stats.isFile()) {
var updates = yaml.safeLoad(fs.readFileSync('updates.yml', 'utf8'));
if (!updates.includeModels) {
updates.includeModels = [];
}
if (!updates.excludeModels) {
updates.excludeModels = [];
}
// first we push changes to main config
if (updates.includeModels.length > 0) {
printMsg(updates.includeModels.length + ' model(s) to include');
config.includeModels = _.union(config.includeModels, updates.includeModels);
dirty = true;
}
if (updates.excludeModels.length > 0) {
printMsg(updates.excludeModels.length + ' model(s) to exclude');
config.excludeModels = _.union(config.excludeModels, updates.excludeModels);
dirty = true;
}
// if there were some updates, then we reset updates.yml
if (dirty) {
updates.includeModels = [];
updates.excludeModels = [];
fs.writeFileSync('updates.yml', yaml.safeDump(updates), 0, 'utf8');
}
}
config.includeModels = _.reject(config.includeModels, function(nm) {
// if we managed to find id of the model in the collection of online models
// we add her id in models and remove he from includeModels
var model = _.findWhere(onlineModels, {nm: nm});
if (!model) {
return false;
} else {
config.models.push(model.uid);
dirty = true;
return true;
}
});
config.excludeModels = _.reject(config.excludeModels, function(nm) {
// if we managed to find id of the model in the collection of online models
// we remove her id in models and remove he from excludeModels
var model = _.findWhere(onlineModels, {nm: nm});
if (!model) {
return false;
} else {
config.models = _.without(config.models, model.uid);
dirty = true;
return true;
}
});
if (dirty) {
fs.writeFileSync('config.yml', yaml.safeDump(config), 0, 'utf8');
}
var myModels = [];
_.each(config.models, function(uid) {
var model = _.findWhere(onlineModels, {uid: uid});
if (model) {
if (model.vs === 0) {
myModels.push(model);
} else {
printMsg(colors.magenta(model.nm) + ' is away or in a private');
}
}
});
printDebugMsg(myModels.length + ' model(s) to capture');
return myModels;
});
}
function createCaptureProcess(model) {
if (modelsCurrentlyCapturing.indexOf(model.uid) != -1) {
printDebugMsg(colors.green(model.nm) + ' is already capturing');
return; // resolve immediately
}
printMsg(colors.green(model.nm) + ' is now online, starting capturing process');
return Promise
.try(function() {
var filename = model.nm + '_' + getCurrentDateTime() + '.ts';
var spawnArguments = [
'-hide_banner',
'-v',
'fatal',
'-i',
'http://video' + (model.camserv - 500) + '.myfreecams.com:1935/NxServer/ngrp:mfc_' + (100000000 + model.uid) + '.f4v_mobile/playlist.m3u8',
'-c',
'copy',
config.captureDirectory + '/' + filename
];
var captureProcess = childProcess.spawn('ffmpeg64', spawnArguments);
captureProcess.stdout.on('data', function(data) {
printMsg(data.toString);
});
captureProcess.stderr.on('data', function(data) {
printMsg(data.toString);
});
captureProcess.on('close', function(code) {
printMsg(colors.green(model.nm) + ' stopped streaming');
var modelIndex = modelsCurrentlyCapturing.indexOf(model.uid);
if (modelIndex !== -1) {
modelsCurrentlyCapturing.splice(modelIndex, 1);
}
fs.stat(config.captureDirectory + '/' + filename, function(err, stats) {
if (err) {
if (err.code == 'ENOENT') {
// do nothing, file does not exists
} else {
printErrorMsg('[' + colors.green(model.nm) + '] ' + err.toString());
}
} else if (stats.size === 0) {
fs.unlink(config.captureDirectory + '/' + filename);
} else {
fs.rename(config.captureDirectory + '/' + filename, config.completeDirectory + '/' + filename, function(err) {
if (err) {
printErrorMsg('[' + colors.green(model.nm) + '] ' + err.toString());
}
});
}
});
});
if (!!captureProcess.pid) {
modelsCurrentlyCapturing.push(model.uid);
}
})
.catch(function(err) {
printErrorMsg('[' + colors.green(model.nm) + '] ' + err.toString());
});
}
function mainLoop() {
printDebugMsg('Start searching for new models');
Promise
.try(function() {
return getFileno();
})
.then(function(fileno) {
return getOnlineModels(fileno);
})
.then(function(onlineModels) {
return selectMyModels(onlineModels);
})
.then(function(myModels) {
return Promise.all(myModels.map(createCaptureProcess));
})
.catch(function(err) {
printErrorMsg(err);
})
.finally(function() {
printMsg('Done, will search for new models in ' + config.modelScanInterval + ' second(s).');
setTimeout(mainLoop, config.modelScanInterval * 1000);
});
}
var modelsCurrentlyCapturing = new Array();
var config = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8'));
config.captureDirectory = path.resolve(config.captureDirectory);
config.completeDirectory = path.resolve(config.completeDirectory);
mkdirp(config.captureDirectory, function(err) {
if (err) {
printErrorMsg(err);
process.exit(1);
}
});
mkdirp(config.completeDirectory, function(err) {
if (err) {
printErrorMsg(err);
process.exit(1);
}
});
mainLoop();