Add config field for lstu;

This commit is contained in:
Benjamin Bouvier 2016-09-04 19:31:33 +02:00
parent e15e43e885
commit ca6dbcfbd5
2 changed files with 24 additions and 13 deletions

View File

@ -21,4 +21,7 @@ module.exports = {
// Network interface on which to run the webhook server. // Network interface on which to run the webhook server.
hostname: '0.0.0.0', hostname: '0.0.0.0',
// Instance of lstu to shorten links -- keep empty to not use.
lstu: 'https://lstu.fr'
} }

View File

@ -33,19 +33,27 @@ var app = express();
app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
function shortenURL(url, callback) { var shortenURL = function(url, callback) { callback(url); };
request('https://lstu.fr/a', { method: 'POST', form: { lsturl: url, format: 'json' } }, function (err, res, body) {
try { if (config.lstu) {
body = JSON.parse(body); if (config.lstu[config.lstu.length - 1] !== '/') {
} catch(err) { config.lstu += '/';
body = {err: 'cant parse JSON'}; }
}
if (err || !body.success) { shortenURL = function shortenURL(url, callback) {
console.error("Error when shortening link: (status: " + res.statusCode + ")", '\nerror:', err, '\nfailure reason:', body.msg); request(config.lstu + 'a', { method: 'POST', form: { lsturl: url, format: 'json' } }, function (err, res, body) {
} else { try {
callback(body.short); body = JSON.parse(body);
} } catch(err) {
}); body = {err: 'cant parse JSON'};
}
if (err || !body.success) {
console.error("Error when shortening link: (status: " + res.statusCode + ")", '\nerror:', err, '\nfailure reason:', body.msg);
} else {
callback(body.short);
}
});
};
} }
var handlers = { var handlers = {