xkcd title / relevent text search feature done right.

This commit is contained in:
Scritches 2018-04-26 19:12:20 -04:00
parent 0c8c377b08
commit bbfc4c18a6

View File

@ -97,7 +97,8 @@ var link = function(dbot) {
}, },
'~xkcd': function(event) { '~xkcd': function(event) {
var comicId = event.params[1] || ""; //var comicId = event.params[1] || "";
var comicId = event.params.slice(1).join(' ');
if(comicId == "*") { if(comicId == "*") {
request("http://xkcd.com/info.0.json", function(error, response, body){ request("http://xkcd.com/info.0.json", function(error, response, body){
@ -111,15 +112,45 @@ var link = function(dbot) {
}); });
} else { } else {
if (isNaN(parseInt(comicId))) { if (isNaN(parseInt(comicId))) {
var relevantUrl = 'https://relevantxkcd.appspot.com/process';
request({ request({
url: relevantUrl, url: 'http://www.explainxkcd.com/wiki/api.php',
qs: { qs: {
action:'xkcd', action: 'query',
query: comicId format: 'json',
} generator: 'search',
gsrwhat: 'text',
gsrsearch: comicId,
prop: 'info|categories',
gsrlimit: 50
},
json: true
}, function(err, res, body) { }, function(err, res, body) {
comicId = body.split(' ').slice(2)[0].trim(); if(!body) {
event.reply(dbot.t("no-hits"));
return;
}
var pages = _.values(body.query.pages);
// page titles must be of the format "####: $$$$$$"
pages = _.filter(pages, p => 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"; var link = "http://xkcd.com/"+comicId+"/info.0.json";
request(link, function(error, response, body) { request(link, function(error, response, body) {
@ -132,6 +163,11 @@ var link = function(dbot) {
} }
} catch(err) { }; } catch(err) { };
}); });
} else {
event.reply(dbot.t("no-hits"));
}
}); });
} else { } else {
if(comicId !== "") { if(comicId !== "") {