mirror of
https://github.com/reality/dbot.git
synced 2025-01-23 10:34:31 +01:00
Remove repeated title fetching code in Link module
This commit is contained in:
parent
c9d78b99c9
commit
5e8495c3bb
@ -7,17 +7,7 @@ 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) {
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
request(link, function (error, response, body) {
|
request(link, function (error, response, body) {
|
||||||
if(!error && response.statusCode == 200) {
|
if(!error && response.statusCode == 200) {
|
||||||
body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
|
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];
|
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