Version 0 of Googling with SOAP

Updated 2003-02-04 15:40:04

This demonstrates a simple accessor for the Google API using the TclSOAP package.

Try google spell "Larry Vriden" ;)


 # Google.tcl - Copyright (C) 2003 Pat Thoyts <[email protected]>
 #
 #
 # -------------------------------------------------------------------------
 # This software is distributed in the hope that it will be useful, but
 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 # or FITNESS FOR A PARTICULAR PURPOSE.  See the accompanying file `LICENSE'
 # for more details.
 # -------------------------------------------------------------------------
 #
 # @(#)$Id: 8322,v 1.1 2003-02-05 09:00:53 jcw Exp $

 package require SOAP
 package require uri

 # You need to register to use the Google SOAP API. You should put your key into
 # $HOME/.googlekey as
 source [file join $env(HOME) .googlekey]

 # -------------------------------------------------------------------------

 proc google {cmd args} {
     global Key
     switch -glob -- $cmd {
         se* {
             set r [GoogleSearchService::doGoogleSearch $Key [lindex $args 0]]
         }
         sp* {
             set r [GoogleSearchService::doSpellingSuggestion $Key [lindex $args 0]]
         }

         c* {
             set r [eval [list GoogleSearchService::doGetCachedPage $Key] $args]
         }
         default {
             usage
         }
     }
     return $r
 }

 proc usage {} {
     puts "usage: google search query"
     puts "       google spell text"
     puts "       google cache url"
     exit 1
 }

 proc set_useragent {{app {}}} {
     global tcl_platform
     set ua "Mozilla/4.0 ([string totitle $tcl_platform(platform)];\
         $tcl_platform(os)) http/[package provide http]"
     if {[string length $app] > 0} {
         append ua " " $app
     } else {
         append ua " Tcl/[package provide Tcl]"
     }
     http::config -useragent $ua
 }
 set_useragent "Google/1.0"

 # -------------------------------------------------------------------------
 # Setup the SOAP accessor methods
 # -------------------------------------------------------------------------
 if {[catch {package require SOAP::WSDL}]} {

     # User doesn't have the WSDL package,  do it manually
     # The following code was generated by parsing the WSDL document
     namespace eval GoogleSearchService {
         set endpoint http://api.google.com/search/beta2
         set schema http://www.w3.org/2001/XMLSchema
         SOAP::create doGetCachedPage \
             -proxy $endpoint -params {key string url string} \
             -action urn:GoogleSearchAction \
             -encoding http://schemas.xmlsoap.org/soap/encoding/ \
             -schema [list xsd $schema] \
             -uri urn:GoogleSearch
         SOAP::create doSpellingSuggestion \
             -proxy $endpoint -params {key string phrase string} \
             -action urn:GoogleSearchAction \
             -encoding http://schemas.xmlsoap.org/soap/encoding/ \
             -schema [list xsd $schema] \
             -uri urn:GoogleSearch
         SOAP::create doGoogleSearch -proxy $endpoint \
             -params {key string q string start int maxResults int \
                          filter boolean restrict string safeSearch boolean \
                          lr string ie string oe string} \
             -action urn:GoogleSearchAction \
             -encoding http://schemas.xmlsoap.org/soap/encoding/ \
             -schema [list xsd $schema] \
             -uri urn:GoogleSearch
     }; # end of GoogleSearchService

 } else {

     # Get the WSDL document (local copy)
     set url "file:[file join [file dirname [info script]] GoogleSearch.wsdl]"
     set data [uri::geturl $url]
     set wsdl [set [subst $data](data)]

     # Process the WSDL and generate Tcl script defining the SOAP accessors.
     # This is going to change in the near future.
     set doc  [dom::DOMImplementation parse $wsdl]
     set impl [SOAP::WSDL::parse $doc]
     eval [set $impl]

     # Fixup the parameters (the rpcvar package needs to be enhanced for this
     # but this hasn't been done yet)
     set schema {http://www.w3.org/2001/XMLSchema}
     foreach cmd [info commands ::GoogleSearchService::*] {
         set fixed {}
         foreach {param type} [SOAP::cget $cmd -params] {
             set type [regsub "${schema}:" $type {}]
             lappend fixed $param $type
         }
         SOAP::configure $cmd -params $fixed -schemas [list xsd $schema]
     }

 }

 # -------------------------------------------------------------------------

 # Make available as a command line script.
 if {!$::tcl_interactive} {
     if {[info command GoogleSearchService::doGoogleSearch] != {}} {
         if {[llength $argv] < 2} {
             usage
         }
         set r [eval [list google] $args]
         puts $r
     }
 }

 # -------------------------------------------------------------------------
 # Local variables:
 #   mode: tcl
 #   indent-tabs-mode: nil
 # End:

[ Category Internet | Category Application ]