The programmatic hacks in this book run either on the command line (that's Terminal for Mac OS X folk, DOS command window for Windows users) or as CGI scriptsdynamic pages living on your web site, accessed through your web browser.
Running a hack on the command line invariably involves the following steps:
Alternatively, you can download the code for all of the hacks online at http://www.oreilly.com/catalog/googlehks2, a ZIP archive filled with individual scripts already saved as text files.
Get to the command line on your computer or remote server. In Mac OS
X, launch the Terminal
(Applications
Navigate to where you saved the script at hand. This varies from operating system to operating system, but usually involves something like cd ~/Desktop (that's your Desktop on the Mac).
Invoke the script by running the programming language's interpreter (e.g., Perl) and feeding it the script (e.g., scriptname.pl) like so:
$ perl scriptname.pl Most often, you'll also need to pass along some parametersyour search query, the number of results you'd like, and so forth. Simply drop them in after the script name, enclosing them in quotes if they're more than one word or if they include an odd character or three:
$ perl scriptname.pl '"much ado about nothing" script' 10 The results of your script are almost always sent straight back to the command-line window in which you're working, like so:
$ perl scriptname.pl '"much ado about nothing" script' 10 1. "Amazon.com: Books: Much Ado About Nothing: Screenplay ..." [http://www.amazon.com/exec/obidos/tg/detail/-/0393311112?v=glance] 2. "Much Ado About Nothing Script" [http://www.signal42.com/much_ado_about_nothing_script.asp] ...
To stop output scrolling off your screen faster than you can read it, on most systems you can "pipe" (read: redirect) the output to a little program called more:
$ perl scriptname.pl | more Hit the Enter/Return key on your keyboard to scroll through line by line, the space bar to leap through page by page.
You'll also sometimes want to direct output to a file for safekeeping, importing into your spreadsheet application, or displaying on your web site. This is as easy; refer to the code shown next.
$ perl scriptname.pl > output_filename.txt And to pour some input into your script from a file, simply do the opposite:
$ perl scriptname.pl < input_filename.txt
CGI scriptsprograms that run on your web site and produce pages dynamicallyare a little more complicated if you're not used to them. While fundamentally they're the same sort of scripts as those run on the command line, they are more troublesome because setups vary so widely. You may be running your own server, your web site may be hosted on an Internet service provider's (ISP) server, your content may live on a corporate intranet serveror anything in between.
Since going through every possibility is beyond the scope of this (or any) book, you should check your ISP's knowledge base or call their technical support department, or ask your local system administrator for help.
Generally, though, the methodology is the same:
Alternatively, you can download the code for all of the hacks online at http://www.oreilly.com/catalog/googlehks2, a ZIP archive filled with individual scripts already saved as text files.
Move the script over to wherever your web site lives. You should have some directory on a server somewhere in which all of your web pages (all those l files) and images (ending in .jpg, .gif, etc.) live. Within this directory, you'll probably see something called a cgi-bin directory: this is where CGI scripts must usually live in order to be run rather than just displayed in your web browser when you visit them.
You usually need to bless CGI scripts as executableto be run rather than displayed. Just how you do this depends on the operating system of your server. If you're on a Unix/Linux or Mac OS X system, this usually entails typing the following on the command line:
$ chmod 755 scriptname.cgi Now you should be able to point your web browser at the script and have it run as expected, behaving in a manner similar to that described in the "Running the Hack" section of the hack at hand.
Just what URL you use once again varies wildly. It should, however, look something like this:
http://www.your_domain.com/cgi-bin/scriptname.cgi , where your_domain.com is your web site domain, cgi-bin refers to the directory in which your CGI scripts live, and scriptname.cgi is the script itself.
If you don't have a domain and are hosted at an ISP, the URL is more likely to look like this:
http://www.your_isp.com/~your_username/cgi-bin/scriptname.cgi , where your_isp.com is your ISP's domain, ~ your_username is your username at the ISP, cgi-bin refers to the directory in which your CGI scripts live, and scriptname .cgi is the script itself.
Be sure to consult Chapter 9 for an introduction to the Google API, how to sign up for a developer's keyyou'll need one for many of the hacks in this bookand the basics of programming Google in a selection of languages to get you going.
Fancy trying your hand at a spot of programming? O'Reilly's best-selling Learning Perl (http://www.oreilly.com/catalog/lperl3) by Randal L. Schwartz and Tom Phoenix provides a good start. Apply what you learn to understanding and using the hacks in this book, perhaps even taking on the "Hacking the Hack" sections to tweak and fiddle with the scripts. This is a useful way to get a little programming under your belt if you're a searching nut, since it's always a little easier to learn how to program when you have a task to accomplish and existing code to leaf through.