summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmaan Bhojwani <me@armaanb.net>2021-11-27 20:28:15 -0500
committerArmaan Bhojwani <me@armaanb.net>2021-11-27 22:49:01 -0500
commita0536c3fe9b8690ba944a4947df2bf6f8d7eefa7 (patch)
tree586ebb7e18fd9c85085565bbeb0fdfaec12b2226
parentfe973842759bbe6c13952e53f9ae9775454d8b35 (diff)
downloadbettersearch-a0536c3fe9b8690ba944a4947df2bf6f8d7eefa7.tar.gz
Implement a working prototype
-rw-r--r--bettersearch.rkt41
-rw-r--r--blacklist.rkt1
-rw-r--r--index.html13
-rw-r--r--search.html25
-rw-r--r--server.rkt72
-rw-r--r--static/styles.css9
6 files changed, 104 insertions, 57 deletions
diff --git a/bettersearch.rkt b/bettersearch.rkt
deleted file mode 100644
index f3acf79..0000000
--- a/bettersearch.rkt
+++ /dev/null
@@ -1,41 +0,0 @@
-#lang racket
-
-(require json)
-(require net/url)
-
-(require "blacklist.rkt")
-
-(define (member-match? itm lst)
- (ormap (lambda (i)
- (regexp-match? (regexp i) itm))
- lst))
-
-(define (get-results query)
-(define engine (string->url
- "https://search.trom.tf/search?q=facebook&format=json"))
-(define response (get-pure-port engine))
-(define json-raw (port->string response))
-(close-input-port response)
-
-(define json
- (with-input-from-string
- json-raw
- (lambda ()
- (read-json))))
-
-; Parse and filter results
-; TODO: replace this with tail-call recursion and return instead of printing
-(for-each (lambda (i)
- (define result-title (hash-ref i 'title ""))
- (define result-url (url-host (string->url (hash-ref i 'url ""))))
- (define result-content (hash-ref i 'content ""))
-
- (unless (member-match? result-url blacklist)
- (writeln result-title)
- (writeln result-url)
- (writeln result-content)
- (writeln "")
-
- ))
- (hash-ref json 'results))
-)
diff --git a/blacklist.rkt b/blacklist.rkt
index 4374c06..cc3b7c2 100644
--- a/blacklist.rkt
+++ b/blacklist.rkt
@@ -25,5 +25,4 @@
"stackoverflow.com"
"superuser.com"
"tex-talk.net"
- "thesffblog.com"
))
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..0d401e9
--- /dev/null
+++ b/index.html
@@ -0,0 +1,13 @@
+<html>
+<head>
+ <link rel="stylesheet" type="text/css" href="/styles.css">
+</head>
+<body>
+<h1>Search</h1>
+ <form action="/search">
+ <label for="q">Search Query</label>
+ <input type="text" name="q" id="q">
+ <input type="submit">
+ </form>
+<body>
+</html>
diff --git a/search.html b/search.html
new file mode 100644
index 0000000..2662d28
--- /dev/null
+++ b/search.html
@@ -0,0 +1,25 @@
+<html>
+<head>
+ <link rel="stylesheet" type="text/css" href="/styles.css">
+</head>
+<body>
+<h1>Search results</h1>
+ <form action="/search">
+ <label for="q">Search Query</label>
+ <input type="text" name="q" id="q">
+ <input type="submit">
+ </form>
+<dl>
+@in[r results]{
+ <div class="result">
+ <dt>@(hash-ref r "title")</dt>
+ <dd>
+ <a href="@(hash-ref r "url")">@(hash-ref r "url")</a>
+ <br>
+ @(hash-ref r "content")
+ </dd>
+ </div>
+}
+</dl>
+<body>
+</html>
diff --git a/server.rkt b/server.rkt
index 7f7562f..81abf7c 100644
--- a/server.rkt
+++ b/server.rkt
@@ -1,35 +1,77 @@
#lang racket
-(require web-server/servlet)
+(require json)
+(require net/url)
(require web-server/formlets)
+(require web-server/servlet)
(require web-server/servlet-env)
+(require web-server/templates)
+
+(require "blacklist.rkt")
-(require "bettersearch.rkt")
+(define (member-match? itm lst)
+ (ormap (lambda (i) (regexp-match? (regexp i) itm)) lst))
(define-values (dispatch generate-url)
(dispatch-rules
- [("") do-index]
+ [("index") do-index]
[("search") do-search]
))
+(define (do-index req)
+ (http-response (include-template "index.html")))
+
+(define (search query)
+ (define engine (string->url
+ (string-append
+ "https://search.trom.tf/search?format=json&q=" query)))
+ (define response (get-pure-port engine))
+ (define json-raw (port->string response))
+ (close-input-port response)
+ (with-input-from-string json-raw (lambda () (read-json))))
+
+(define (http-response content) ; The 'content' parameter should be a string.
+ (response/full
+ 200 ; HTTP response code.
+ #"OK" ; HTTP response message.
+ (current-seconds) ; Timestamp.
+ TEXT/HTML-MIME-TYPE ; MIME type for content.
+ '() ; Additional HTTP headers.
+ (list ; Content (in bytes) to send to the browser.
+ (string->bytes/utf-8 content))))
+
(define (do-search req)
(define binds (request-bindings req))
- (define query (if
- (exists-binding? 'q binds)
+ (define query (if (exists-binding? 'q binds)
(extract-binding/single 'q binds)
""))
(if (non-empty-string? query)
- (response/xexpr `(html
- (h1 "Search results")
- (fetch-results query)
- ))
- (response/xexpr `(html
- (h1 "ho")
- ))
- )
- )
+ (let ()
+ (define results
+ (foldr cons '()
+ (filter hash?
+ (map (lambda (i)
+ (define result-host
+ (url-host (string->url
+ (hash-ref i 'url ""))))
+ (define result-url (hash-ref i 'url ""))
+ (define result-title (hash-ref i 'title ""))
+ (define result-content (hash-ref i 'content ""))
+ (define ht (make-hash))
+ (unless (member-match? result-host blacklist)
+ (let ()
+ (hash-set! ht "title" result-title)
+ (hash-set! ht "url" result-url)
+ (hash-set! ht "content" result-content)
+ )
+ ht
+ ))
+ (hash-ref (search query) 'results)))))
+ (http-response (include-template "search.html")))
+ (do-index req)))
(serve/servlet dispatch
#:command-line? #t
- #:servlet-regexp #rx"")
+ #:servlet-regexp #rx""
+ #:extra-files-paths (list (build-path "./static")))
diff --git a/static/styles.css b/static/styles.css
new file mode 100644
index 0000000..c0bbd45
--- /dev/null
+++ b/static/styles.css
@@ -0,0 +1,9 @@
+body {
+ max-width: 1000px;
+}
+
+.result {
+ background: #eee;
+ margin: 0.5em 0;
+ padding: 0.25em;
+}