4 (require web-server/servlet)
5 (require web-server/servlet-env)
6 (require web-server/templates)
8 (require "blacklist.rkt")
10 (define (member-match? itm lst)
11 (ormap (lambda (i) (regexp-match? (regexp i) itm)) lst))
13 (define-values (dispatch generate-url)
16 [("search") do-search]
19 (define (do-head title)
20 (include-template "templates/head.html"))
23 (include-template "templates/footer.html"))
25 (define (do-index req)
26 (http-response (string-append
27 (do-head "Web Search")
28 (include-template "templates/index.html")
31 (define (search query pageno)
32 (define engine (string->url
34 "https://search.trom.tf/search?format=json&q="
39 (define response (get-pure-port engine))
40 (define json-raw (port->string response))
41 (close-input-port response)
42 (with-input-from-string json-raw (lambda () (read-json))))
44 (define (http-response content)
52 (string->bytes/utf-8 content))))
54 (define (do-search req)
55 (define binds (request-bindings req))
56 (define query (if (exists-binding? 'q binds)
57 (extract-binding/single 'q binds)
60 (if (non-empty-string? query)
62 (define pageno (if (exists-binding? 'pageno binds)
63 (extract-binding/single 'pageno binds)
70 (url-host (string->url
71 (hash-ref i 'url ""))))
72 (define result-url (hash-ref i 'url ""))
73 (define result-title (hash-ref i 'title ""))
74 (define result-content (hash-ref i 'content ""))
75 (define ht (make-hash))
76 (unless (member-match? result-host blacklist)
78 (hash-set! ht "title" result-title)
79 (hash-set! ht "url" result-url)
80 (hash-set! ht "content" result-content)
84 (hash-ref (search query pageno) 'results)))))
85 (http-response (string-append
86 (do-head (string-append query " | Web Search"))
87 (include-template "templates/search.html")
91 (serve/servlet dispatch
93 #:servlet-regexp #rx""
94 #:extra-files-paths (list (build-path "./static")))