[Xotcl] google webservice interface in xotcl
Gustaf Neumann
neumann at wu-wien.ac.at
Wed Mar 23 20:07:24 CET 2005
Dear XOTcl Community,
some of you might have a use of the simple Google query API in XOTcl.
enjoy.
-gustaf
###################################################
# Simple Google query interface in XOTcl
# This script submits a SOAP query to the Google
# web service query interface
#
# -gustaf neumann, Jan. 2005
###################################################
package req XOTcl; namespace import -force ::xotcl::*
package req tdom
set LICENSE_KEY <****INSERT_YOUR_LICENCE_KEY_HERE*****>
Class GoogleQuery -parameter {
{key $::LICENSE_KEY}
{q "shrdlu winograd maclisp teletype"}
{start 0}
{maxResults 10}
{restrict ""}
{safeSearch false}
{lr ""}
{ie "latin1"}
{oe "latin1"}
} -set template \
{<?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">[my key]</key>
<q xsi:type="xsd:string">[my q]</q>
<start xsi:type="xsd:int">[my start]</start>
<maxResults xsi:type="xsd:int">[my maxResults]</maxResults>
<filter xsi:type="xsd:boolean">true</filter>
<restrict xsi:type="xsd:string">[my restrict]</restrict>
<safeSearch xsi:type="xsd:boolean">[my safeSearch]</safeSearch>
<lr xsi:type="xsd:string"[my lr]></lr>
<ie xsi:type="xsd:string">[my ie]</ie>
<oe xsi:type="xsd:string">[my oe]</oe>
</ns1:doGoogleSearch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>}
GoogleQuery instproc query {} {
set SOAPmsg [subst [[self class] set template]]
set S [socket api.google.com 80]
puts $S "POST /search/beta2 HTTP/1.0"
puts $S "Host: api.google.com"
puts $S "Content-Length: [string length $SOAPmsg]"
puts $S "Content-Type: text/xml"
puts $S ""
fconfigure $S -translation binary
puts -nonewline $S $SOAPmsg
flush $S
set result [read $S]
close $S
set F [open OUT w]; puts $F $result; close $F
return [string range $result \
[expr {[string first \r\n\r\n $result]+4}] end]
}
GoogleQuery instproc init {} {
set result [my query]
set doc [dom parse $result]
set root [$doc documentElement]
puts "[llength [$root selectNodes //snippet]] snippets found"
foreach item [$root selectNodes //resultElements/item] {
Answer new -childof [self] \
-URL [[$item selectNodes URL] text] \
-title [[$item selectNodes title] text] \
-chachedSize [[$item selectNodes cachedSize] text] \
-snippet [[$item selectNodes snippet] text]
}
}
Class Answer -parameter {URL title chachedSize snippet}
set G [GoogleQuery new -q xotcl]
foreach a [$G info children] {
foreach e {URL title chachedSize snippet} {puts "$e: [$a $e]"}
puts ""
}
More information about the Xotcl
mailing list