PageRenderTime 163ms CodeModel.GetById 81ms app.highlight 9ms RepoModel.GetById 71ms app.codeStats 0ms

/duckduckgo/duckduckgo.factor

http://github.com/mrjbq7/re-factor
Unknown | 81 lines | 67 code | 14 blank | 0 comment | 0 complexity | 7929c79029780271e32eecfc7c4fac4b MD5 | raw file
 1! Copyright (C) 2013 John Benediktsson
 2! See http://factorcode.org/license.txt for BSD license
 3
 4USING: assocs combinators http.client json.reader kernel
 5sequences urls ;
 6
 7IN: duckduckgo
 8
 9<PRIVATE
10
11: search-url ( query -- url )
12    URL" http://api.duckduckgo.com"
13        swap "q" set-query-param
14        "json" "format" set-query-param
15        "1" "pretty" set-query-param
16        "1" "no_redirect" set-query-param
17        "1" "no_html" set-query-param
18        "1" "skip_disambig" set-query-param ;
19
20TUPLE: abstract html text url source heading ;
21TUPLE: answer text type url ;
22TUPLE: result html text url ;
23TUPLE: redirect url ;
24TUPLE: definition text url source ;
25TUPLE: results type image answer result related-topics abstract
26definition redirect ;
27
28: >abstract ( json -- abstract )
29    {
30        [ "Abstract" of ]
31        [ "AbstractText" of ]
32        [ "AbstractURL" of ]
33        [ "AbstractSource" of ]
34        [ "Heading" of ]
35    } cleave abstract boa ;
36
37: >answer ( json -- answer )
38    [ "Answer" of ]
39    [ "AnswerType" of ] bi f answer boa ;
40
41: >definition ( json -- definition )
42    [ "Definition" of ]
43    [ "DefinitionURL" of ]
44    [ "DefinitionSource" of ] tri definition boa ;
45
46: >redirect ( json -- redirect )
47    "Redirect" of redirect boa ;
48
49: >result ( json -- result )
50    [ "Result" of ]
51    [ "Text" of ]
52    [ "FirstURL" of ] tri result boa ;
53
54SYMBOLS: +article+ +disambiguation+ +category+ +name+
55+exclusive+ +nothing+ ;
56
57: >results ( json -- results )
58    {
59        [
60            "Type" of H{
61                { "A" +article+ }
62                { "D" +disambiguation+ }
63                { "C" +category+ }
64                { "N" +name+ }
65                { "E" +exclusive+ }
66                { "" +nothing+ }
67            } at
68        ]
69        [ "Image" of ]
70        [ >answer ]
71        [ "Results" of [ >result ] map ]
72        [ "RelatedTopics" of [ >result ] map ]
73        [ >abstract ]
74        [ >definition ]
75        [ >redirect ]
76    } cleave results boa ;
77
78PRIVATE>
79
80: search ( query -- results )
81    search-url http-get nip "" like json> >results ;