Skip to content

Commit

Permalink
Update Find a relevant XKCD comic.js
Browse files Browse the repository at this point in the history
If no comic is found, lookup a random comic and add a message letting people know that it is just a random one.
  • Loading branch information
eriemer1 authored Oct 3, 2023
1 parent ed01f59 commit e8649ee
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions Parsers/Find a relevant XKCD comic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ regex:!xkcd
flags:g
*/

function buildComicOutput(xkcdPayload) {
function buildComicOutput(xkcdPayload, foundComic) {
var blockArr = [];
var block = {};
if(!foundComic) { //if search didn't find a relevant comic add a message that this is a random one.
block = {};
block.type = "section";
block.text = {};
block.text.type = "plain_text";
block.text.text = 'No relevant XKCD found, here is a random one';
blockArr.push(block);
}
block = {};
block.type = "header";
block.text = {};
block.text.type = "plain_text";
Expand Down Expand Up @@ -37,6 +46,31 @@ function buildComicOutput(xkcdPayload) {
return block;
}

function getComic(endpoint) {
var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
rm.setEndpoint(endpoint);
rm.setRequestHeader('User-Agent', 'servicenow');
var response = rm.execute();
var body = response.getBody();
// Check if we got an empty search result, as the result regex will match the page even if no result
var checkResponse = body.match(/<p class="mw-search-nonefound">/g);
if (checkResponse === null) {
var result = body.match(/(?:<a href="\/wiki\/index.php\/)[0-9]+(?!" class="mw-redirect")/gm)[0].replace(/<a href="\/wiki\/index.php\//g, '');
if (parseInt(result)) {
var rm2 = new sn_ws.RESTMessageV2();
rm2.setHttpMethod('GET');
rm2.setEndpoint('https://xkcd.com/' + result + '/info.0.json');
rm2.setRequestHeader('User-Agent', 'servicenow');
var response2 = rm2.execute();
var body2 = JSON.parse(response2.getBody());
return body2;
} else {
return false;
}
}
}

var terms = current.text.replace(/!xkcd/g, '').trim();
var search = gs.urlEncode(terms);
var endpoint;
Expand All @@ -45,35 +79,13 @@ if (terms === '-random' || terms === '') {
} else {
endpoint = 'https://www.explainxkcd.com/wiki/index.php?&title=Special%3ASearch&go=Go&fulltext=1&search=' + search;
}

var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
rm.setEndpoint(endpoint);
rm.setRequestHeader('User-Agent', 'servicenow');
var response = rm.execute();
var body = response.getBody();

var msg;

// Check if we got an empty search result, as the result regex will match the page even if no result
var checkResponse = body.match(/<p class="mw-search-nonefound">/g);
if (checkResponse === null){
var result = body.match(/(?:<a href="\/wiki\/index.php\/)[0-9]+(?!" class="mw-redirect")/gm)[0].replace(/<a href="\/wiki\/index.php\//g, '');
if (parseInt(result)) {
var rm2 = new sn_ws.RESTMessageV2();
rm2.setHttpMethod('GET');
rm2.setEndpoint('https://xkcd.com/' + result + '/info.0.json');
rm2.setRequestHeader('User-Agent', 'servicenow');
var response2 = rm2.execute();
var body2 = JSON.parse(response2.getBody());

msg = buildComicOutput(body2);
} else {
msg = 'No relevant XKCD found for `' + search + '`';
}
}
else {
msg = 'No relevant XKCD found for `' + search + '`';
var foundComic = true;
var comic = getComic(endpoint);
if (comic = false){
foundComic = false;
endpoint = 'https://www.explainxkcd.com/wiki/index.php/Special:Random';
comic = getComic(endpoint);
}
buildComicOutput(comic, foundComic);

var sendIt = new x_snc_slackerbot.Slacker().send_chat(current, msg, false);

0 comments on commit e8649ee

Please sign in to comment.