1 ! Copyright (C) 2006 Daniel Ehrenberg, Walton Chan
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: http.client xml xml.utilities kernel sequences
4 math.parser urls accessors locals ;
7 TUPLE: result title url summary ;
11 TUPLE: search query results adult-ok start appid region type
12 format similar-ok language country site subscription license ;
14 : parse-yahoo ( xml -- seq )
15 "Result" deep-tags-named [
16 { "Title" "Url" "Summary" }
17 [ tag-named children>string ] with map
21 : yahoo-url ( -- str )
22 URL" http://search.yahooapis.com/WebSearchService/V1/webSearch" ;
24 :: param ( search url name quot -- search url )
25 search url search quot call
26 [ name set-query-param ] when* ; inline
28 : num-param ( search str quot -- search )
29 [ dup [ number>string ] when ] compose param ; inline
31 : bool-param ( search str quot -- search )
32 [ "1" and ] compose param ; inline
34 : query ( search -- url )
36 "appid" [ appid>> ] param
37 "query" [ query>> ] param
38 "region" [ region>> ] param
39 "type" [ type>> ] param
40 "format" [ format>> ] param
41 "language" [ language>> ] param
42 "country" [ country>> ] param
43 "site" [ site>> ] param
44 "subscription" [ subscription>> ] param
45 "license" [ license>> ] param
46 "results" [ results>> ] num-param
47 "start" [ start>> ] num-param
48 "adult_ok" [ adult-ok>> ] bool-param
49 "similar_ok" [ similar-ok>> ] bool-param
53 "fRrVAKzV34GDyeRw6bUHDhEWHRedwfOC7e61wwXZLgGF80E67spxdQXuugBe2pgIevMmKwA-" ;
55 : <search> ( query -- search )
61 : search-yahoo ( search -- seq )
62 query http-get nip string>xml parse-yahoo ;