forked from GitHub/dbot
Remove repeated title fetching code in Link module
This commit is contained in:
parent
c9d78b99c9
commit
5e8495c3bb
@ -7,6 +7,19 @@ var request = require('request');
|
|||||||
var link = function(dbot) {
|
var link = function(dbot) {
|
||||||
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
|
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
|
||||||
var links = {};
|
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 = {
|
var commands = {
|
||||||
'~title': function(event) {
|
'~title': function(event) {
|
||||||
@ -17,18 +30,7 @@ var link = function(dbot) {
|
|||||||
link = urlMatches[0];
|
link = urlMatches[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fetchTitle(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'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,15 +45,7 @@ var link = function(dbot) {
|
|||||||
links[event.channel.name] = urlMatches[0];
|
links[event.channel.name] = urlMatches[0];
|
||||||
|
|
||||||
if(dbot.config.link.autoTitle == true) {
|
if(dbot.config.link.autoTitle == true) {
|
||||||
request(urlMatches[0], function (error, response, body) {
|
fetchTitle(event, urlMatches[0]);
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user