Skip to content

Commit 19ced20

Browse files
committed
Release SARAH v2.9
1 parent 02c304b commit 19ced20

File tree

346 files changed

+50231
-3062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

346 files changed

+50231
-3062
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Disable LF normalization for all files
2+
* -text

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ overview.docx
2828
README/SayStatic/
2929
README/ScanSoftTTS/
3030
README/changelog.txt
31-
plugins/karotz/java/jre
31+
plugins/
3232

3333

3434
# External tool builders

script/lib/phantom.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ var exec = require('child_process').exec;
22
var spawn = require('child_process').spawn;
33
var fs = require('fs');
44
var xtend = require('./extend.js');
5+
var winston = require('winston');
56

67
exports.action = function(cmd, data, callback, SARAH){
78

8-
console.log('Fetching phantom script %s ...', cmd);
9+
winston.log('info', 'Fetching phantom script %s ...', cmd);
910

1011
// Compute script
1112
var script = 'script/phantom/'+cmd+'.js';
@@ -32,18 +33,18 @@ exports.action = function(cmd, data, callback, SARAH){
3233
var soft = config.http.phantom || (__dirname + "/../../PhantomJS/phantomjs.exe");
3334
var proc = path.normalize(soft);
3435

35-
console.log("Phantom: ", proc, '--proxy-type=none', script, json);
36+
winston.log('info',"Phantom: ", proc, '--proxy-type=none', script, json);
3637

3738
var child = spawn(proc, ['--proxy-type=none', script, json]);
3839
child.stderr.on('data', function (data) {
39-
console.log('Error: ',getBuffer(data));
40+
winston.log('warn','Error: ',getBuffer(data));
4041
callback({'tts' : 'Une erreur est survenue'});
4142
});
4243

4344
child.stdout.on('data', function (data) {
4445
var response = getBuffer(data);
4546
var json = JSON.parse(response);
46-
console.log('Success: ', response);
47+
winston.log('info','Success: ', response);
4748

4849
// Hook perfoming on results
4950
var module = false;
@@ -52,7 +53,7 @@ exports.action = function(cmd, data, callback, SARAH){
5253
try { module = require('./'+cmd+'.node.js'); } catch (ex) { }
5354
}
5455
if (module){
55-
console.log('Running phantom callback %s ...', cmd+'.node');
56+
winston.log('info','Running phantom callback %s ...', cmd+'.node');
5657
options.directory = data.directory;
5758
module.after(options, json, SARAH);
5859
}

script/lib/scraper.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var winston = require('winston');
2+
13
// ------------------------------------------
24
// EVALUATE
35
// ------------------------------------------
@@ -47,7 +49,7 @@ var scraper = {
4749

4850
// Check for page load success
4951
if (status !== "success") {
50-
console.log(JSON.stringify(results));
52+
winston.log('info',JSON.stringify(results));
5153
//phantom.exit();
5254
return;
5355
}
@@ -56,7 +58,7 @@ var scraper = {
5658

5759
// Load jQuery
5860
if (!page.injectJs(jquery)){
59-
console.log(JSON.stringify(results));
61+
winston.log('info',JSON.stringify(results));
6062
//phantom.exit();
6163
return;
6264
}

script/lib/upload.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
var winston = require('winston');
22
exports.action = function(req, res, config){
33

44
res.writeHead(200, {'content-type': 'text/plain'});
@@ -17,7 +17,7 @@ exports.action = function(req, res, config){
1717
path = req.files.file.path;
1818
}
1919

20-
console.log('Upload ' + path + ' to ' + upload+name);
20+
winston.log('info','Upload ' + path + ' to ' + upload+name);
2121

2222
var fs = require('fs');
2323
var util = require('util');

script/manager/config.js

+80-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var fs = require('fs');
22
var xtend = require('../lib/extend.js');
3+
var winston = require('winston');
4+
var pwd = require('path');
5+
36
// ------------------------------------------
47
// REQUIRE - HELPER
58
// ------------------------------------------
@@ -66,11 +69,11 @@ var loadPlugins = function(folder, conf){
6669

6770
// Ends with .prop
6871
if (file.endsWith('.prop')){
69-
console.log('Loading plugin properties %s ...', path);
72+
winston.log('info', 'Loading plugin properties %s ...', path);
7073
try {
7174
var json = fs.readFileSync(path,'utf8');
7275
var plugin = JSON.parse(json);
73-
} catch(ex){ console.log(ex); }
76+
} catch(ex){ winston.warn(ex); }
7477
xtend.extend(true, conf, plugin);
7578
}
7679
});
@@ -81,19 +84,19 @@ var getJSON = function(name){
8184
var path = 'plugins/'+name+'/'+name+'.prop';
8285
if (!fs.existsSync(path)){ return {}; }
8386

84-
console.log('Loading plugin properties %s ...', path);
87+
//winston.log('info', 'Loading plugin properties %s ...', path);
8588
try {
8689
var json = fs.readFileSync(path,'utf8');
8790
return JSON.parse(json);
88-
} catch(ex){ console.log(ex); }
91+
} catch(ex){ winston.warn(ex); }
8992
}
9093

9194
/**
9295
* Load default properties
9396
*/
9497
var loadProperties = function(){
9598
if (!fs.existsSync('script/wsrnode.prop')) { return {}; }
96-
console.log('Loading core properties...');
99+
winston.info('Loading core properties...');
97100
var json = fs.readFileSync('script/wsrnode.prop','utf8');
98101
return JSON.parse(json);
99102
}
@@ -103,11 +106,32 @@ var loadProperties = function(){
103106
*/
104107
var loadCustoms = function(){
105108
if (!fs.existsSync('custom.prop')) { return {}; }
106-
console.log('Loading custom properties...');
109+
winston.info('Loading custom properties...');
107110
var json = fs.readFileSync('custom.prop','utf8');
108111
var parse = {};
109-
try { parse = JSON.parse(json); } catch (ex){ console.log(ex.message); }
110-
return parse
112+
try { parse = JSON.parse(json); } catch (ex){ winston.error(ex.message); }
113+
114+
parse['modules'] = retains(parse['modules'], config['modules']);
115+
parse['phantoms'] = retains(parse['phantoms'], config['phantoms']);
116+
parse['cron'] = retains(parse['cron'], config['cron']);
117+
118+
return parse;
119+
}
120+
121+
var retains = function(source, target){
122+
if (typeof source != 'object') return source;
123+
124+
var clean = {};
125+
Object.keys(source).forEach(function(attr){
126+
if (attr == 'description' || attr == 'version'){ return false; }
127+
if (target[attr] === undefined
128+
&& attr != 'x' && attr != 'y'
129+
&& attr != 'w' && attr != 'h'
130+
&& attr != 'c'){ return winston.log('warn', 'Bypass config: ', attr); }
131+
clean[attr] = retains(source[attr], target[attr]);
132+
});
133+
134+
return clean;
111135
}
112136

113137
// ------------------------------------------
@@ -121,9 +145,9 @@ var saveProperties = function(cfg) {
121145

122146
//json = json.replace(/\{/g,"{\n ").replace(/\}/g,"\n }").replace(/,/g,",\n ");
123147
fs.writeFileSync('custom.prop', json, 'utf8');
124-
console.log('Properties saved successfully');
148+
winston.info('Properties saved successfully');
125149
} catch(ex) {
126-
console.log('Error while saving properties:', ex.message);
150+
winston.log('error', 'Error while saving properties:', ex.message);
127151
}
128152
}
129153

@@ -133,6 +157,15 @@ var saveProperties = function(cfg) {
133157

134158
var routes = function(req, res, next){
135159

160+
var json = req.body.json;
161+
if (json){
162+
json = JSON.parse(json);
163+
xtend.extend(true, config, json);
164+
ConfigManager.save(config);
165+
res.redirect('/home');
166+
return;
167+
}
168+
136169
var key = req.body.key;
137170
if (!key){ res.redirect('/home'); return; }
138171

@@ -141,7 +174,7 @@ var routes = function(req, res, next){
141174

142175
Object.keys(req.body).forEach(function(attr){
143176
if (attr == 'key') { return; }
144-
console.log(key+'.'+attr+' => '+req.body[attr]);
177+
winston.info(key+'.'+attr+' => '+req.body[attr]);
145178
cnf[attr] = req.body[attr];
146179
});
147180

@@ -156,21 +189,52 @@ var routes = function(req, res, next){
156189

157190
var getModule = function(name, uncache){
158191
var module = false;
192+
var path = false;
193+
159194
try {
160-
var path = '../../plugins/'+name+'/'+name+'.js';
195+
path = pwd.normalize(__dirname+'/../../plugins/'+name+'/'+name+'.js');
161196
if (config.debug || uncache){ require.uncache(path); }
162-
module = require(path);
197+
module = require(path);
163198
}
164199
catch (ex) {
165200
try {
166-
var path = '../'+name+'.js';
201+
path = pwd.normalize(__dirname+'/../'+name+'.js');
167202
if (config.debug || uncache){ require.uncache(path); }
168203
module = require(path);
169204
} catch (ex) { }
170205
}
206+
207+
initModule(module, name);
208+
if (!module){ return false; }
209+
if (uncache){ return module; }
210+
211+
// Force reload if file change
212+
213+
var modified = fs.statSync(path).mtime.getTime();
214+
if (!module.lastModified){
215+
module.lastModified = modified;
216+
}
217+
else if (module.lastModified < modified){
218+
winston.log('info', 'Reloading '+name);
219+
return getModule(name, true);
220+
}
221+
171222
return module;
172223
}
173224

225+
var initModule = function(module, name){
226+
try {
227+
if (!module) { return; }
228+
229+
if (module.initialized){ return; }
230+
module.initialized = true;
231+
232+
winston.log('info','initModule: ', name);
233+
if (!module.init){ return; }
234+
module.init(SARAH);
235+
} catch (ex) { winston.log('warn','initModule: ' + ex.message); }
236+
}
237+
174238
// ------------------------------------------
175239
// GET TICKER
176240
// ------------------------------------------
@@ -180,7 +244,7 @@ var getTicker = function(){
180244
var url = 'https://dl.dropbox.com/u/255810/Encausse.net/Sarah/plugins/ticker.json';
181245
var request = require('request');
182246
request({ 'uri' : url, json : true }, function (err, response, json){
183-
if (err || response.statusCode != 200) { console.log("Can't retrieve remote ticker"); return; }
247+
if (err || response.statusCode != 200) { winston.info("Can't retrieve remote ticker"); return; }
184248
ticker = json.message; // delayed by 1 request
185249
});
186250
return ticker;
@@ -208,7 +272,7 @@ var ConfigManager = {
208272
xtend.extend(true, config, loadPlugins());
209273
xtend.extend(true, config, loadCustoms());
210274
}
211-
catch(ex) { console.log('Error while loding properties:', ex.message); }
275+
catch(ex) { winston.log('error', 'Error while loding properties:', ex.message); }
212276
return ConfigManager;
213277
},
214278

script/manager/cron.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var winston = require('winston');
12
var cronJob = require('../vendor/cron').CronJob;
23

34
// ------------------------------------------
@@ -9,16 +10,16 @@ var cronJob = require('../vendor/cron').CronJob;
910
*/
1011
var startJob = function(task, module){
1112
if (!module){
12-
console.log('Missing CRON module');
13+
winston.warn('Missing CRON module: ', task.name);
1314
return;
1415
}
1516

1617
if (!task.time){
17-
console.log("Missing task's name or time properties");
18+
winston.warn("Missing task's name or time properties");
1819
return;
1920
}
2021

21-
console.log('Starting CRON Job for %s at %s', task.name, task.time);
22+
winston.log('info', 'Starting CRON Job for %s at %s', task.name, task.time);
2223

2324
// Build callback
2425
var callback = function(options){
@@ -29,7 +30,7 @@ var startJob = function(task, module){
2930
var job = new cronJob({
3031
cronTime: task.time,
3132
onTick: function() {
32-
console.log('Cron: %s', task.name);
33+
winston.log('info', 'Cron: %s', task.name);
3334
module.cron(callback, task, SARAH);
3435
},
3536
start: true
@@ -49,7 +50,7 @@ var startAll = function(){
4950
Object.keys(config.cron).forEach(function(key){
5051
var task = config.cron[key];
5152
var module = SARAH.ConfigManager.getModule(key);
52-
console.log('Starting task %s ...', key);
53+
winston.log('info', 'Starting task %s ...', key);
5354
CRONManager.start(task, module);
5455
});
5556
}

script/manager/phantom.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var url = require('url');
22
var qs = require('querystring');
33
var xtend = require('../lib/extend.js');
4+
var winston = require('winston');
45

56
// ------------------------------------------
67
// ROUTES PHANTOM
@@ -11,7 +12,8 @@ var routes = function(req, res, next){
1112
// Parse URL & QueryString
1213
var rUrl = url.parse(req.url);
1314
var rQs = qs.parse(rUrl.query);
14-
15+
rQs.body = req.body;
16+
1517
// Parse Command
1618
var cmd = rUrl.pathname;
1719
cmd = cmd.substring(cmd.lastIndexOf('/')+1);
@@ -20,20 +22,26 @@ var routes = function(req, res, next){
2022
SARAH.run(cmd, rQs, res);
2123
}
2224

23-
var run = function(cmd, rQs, res){
25+
var run = function(cmd, rQs, res, cb){
2426
// Callback
2527
var callback = function(opts){
28+
var end = (new Date()).getTime();
29+
winston.log('info', 'Run '+cmd+': '+(end-start)+'ms');
30+
31+
if (cb){ return cb(opts); }
32+
2633
var options = xtend.extend(true, rQs, opts);
2734
SARAH.dispatch(cmd, options, res);
2835
}
2936

3037
// Call phantom
38+
var start = (new Date()).getTime();
3139
try {
3240
var phantom = require('../lib/phantom.js');
3341
phantom.action(cmd, rQs, callback, SARAH);
3442
}
3543
catch(ex){
36-
console.log(ex);
44+
winston.log('warn', 'Phantom '+cmd+': ', ex);
3745
SARAH.dispatch({ tts : 'Je ne comprends pas'}, res);
3846
}
3947
}

0 commit comments

Comments
 (0)