google-search: library for searching google

darcs
chris.double 2006-12-14 13:36:40 +00:00
parent f23ba12c95
commit 49b85d2525
3 changed files with 112 additions and 0 deletions

View File

@ -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
[
"<?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 ;

View File

@ -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" } ;

View File

@ -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" }
} ;