diff --git a/libs/google-search/google-search.factor b/libs/google-search/google-search.factor new file mode 100644 index 0000000000..6c6bff363b --- /dev/null +++ b/libs/google-search/google-search.factor @@ -0,0 +1,47 @@ +! Copyright (C) 2006 Chris Double. All Rights Reserved. +! See http://factorcode.org/license.txt for BSD license. +! +USING: kernel http-client namespaces io errors sequences rss ; +IN: google-search + +: build-soap-request ( key string -- soap ) + #! Return the soap request for a google search + [ + "" % + "" % + "" % + "" % + " " % + swap % + "" % + " " % + % + "" % + " 0" % + " 10" % + " true" % + " " % + " false" % + " " % + " latin1" % + " latin1" % + "" % + " " % + " " % + ] "" make ; + +TUPLE: search-item url snippet title ; + +: parse-result ( string -- seq ) + "resultElements" swap between-tags "item" swap child-tags [ + [ "URL" swap between-tags ] keep + [ "snippet" swap between-tags ] keep + "title" swap between-tags + ] map ; + +: google-search ( key string -- result ) + #! Perform a google searching using the Google Web API + #! key and the search string. + build-soap-request "text/xml" swap "http://api.google.com/search/beta2" http-post + rot 200 = not [ 2drop "Google search failed." throw ] [ nip ] if parse-result ; diff --git a/libs/google-search/google-search.facts b/libs/google-search/google-search.facts new file mode 100644 index 0000000000..7f5ca359ec --- /dev/null +++ b/libs/google-search/google-search.facts @@ -0,0 +1,48 @@ +! Copyright (C) 2006 Chris Double +! See http://factorcode.org/license.txt for BSD license. +USING: help google-search ; + +HELP: build-soap-request +{ $values { "key" "the Google Web API key" } { "string" "the Google search string" } { "soap" "the SOAP body for the request" } } +{ $description + "Return a string containing the SOAP request to performthe results of a google search from the Google Web API." + "The given key must be the authentication key obtained from Google to use the API." } +{ $see-also google-search } ; + +HELP: google-search +{ $values { "key" "the Google Web API key" } { "string" "the Google search string" } { "result" "a sequence of search results" } } +{ $description "Request the results of a google search from the Google Web API. The given key must be the authentication key obtained from Google to use the API. The search results are returned as a sequence of " { $link search-item } " objects." } +{ $see-also search-item-url search-item-snippet search-item-title } ; + +HELP: search-item-url +{ $values { "item" "a search item" } { "url" "a string" } } +{ $description "Return the URL of the given search item." } +{ $see-also google-search search-item-snippet search-item-title } ; + +HELP: search-item-snippet +{ $values { "item" "a search item" } { "snippet" "a string" } } +{ $description "Return the snippet of the given search item." } +{ $see-also google-search search-item-url search-item-title } ; + +HELP: search-item-title +{ $values { "item" "a search item" } { "title" "a string" } } +{ $description "Return the title of the given search item." } +{ $see-also google-search search-item-snippet search-item-url } ; + +ARTICLE: { "google-search" "overview" } "Google Search" +"The google-search library is used for querying the Google search engine using their " +"SOAP based API. It requires a special key that used to be obtained from Google to use " +"the service." +$terpri +"Unfortunately as of December 2006 the service has been deprecated and keys " +"are no longer available. The service continues to be usable if you have (or can find) " +"an existing key. I'll update this library when an alternative service becomes available." +$terpri +"An example of usage is:" +{ $code "\"mygooglekey\" \"factor programming language\" google-search" } +"This returns a sequence of " { $link search-item } " objects. The following methods on this object can be used to get the results of the search:" +{ $subsection search-item-url } +{ $subsection search-item-title } +{ $subsection search-item-snippet } +"For example:" +{ $code "\"mygooglekey\" \"factor programming language\" google-search [ search-item-title ] map" } ; \ No newline at end of file diff --git a/libs/google-search/load.factor b/libs/google-search/load.factor new file mode 100644 index 0000000000..e2439c5c78 --- /dev/null +++ b/libs/google-search/load.factor @@ -0,0 +1,17 @@ +! Copyright (C) 2006 Chris Double. All Rights Reserved. +! See http://factorcode.org/license.txt for BSD license. +! +REQUIRES: libs/http-client apps/rss ; + +PROVIDE: libs/google-search +{ + +files+ { + "google-search.factor" + "google-search.facts" + } +} { + +tests+ { + } +} { + +help+ { "google-search" "overview" } +} ;