--- /dev/null
+! 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
+ [
+ "<?xml version='1.0' encoding='UTF-8'?>" %
+ "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:xsd='http://www.w3.org/1999/XMLSchema'>" %
+ "<SOAP-ENV:Body>" %
+ "<ns1:doGoogleSearch xmlns:ns1='urn:GoogleSearch'" %
+ " SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" %
+ " <key xsi:type='xsd:string'>" %
+ swap %
+ "</key>" %
+ " <q xsi:type='xsd:string'>" %
+ %
+ "</q>" %
+ " <start xsi:type='xsd:int'>0</start>" %
+ " <maxResults xsi:type='xsd:int'>10</maxResults>" %
+ " <filter xsi:type='xsd:boolean'>true</filter>" %
+ " <restrict xsi:type='xsd:string'></restrict>" %
+ " <safeSearch xsi:type='xsd:boolean'>false</safeSearch>" %
+ " <lr xsi:type='xsd:string'></lr>" %
+ " <ie xsi:type='xsd:string'>latin1</ie>" %
+ " <oe xsi:type='xsd:string'>latin1</oe>" %
+ "</ns1:doGoogleSearch>" %
+ " </SOAP-ENV:Body>" %
+ "</SOAP-ENV:Envelope> " %
+ ] "" 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 <search-item>
+ ] 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 ;
--- /dev/null
+! Copyright (C) 2006 Chris Double\r
+! See http://factorcode.org/license.txt for BSD license.\r
+USING: help google-search ;\r
+\r
+HELP: build-soap-request\r
+{ $values { "key" "the Google Web API key" } { "string" "the Google search string" } { "soap" "the SOAP body for the request" } }\r
+{ $description \r
+ "Return a string containing the SOAP request to performthe results of a google search from the Google Web API."\r
+ "The given key must be the authentication key obtained from Google to use the API." } \r
+{ $see-also google-search } ;\r
+\r
+HELP: google-search\r
+{ $values { "key" "the Google Web API key" } { "string" "the Google search string" } { "result" "a sequence of search results" } }\r
+{ $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." } \r
+{ $see-also search-item-url search-item-snippet search-item-title } ;\r
+\r
+HELP: search-item-url\r
+{ $values { "item" "a search item" } { "url" "a string" } }\r
+{ $description "Return the URL of the given search item." } \r
+{ $see-also google-search search-item-snippet search-item-title } ;\r
+\r
+HELP: search-item-snippet\r
+{ $values { "item" "a search item" } { "snippet" "a string" } }\r
+{ $description "Return the snippet of the given search item." } \r
+{ $see-also google-search search-item-url search-item-title } ;\r
+\r
+HELP: search-item-title\r
+{ $values { "item" "a search item" } { "title" "a string" } }\r
+{ $description "Return the title of the given search item." } \r
+{ $see-also google-search search-item-snippet search-item-url } ;\r
+\r
+ARTICLE: { "google-search" "overview" } "Google Search"\r
+"The google-search library is used for querying the Google search engine using their "\r
+"SOAP based API. It requires a special key that used to be obtained from Google to use "\r
+"the service."\r
+$terpri\r
+"Unfortunately as of December 2006 the service has been deprecated and keys "\r
+"are no longer available. The service continues to be usable if you have (or can find) "\r
+"an existing key. I'll update this library when an alternative service becomes available."\r
+$terpri\r
+"An example of usage is:"\r
+{ $code "\"mygooglekey\" \"factor programming language\" google-search" }\r
+"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:"\r
+{ $subsection search-item-url }\r
+{ $subsection search-item-title }\r
+{ $subsection search-item-snippet }\r
+"For example:"\r
+{ $code "\"mygooglekey\" \"factor programming language\" google-search [ search-item-title ] map" } ;
\ No newline at end of file