5 (require web-server/formlets)
6 (require web-server/servlet)
7 (require web-server/servlet-env)
8 (require web-server/templates)
10 (require "blacklist.rkt")
12 (define (member-match? itm lst)
13 (ormap (lambda (i) (regexp-match? (regexp i) itm)) lst))
15 (define-values (dispatch generate-url)
18 [("search") do-search]
21 (define (do-head title)
22 (include-template "templates/head.html"))
25 (include-template "templates/footer.html"))
27 (define (do-index req)
28 (http-response (string-append
29 (do-head "Web Search")
30 (include-template "templates/index.html")
33 (define (search query pageno)
34 (define engine (string->url
36 "https://search.trom.tf/search?format=json&q="
41 (define response (get-pure-port engine))
42 (define json-raw (port->string response))
43 (close-input-port response)
44 (with-input-from-string json-raw (lambda () (read-json))))
46 (define (http-response content)
54 (string->bytes/utf-8 content))))
56 (define (do-search req)
57 (define binds (request-bindings req))
58 (define query (if (exists-binding? 'q binds)
59 (extract-binding/single 'q binds)
62 (if (non-empty-string? query)
64 (define pageno (if (exists-binding? 'pageno binds)
65 (extract-binding/single 'pageno binds)
72 (url-host (string->url
73 (hash-ref i 'url ""))))
74 (define result-url (hash-ref i 'url ""))
75 (define result-title (hash-ref i 'title ""))
76 (define result-content (hash-ref i 'content ""))
77 (define ht (make-hash))
78 (unless (member-match? result-host blacklist)
80 (hash-set! ht "title" result-title)
81 (hash-set! ht "url" result-url)
82 (hash-set! ht "content" result-content)
86 (hash-ref (search query pageno) 'results)))))
87 (http-response (string-append
88 (do-head (string-append query " | Web Search"))
89 (include-template "templates/search.html")
93 (serve/servlet dispatch
95 #:servlet-regexp #rx""
96 #:extra-files-paths (list (build-path "./static")))