Remove repeated title fetching code in Link module

This commit is contained in:
reality 2012-12-30 17:36:52 +00:00
parent c9d78b99c9
commit 5e8495c3bb

View File

@ -7,6 +7,19 @@ var request = require('request');
var link = function(dbot) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var links = {};
var fetchTitle = function(event, link) {
request(link, function (error, response, body) {
if(!error && response.statusCode == 200) {
body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
var title = body.valMatch(/<title>(.*)<\/title>/, 2);
if(title) {
event.reply(title[1]);
} else {
event.reply(dbot.t('title_not_found'));
}
}
});
};
var commands = {
'~title': function(event) {
@ -17,18 +30,7 @@ var link = function(dbot) {
link = urlMatches[0];
}
}
request(link, function (error, response, body) {
if(!error && response.statusCode == 200) {
body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
var title = body.valMatch(/<title>(.*)<\/title>/, 2);
if(title) {
event.reply(title[1]);
} else {
event.reply(dbot.t('title_not_found'));
}
}
});
fetchTitle(event, link);
}
};
@ -43,15 +45,7 @@ var link = function(dbot) {
links[event.channel.name] = urlMatches[0];
if(dbot.config.link.autoTitle == true) {
request(urlMatches[0], function (error, response, body) {
if(!error && response.statusCode == 200) {
body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
var title = body.valMatch(/<title>(.*)<\/title>/, 2);
if(title) {
event.reply(title[1]);
}
}
});
fetchTitle(event, urlMatches[0]);
}
}
},