Tuesday, April 16, 2002

Google API implementation

Here is an implementation of Google API in VBScript. I have modified the original source by David Watson. This code should run on any windows machine which has the Windows Scripting Host and Microsoft SOAP 2.0 Toolkit installed. You can run this script by saving it as a .vbs file and double clicking it or by issuing "csript <filename>" command at the command prompt.



REM Declare the variables
Dim soapClient, ResultCount, Results, i, key, query
Dim ResultElements, ResultCount
REM Create the object
set soapClient = createobject("MSSOAP.SoapClient")
on error resume next
REM Extract the WSDL file from the googleapi.zip in the same directory as this script file
soapClient.mssoapinit("GoogleSearch.wsdl")
key = "Your key here"
query="Your query here"
if err then
wscript.echo err.description
wscript.echo "Faultstring =" + SOAPClient.faultString
wscript.echo "Faultactor =" + SOAPClient.faultactor
wscript.echo "Faultcode =" + SOAPClient.faultcode
wscript.echo "Detail =" + SOAPClient.detail
end if
REM If you are connecting through a proxy server, Keep the following three lines
soapClient.ClientProperty("ServerHTTPRequest") = True
soapClient.ConnectorProperty("ProxyServer") = "Proxy"
soapClient.ConnectorProperty("ProxyPort") = 8080
set Results = soapClient.doGoogleSearch (key, query, 0, 3, False, "", False, "", "", "")
For i = 0 To Results.length - 1
If Results.Item(i).nodeName = "resultElements" Then
ResultElements = Results.Item(i).nodeTypedValue
Exit For
End If
Next
if err then
wscript.echo err.description
wscript.echo "Faultstring =" + SOAPClient.faultString
wscript.echo "Faultactor =" + SOAPClient.faultactor
wscript.echo "Faultcode =" + SOAPClient.faultcode
wscript.echo "Detail =" + SOAPClient.detail
else
wscript.echo ResultElements
end if


This should return you three(3) results of your query. This example can be extended to take care of other search responses. For example the following will return you the estimated total number of results that exist for the query.



For i = 0 To Results.length - 1
If Results.Item(i).nodeName =
"estimatedTotalResultsCount" Then
ResultCount = Results.Item(i).nodeTypedValue
Exit For
End If
Next
wscript.echo ResultCount

No comments: