Skip to content

Commit

Permalink
add -random option to xkcd parser (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayden-Chiu authored Oct 3, 2023
1 parent 46b7f4d commit 98d8672
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Parsers/Find a relevant XKCD comic.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ function buildComicOutput(xkcdPayload) {
return block;
}

var search = gs.urlEncode(current.text.replace(/!xkcd/g, '').trim());
var terms = current.text.replace(/!xkcd/g, '').trim();
var search = gs.urlEncode(terms);
var endpoint;
if (terms === '-random' || terms === '') {
endpoint = 'https://www.explainxkcd.com/wiki/index.php/Special:Random';
} 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('https://www.explainxkcd.com/wiki/index.php?&title=Special%3ASearch&go=Go&fulltext=1&search=' + search);
rm.setEndpoint(endpoint);
rm.setRequestHeader('User-Agent', 'servicenow');
var response = rm.execute();
var body = response.getBody();
Expand All @@ -51,7 +58,7 @@ 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]+/gm)[0].replace(/<a href="\/wiki\/index.php\//g, '');
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');
Expand Down

0 comments on commit 98d8672

Please sign in to comment.