@@ -41,10 +41,10 @@ function buildComicOutput(xkcdPayload, comicFound, additionalComics) {
4141 ]
4242 } ) ;
4343
44- if ( additionalComics !== null ) {
44+ if ( additionalComics && additionalComics . length > 1 ) {
4545 var msgArr = [ ] ;
4646 for ( var i = 0 ; i < additionalComics . length ; i ++ ) {
47- additionalComics [ i ] ! = xkcdPayload . num ? msgArr . push ( additionalComics [ i ] ) : null ;
47+ parseInt ( additionalComics [ i ] , 10 ) != = xkcdPayload . num ? msgArr . push ( additionalComics [ i ] ) : null ;
4848 }
4949 blockArr . push ( {
5050 "type" : "context" ,
@@ -64,23 +64,6 @@ function buildComicOutput(xkcdPayload, comicFound, additionalComics) {
6464
6565}
6666
67- function getComicNumbers ( body ) {
68- var regex = / t i t l e = " ( [ 0 - 9 ] + ) : .+ " / gm;
69- var matches ;
70- var comicNumbers = [ ] ; // store the search results, just the comic number
71- while ( ( matches = regex . exec ( body ) ) !== null ) {
72- // This is necessary to avoid infinite loops with zero-width matches
73- if ( matches . index === regex . lastIndex ) {
74- regex . lastIndex ++ ;
75- }
76-
77- // push the actual match to the array
78- comicNumbers . push ( matches [ 1 ] ) ;
79- }
80-
81- return comicNumbers ;
82- }
83-
8467// if no parameters, display latest
8568// if number, display that number if valid otherwise random
8669// if -random then find a random
@@ -91,6 +74,7 @@ var comicNumProvided = /^-?\d+$/.test(terms);
9174var endPoint = 'https://xkcd.com/info.0.json' ;
9275var comicFound = ( terms == "" ) ;
9376var msg ;
77+ var comicNumbers = [ ] ;
9478
9579// we need the number of comics to be able to handle random, number, and negative path
9680var restMsg = new sn_ws . RESTMessageV2 ( ) ;
@@ -107,52 +91,74 @@ if (terms != "" && (terms == "-random" || comicNumProvided)) {
10791 comicFound = true ;
10892 } else {
10993 // otherwise just work out
110- var randomComic = Math . floor ( Math . random ( ) * parseInt ( jsonBody . num ) ) ;
94+ var randomComic = Math . floor ( Math . random ( ) * parseInt ( jsonBody . num ) ) + 1 ;
11195 endPoint = "https://xkcd.com/" + randomComic + "/info.0.json" ;
11296 comicFound = ! comicNumProvided ; // set comic found to true only if a random one was selected on purpose
11397 }
11498} else if ( terms != "" ) {
115- // assume it's a search, so use the search facility of explainxkcd
116- // the search results don't give us an indication of relevance, so to clean the results
117- // up we'll remove punctuation and insignificant words
118- var littleWords = [ 'of' , 'the' , "in" , "on" , "at" , "to" , "a" , "is" ] ;
119- terms = terms . split ( ' ' )
120- . filter ( function ( _x ) {
121- return _x != '' && littleWords . indexOf ( _x . toLowerCase ( ) ) == - 1 ;
122- } )
123- . join ( '+' ) ;
124-
125- var searchEndPoint = "https://www.explainxkcd.com/wiki/index.php?search=" + terms + "&title=Special%3ASearch&profile=default&fulltext=1" ;
126- var searchMsg = new sn_ws . RESTMessageV2 ( ) ;
127- searchMsg . setHttpMethod ( 'GET' ) ;
128- searchMsg . setEndpoint ( searchEndPoint ) ;
129- searchMsg . setRequestHeader ( 'User-Agent' , 'servicenow' ) ;
130- response = searchMsg . execute ( ) ;
131- var body = response . getBody ( ) ;
132-
133- // search the body in two passes, first the section that contains the page title matches
134- // if nothing found there, we'll search the page text section which is a search of body text
135- var pageTitleBody = body . substr ( 0 , body . indexOf ( 'Page text matches' ) ) ;
136- var pageTextBody = body . substr ( body . indexOf ( 'Page text matches' ) ) ;
137- var comicNumbers = getComicNumbers ( pageTitleBody ) ;
138- comicNumbers = comicNumbers . length > 0 ? comicNumbers : getComicNumbers ( pageTextBody ) ;
139-
140- if ( comicNumbers . length > 0 ) {
141- // we have some comics so get a random one
142- var randomComic = Math . floor ( Math . random ( ) * comicNumbers . length ) ;
143- endPoint = "https://xkcd.com/" + comicNumbers [ randomComic ] + "/info.0.json" ;
144- comicFound = true ;
145- } else {
146- var randomComic = Math . floor ( Math . random ( ) * parseInt ( jsonBody . num ) ) ;
147- endPoint = "https://xkcd.com/" + randomComic + "/info.0.json" ;
148- }
99+
100+ var searchEndPoint =
101+ "https://qtg5aekc2iosjh93p.a1.typesense.net" +
102+ "/collections/xkcd/documents/search" +
103+ "?q=" + encodeURIComponent ( terms ) +
104+ "&query_by=title,altTitle,transcript,topics" +
105+ "&query_by_weights=127,80,80,1" +
106+ "&num_typos=1" +
107+ "&exclude_fields=embedding" ;
108+
109+ var searchMsg = new sn_ws . RESTMessageV2 ( ) ;
110+ searchMsg . setHttpMethod ( 'GET' ) ;
111+ searchMsg . setEndpoint ( searchEndPoint ) ;
112+
113+ // this is a public key, not specific to us.
114+ searchMsg . setRequestHeader (
115+ 'X-TYPESENSE-API-KEY' ,
116+ '8hLCPSQTYcBuK29zY5q6Xhin7ONxHy99'
117+ ) ;
118+
119+ response = searchMsg . execute ( ) ;
120+
121+ if ( response . getStatusCode ( ) == 200 ) {
122+
123+ var searchResults = JSON . parse ( response . getBody ( ) ) ;
124+ comicNumbers = [ ] ;
125+
126+ if ( searchResults . hits ) {
127+ for ( var i = 0 ; i < Math . min ( searchResults . hits . length , 10 ) ; i ++ ) {
128+ comicNumbers . push (
129+ searchResults . hits [ i ] . document . id
130+ ) ;
131+ }
132+ }
133+
134+ if ( comicNumbers . length > 0 ) {
135+
136+ // Use the highest ranked result instead of random
137+ endPoint =
138+ "https://xkcd.com/" +
139+ comicNumbers [ 0 ] +
140+ "/info.0.json" ;
141+
142+ comicFound = true ;
143+
144+ } else {
145+
146+ var randomComic =
147+ Math . floor ( Math . random ( ) * parseInt ( jsonBody . num ) ) ;
148+
149+ endPoint =
150+ "https://xkcd.com/" +
151+ randomComic +
152+ "/info.0.json" ;
153+ }
154+ }
149155}
150156
151157var comicDataMsg = new sn_ws . RESTMessageV2 ( ) ;
152158comicDataMsg . setHttpMethod ( 'GET' ) ;
153159comicDataMsg . setEndpoint ( endPoint ) ;
154160comicDataMsg . setRequestHeader ( 'User-Agent' , 'servicenow' ) ;
155- var response = comicDataMsg . execute ( ) ;
161+ response = comicDataMsg . execute ( ) ;
156162var body = JSON . parse ( response . getBody ( ) ) ;
157163
158164if ( comicNumbers && comicNumbers . length > 1 ) {
0 commit comments