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,17 +7,7 @@ var request = require('request');
var link = function(dbot) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var links = {};
var commands = {
'~title': function(event) {
var link = links[event.channel.name];
if(event.params[1] !== undefined) {
var urlMatches = event.params[1].match(urlRegex);
if(urlMatches !== null) {
link = urlMatches[0];
}
}
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, " ");
@ -29,6 +19,18 @@ var link = function(dbot) {
}
}
});
};
var commands = {
'~title': function(event) {
var link = links[event.channel.name];
if(event.params[1] !== undefined) {
var urlMatches = event.params[1].match(urlRegex);
if(urlMatches !== null) {
link = urlMatches[0];
}
}
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]);
}
}
},