Version 1 of Google AJAX search API

Updated 2008-01-23 16:32:16 by LV

jdc 23-jan-2008 This example shows how to use the Google AJAX search API from Tcl. You will need a Google AJAX Search API Key which you can get here .

set api_key "<your google ajax api here>"
set query $argv

package require http
package require json

set url "http://google.com/uds/GwebSearch?"
append url [::http::formatQuery callback GwebSearch.RawCompletion context 0 lstkp 0 rsz large hl en sig $api_key q $query key  internal v 1.0 nocache 7]

set t [http::geturl $url]

if { [http::ncode $t] == 200 } {
    set d [http::data $t]
    set idx 0
    while { $idx >= 0 } {
	set idx [string first {GsearchResultClass} $d $idx]
	if { $idx >= 0 } { 
	    set jd [json::json2dict [string range $d [expr {$idx-2}] end]]
	    puts "[dict get $jd url] [dict get $jd titleNoFormatting]"
	    incr idx 5
	}
    }
} else {
    puts "Error=[http::error $t]"
    puts "Status=[http::status $t]"
    puts "Code=[http::code $t]"
    puts "Ncode=[http::ncode $t]"
    puts "Data=[http::data $t]"
}

http::cleanup $t
exit

Example searching this site for tcltest:

% tclsh test.tcl tcltest site::http://wiki.tcl.tk
http://wiki.tcl.tk/1502 tcltest
http://wiki.tcl.tk/3882 How to write tcltest result values
http://wiki.tcl.tk/13235 tcltest customMatch
http://wiki.tcl.tk/14465 Your first tcltests
http://wiki.tcl.tk/8235 How to mimic event generation without TK
http://wiki.tcl.tk/11263 examples needed
http://wiki.tcl.tk/1318 Tcl and LISP
http://wiki.tcl.tk/9989 Toplevel widgets in a tree hierarchy: the test suite

See also json