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) {
var comicId = event.params[1] || "";
//var comicId = event.params[1] || "";
var comicId = event.params.slice(1).join(' ');
if(comicId == "*") {
request("http://xkcd.com/info.0.json", function(error, response, body){
@ -111,15 +112,45 @@ var link = function(dbot) {
});
} else {
if (isNaN(parseInt(comicId))) {
var relevantUrl = 'https://relevantxkcd.appspot.com/process';
request({
url: relevantUrl,
qs: {
action:'xkcd',
query: comicId
}
url: 'http://www.explainxkcd.com/wiki/api.php',
qs: {
action: 'query',
format: 'json',
generator: 'search',
gsrwhat: 'text',
gsrsearch: comicId,
prop: 'info|categories',
gsrlimit: 50
},
json: true
}, 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";
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 !== "") {