From bbfc4c18a6dd49694799d4833b03631f6049644c Mon Sep 17 00:00:00 2001 From: Scritches Date: Thu, 26 Apr 2018 19:12:20 -0400 Subject: [PATCH] xkcd title / relevent text search feature done right. --- modules/link/link.js | 82 +++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index d9550be..62a7790 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -9,15 +9,15 @@ var request = require('request'), var link = function(dbot) { this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; - this.links = {}; + this.links = {}; this.handlers = []; - + this.api = { 'addHandler': function(name, regex, handler) { - this.handlers.push({ - 'name': name, - 'regex': regex, - 'callback': handler + this.handlers.push({ + 'name': name, + 'regex': regex, + 'callback': handler }); }, @@ -43,8 +43,8 @@ var link = function(dbot) { }, 'udLookup': function(query, callback) { - var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + - encodeURI(query); + var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + + encodeURI(query); request(reqUrl, function(error, response, body) { try { @@ -62,14 +62,14 @@ var link = function(dbot) { 'parseLink': function(link, callback) { var handler = false; for(var i=0;i p.title.indexOf(':') > 0); + + if (pages.length > 0) { + // See if any of these matches are exact title matches + var match = false; + _.each(pages, function(p) { + var title = p.title.slice(p.title.indexOf(':')+2).trim(); + if(title.toLowerCase() == comicId.toLowerCase()) { + match = p; + } + }); + + if (match) { + // We got a match! Get the ID and let's get tf out of here. + comicId = match.title.slice(0, match.title.indexOf(':')); + } else { + comicId = pages[0].title.slice(0, pages[0].title.indexOf(':')); + } var link = "http://xkcd.com/"+comicId+"/info.0.json"; request(link, function(error, response, body) { @@ -132,6 +163,11 @@ var link = function(dbot) { } } catch(err) { }; }); + + + } else { + event.reply(dbot.t("no-hits")); + } }); } else { if(comicId !== "") { @@ -152,7 +188,7 @@ var link = function(dbot) { } } }, - + '~ud': function(event) { var query = event.input[1]; @@ -175,7 +211,7 @@ var link = function(dbot) { console.log('DEBUG: got a link'); if(this.config.autoTitle == true) { this.api.parseLink(urlMatches[0], function(result) { - event.reply(result); + event.reply(result); }); } }