-
Notifications
You must be signed in to change notification settings - Fork 13
/
duckduckgo.factor
57 lines (47 loc) · 1.63 KB
/
duckduckgo.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
! Copyright (C) 2013 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: ascii assocs colors combinators html.parser
html.parser.printer http.client images.http io
io.encodings.string io.encodings.utf8 io.styles json kernel make
sequences splitting urls wrap.strings ;
IN: duckduckgo
<PRIVATE
: duckduckgo-url ( query -- url )
URL" http://api.duckduckgo.com"
swap "q" set-query-param
"json" "format" set-query-param
"1" "pretty" set-query-param
"1" "no_redirect" set-query-param
"1" "no_html" set-query-param
"1" "skip_disambig" set-query-param ;
: write-link ( title url -- )
'[
_ presented ,,
COLOR: blue foreground ,,
] H{ } make format ;
: result. ( result -- )
"Result" of [
"<a href=\"" ?head drop "\">" split1 "</a>" split1
[ swap >url write-object nl ]
[ parse-html html-text ] bi*
[ blank? ] trim-head "- " ?head drop
[ 78 wrap-string print ] unless-empty nl
] when* ;
: abstract. ( results -- )
dup "Heading" of [ drop ] [
swap {
[ "AbstractURL" of >url write-object nl ]
[ "AbstractText" of 78 wrap-string print ]
[ "AbstractSource" of "- " write print ]
} cleave nl
] if-empty ;
PRIVATE>
: duckduckgo ( query -- results )
duckduckgo-url http-get nip utf8 decode json> ;
: duckduckgo. ( query -- )
duckduckgo {
[ "Image" of [ "https://duckduckgo.com" prepend http-image. ] unless-empty ]
[ abstract. ]
[ "Results" of [ result. ] each ]
[ "RelatedTopics" of [ result. ] each ]
} cleave ;