1 ! Copyright (C) 2011 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays assocs assocs.extras classes.tuple
5 colors.constants combinators formatting fry http.client io
6 io.styles json.reader kernel sequences urls wrap.strings ;
12 : search-url ( query -- url )
13 URL" http://ajax.googleapis.com/ajax/services/search/web"
14 "1.0" "v" set-query-param
15 swap "q" set-query-param
16 "8" "rsz" set-query-param
17 "0" "start" set-query-param ;
19 TUPLE: search-result cacheUrl GsearchResultClass visibleUrl
20 title content unescapedUrl url titleNoFormatting fileFormat ;
24 : google-search ( query -- results )
25 search-url http-get nip json>
26 { "responseData" "results" } deep-at
27 [ \ search-result from-slots ] map ;
31 : write-heading ( str -- )
34 { background COLOR: light-gray }
37 : write-title ( str -- )
39 { foreground COLOR: blue }
42 : write-content ( str -- )
43 60 wrap-string print ;
45 : write-url ( str -- )
47 { font-name "monospace" }
48 { foreground COLOR: dark-green }
49 } [ write-object ] with-style nl ;
53 : google-search. ( query -- )
54 [ "Search results for '%s'" sprintf write-heading nl ]
55 [ google-search ] bi [
57 [ titleNoFormatting>> write-title ]
58 [ content>> write-content ]
59 [ unescapedUrl>> write-url ]