2022-08-23 09:57:42 +00:00
|
|
|
#lang racket/base
|
|
|
|
(require racket/dict
|
|
|
|
racket/list
|
|
|
|
racket/string
|
|
|
|
(prefix-in easy: net/http-easy)
|
|
|
|
; html libs
|
|
|
|
html-writing
|
|
|
|
; web server libs
|
|
|
|
net/url
|
|
|
|
web-server/http
|
|
|
|
(only-in web-server/dispatchers/dispatch next-dispatcher)
|
|
|
|
#;(only-in web-server/http/redirect redirect-to)
|
|
|
|
"application-globals.rkt"
|
2022-09-16 13:56:03 +00:00
|
|
|
"config.rkt"
|
|
|
|
"data.rkt"
|
2023-02-05 04:56:15 +00:00
|
|
|
"../lib/syntax.rkt"
|
2023-02-12 00:55:33 +00:00
|
|
|
"../lib/thread-utils.rkt"
|
2023-02-05 04:56:15 +00:00
|
|
|
"../lib/url-utils.rkt"
|
|
|
|
"whole-utils.rkt"
|
|
|
|
"../lib/xexpr-utils.rkt")
|
2022-08-23 09:57:42 +00:00
|
|
|
|
|
|
|
(provide
|
|
|
|
page-search)
|
|
|
|
|
|
|
|
(module+ test
|
2022-11-29 11:36:53 +00:00
|
|
|
(require rackunit
|
|
|
|
"test-utils.rkt")
|
2022-08-23 09:57:42 +00:00
|
|
|
(define search-json-data
|
|
|
|
'#hasheq((batchcomplete . #t) (query . #hasheq((search . (#hasheq((ns . 0) (pageid . 219) (size . 1482) (snippet . "") (timestamp . "2022-08-21T08:54:23Z") (title . "Gacha Capsule") (wordcount . 214)) #hasheq((ns . 0) (pageid . 201) (size . 1198) (snippet . "") (timestamp . "2022-07-11T17:52:47Z") (title . "Badges") (wordcount . 181)))))))))
|
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; this takes the info we gathered from fandom and makes the big fat x-expression page
|
2022-11-29 11:03:54 +00:00
|
|
|
(define (generate-results-page req dest-url wikiname query data #:siteinfo [siteinfo #f])
|
2022-08-23 09:57:42 +00:00
|
|
|
(define search-results (jp "/query/search" data))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; this is *another* helper that builds the wiki page UI and lets me put the search results (or whatever else) in the middle
|
2022-08-23 09:57:42 +00:00
|
|
|
(generate-wiki-page
|
2023-05-27 11:48:08 +00:00
|
|
|
;; so I provide my helper function with the necessary context...
|
2022-11-29 11:03:54 +00:00
|
|
|
#:req req
|
2022-09-16 12:56:05 +00:00
|
|
|
#:source-url dest-url
|
|
|
|
#:wikiname wikiname
|
2022-10-30 11:03:13 +00:00
|
|
|
#:title query
|
2022-10-09 09:50:50 +00:00
|
|
|
#:siteinfo siteinfo
|
2023-05-27 11:48:08 +00:00
|
|
|
;; and here's the actual results to display in the wiki page layout
|
2022-08-23 09:57:42 +00:00
|
|
|
`(div (@ (class "mw-parser-output"))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; header before the search results showing how many we found
|
2022-08-23 09:57:42 +00:00
|
|
|
(p ,(format "~a results found for " (length search-results))
|
|
|
|
(strong ,query))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; *u*nordered *l*ist of matching search results
|
2022-08-23 09:57:42 +00:00
|
|
|
(ul ,@(map
|
2023-05-27 11:48:08 +00:00
|
|
|
(λ (result) ;; for each result, run this code...
|
2022-08-23 09:57:42 +00:00
|
|
|
(let* ([title (jp "/title" result)]
|
2022-11-17 10:25:06 +00:00
|
|
|
[page-path (page-title->path title)]
|
2022-08-23 09:57:42 +00:00
|
|
|
[timestamp (jp "/timestamp" result)]
|
|
|
|
[wordcount (jp "/wordcount" result)]
|
|
|
|
[size (jp "/size" result)])
|
2023-05-27 11:48:08 +00:00
|
|
|
;; and make this x-expression...
|
2022-08-23 09:57:42 +00:00
|
|
|
`(li (@ (class "my-result"))
|
2023-05-27 11:48:08 +00:00
|
|
|
(a (@ (class "my-result__link") (href ,(format "/~a/wiki/~a" wikiname page-path))) ; using unquote to insert the result page URL
|
|
|
|
,title) ; using unquote to insert the result page title
|
|
|
|
(div (@ (class "my-result__info")) ; constructing the line under the search result
|
2022-08-23 09:57:42 +00:00
|
|
|
"last edited "
|
|
|
|
(time (@ (datetime ,timestamp)) ,(list-ref (string-split timestamp "T") 0))
|
|
|
|
,(format ", ~a words, ~a kb"
|
|
|
|
wordcount
|
|
|
|
(exact->inexact (/ (round (/ size 100)) 10)))))))
|
|
|
|
search-results)))))
|
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; will be called when the web browser asks to load the page
|
2022-08-23 09:57:42 +00:00
|
|
|
(define (page-search req)
|
2023-05-27 11:48:08 +00:00
|
|
|
;; this just means, catch any errors and display them in the browser. it's a function somewhere else
|
2022-08-23 09:57:42 +00:00
|
|
|
(response-handler
|
2023-05-27 11:48:08 +00:00
|
|
|
;; the URL will look like "/minecraft/wiki/Special:Search?q=Spawner"
|
|
|
|
;; grab the first part to use as the wikiname, in this case, "minecraft"
|
2022-08-23 09:57:42 +00:00
|
|
|
(define wikiname (path/param-path (first (url-path (request-uri req)))))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; grab the part after ?q= which is the search terms
|
2022-08-23 09:57:42 +00:00
|
|
|
(define query (dict-ref (url-query (request-uri req)) 'q #f))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; constructing the URL where I want to get fandom data from...
|
2022-08-23 09:57:42 +00:00
|
|
|
(define origin (format "https://~a.fandom.com" wikiname))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; the dest-URL will look something like https://minecraft.fandom.com/api.php?action=query&list=search&srsearch=Spawner&formatversion=2&format=json
|
2022-09-16 13:56:03 +00:00
|
|
|
(define dest-url
|
|
|
|
(format "~a/api.php?~a"
|
|
|
|
origin
|
|
|
|
(params->query `(("action" . "query")
|
|
|
|
("list" . "search")
|
|
|
|
("srsearch" . ,query)
|
|
|
|
("formatversion" . "2")
|
|
|
|
("format" . "json")))))
|
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; simultaneously get the search results from the fandom API, as well as information about the wiki as a whole (its license, icon, name)
|
2023-02-12 00:55:33 +00:00
|
|
|
(define-values (dest-res siteinfo)
|
|
|
|
(thread-values
|
|
|
|
(λ ()
|
|
|
|
(log-outgoing dest-url)
|
2023-05-27 11:48:08 +00:00
|
|
|
(easy:get dest-url #:timeouts timeouts)) ;; HTTP request to dest-url for search results
|
2023-02-12 00:55:33 +00:00
|
|
|
(λ ()
|
2023-05-27 11:48:08 +00:00
|
|
|
(siteinfo-fetch wikiname)))) ;; helper function in another file to get information about the wiki
|
2022-08-23 09:57:42 +00:00
|
|
|
|
2023-05-27 11:48:08 +00:00
|
|
|
;; search results are a JSON string. parse JSON into racket data structures
|
2023-02-12 00:55:33 +00:00
|
|
|
(define data (easy:response-json dest-res))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; calling my generate-results-page function with the information so far in order to get a big fat x-expression
|
|
|
|
;; big fat x-expression goes into the body variable
|
2023-02-12 00:55:33 +00:00
|
|
|
(define body (generate-results-page req dest-url wikiname query data #:siteinfo siteinfo))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; error checking
|
2023-02-12 00:55:33 +00:00
|
|
|
(when (config-true? 'debug)
|
|
|
|
; used for its side effects
|
|
|
|
; convert to string with error checking, error will be raised if xexp is invalid
|
|
|
|
(xexp->html body))
|
2023-05-27 11:48:08 +00:00
|
|
|
;; convert body to HTML and send to browser
|
2023-02-12 00:55:33 +00:00
|
|
|
(response/output
|
|
|
|
#:code 200
|
|
|
|
#:headers (build-headers always-headers)
|
|
|
|
(λ (out)
|
|
|
|
(write-html body out)))))
|
2022-08-23 09:57:42 +00:00
|
|
|
(module+ test
|
2023-02-05 04:56:15 +00:00
|
|
|
(parameterize ([(config-parameter 'feature_offline::only) "false"])
|
|
|
|
(check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Gacha_Capsule")
|
|
|
|
(generate-results-page test-req "" "test" "Gacha" search-json-data))))))
|