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
exitExample searching this site for tcltest:
% tclsh test.tcl tcltest site::https://wiki.tcl-lang.org https://wiki.tcl-lang.org/1502 tcltest https://wiki.tcl-lang.org/3882 How to write tcltest result values https://wiki.tcl-lang.org/13235 tcltest customMatch https://wiki.tcl-lang.org/14465 Your first tcltests https://wiki.tcl-lang.org/8235 How to mimic event generation without TK https://wiki.tcl-lang.org/11263 examples needed https://wiki.tcl-lang.org/1318 Tcl and LISP https://wiki.tcl-lang.org/9989 Toplevel widgets in a tree hierarchy: the test suite
See also json
jdc 24-jan-2008 The following piece of html contains some javascript to use the Google AJAX API to query this wiki. Again you have to provide a Google AJAX Search API Key (put it where there now is ##########). At most 32 search results can be obtained with this API.
<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi?key=##########"></script>
<script type="text/javascript">
google.load("search", "1");
function App(query) {
this.siteSearch = new GwebSearch();
this.siteSearch.setUserDefinedLabel("Tcler's wiki");
this.siteSearch.setUserDefinedClassSuffix("siteSearch");
this.siteSearch.setSiteRestriction("https://wiki.tcl-lang.org");
this.siteSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
this.siteSearch.setSearchCompleteCallback(this, App.prototype.OnSearchComplete);
document.getElementById("searchresult").innerHTML = "<ul>";
this.siteSearch.execute(query);
document.getElementById("searchresult").innerHTML += "</ul>";
document.getElementById("searchresult").innerHTML += "<form action='http://www.google.com/search'>";
document.getElementById("searchresult").innerHTML += "<input type='test' id='q' name='q' size='20' value='" + query + " site:https://wiki.tcl-lang.org'/>";
document.getElementById("searchresult").innerHTML += "<input type='submit' value='All matches on Google' />";
document.getElementById("searchresult").innerHTML += "</form>";
}
App.prototype.OnSearchComplete = function() {
if (this.siteSearch.results && this.siteSearch.results.length > 0) {
for (var i = 0; i < this.siteSearch.results.length; i++) {
var result = this.siteSearch.results[i];
if (result.GsearchResultClass == GwebSearch.RESULT_CLASS) {
document.getElementById("searchresult").innerHTML += "<li><a href='" + result.url + "'>" + result.title + "</a><p>" + result.content + "</p></li>";
}
}
var cursor = this.siteSearch.cursor;
if (cursor && cursor.currentPageIndex < cursor.pages.length - 1) {
this.siteSearch.gotoPage(cursor.currentPageIndex + 1);
}
}
}
function googleQuery() {
var app = new App(document.getElementById("searchstring").value);
return false;
}
</script>
</head>
<body>
<form action="" method="get" onSubmit="return googleQuery();">
<input id="searchstring" size="20" />
<input type="button" value="Click Here" onClick="googleQuery();" />
</form>
<div id="searchresult"></div>
</body>
</html>