O'Reilly Network    
 Published on O'Reilly Network (http://www.oreillynet.com/)
 See this if you're having trouble printing code examples

O'Reilly Book Excerpts: Google Hacks, 2nd Edition

Hacking Google

by Rael Dornfest, Tara Calishain
SEO + Web Traffic = Money

Editor's note: With access to more than three million documents in over 30 languages, Google is a researcher's dream. But like any invaluable tool, knowing the insider tricks of the trade is a must to save time and needless effort. Tara Calishain and Rael Dornfest, authors of Google Hacks, 2nd Edition, have set out to educate the masses to the ins and outs of Google. In today's excerpt, they offer the inside scoop on scattersearching, cartography, Google on the go, gmail-lite, and AdSense. With over 150 million Google searches conducted every day, why be just a number?


Scattersearch with Yahoo! and Google

Sometimes, illuminating results can be found when scraping from one site and feeding the results into the API of another. With scattersearching, you can narrow down the most popular related results, as suggested by Yahoo! and Google.

We've combined a scrape of a Yahoo! web page with a Google search[Hack #41], blending scraped data with data generated via a web service API to good effect. In this hack, we're doing something similar, except this time we're taking the results of a Yahoo! search and blending it with a Google search.

Yahoo! has a "Related searches" feature, where you enter a search term and get a list of related terms under the search box, if any are available. This hack scrapes those related terms and performs a Google search for the related terms in the title. It then returns the count for those searches, along with a direct link to the results. Aside from showing how scraped and API-generated data can live together in harmony, this hack is good to use when you're exploring concepts; for example, you might know that something called Pokemon exists, but you might not know anything about it. You'll get Yahoo!'s related searches and an idea of how many results each of those searches generates in Google. From there, you can choose the search terms that generate the most results or look the most promising based on your limited knowledge, or you can simply pick a road that appears less traveled.

The Code

Save the following code to a file called scattersearch.pl.

TIP: Bear in mind that this hack, while using the Google API for the Google portion, involves some scraping of Yahoo!'s search pages and thus is rather brittle. If it stops working at any point, take a gander at the regular expressions for they're almost sure to be the breakage point.

#!/usr/bin/perl -w
# Scattersearch -- Use the search suggestions from
# Yahoo! to build a series of intitle: searches at Google. 
use strict;
use LWP;
use SOAP::Lite;
use CGI qw/:standard/;
# Get our query, else die miserably.
my $query = shift @ARGV; die unless $query;
# Your Google API developer's key.
my $google_key = 'insert key here';
# Location of the GoogleSearch WSDL file.
my $google_wdsl = "./GoogleSearch.wsdl";
# Search Yahoo! for the query.
my $ua  = LWP::UserAgent->new;
my $url = URI->new('http://search.yahoo.com/search');
$url->query_form(rs => "more", p => $query);
my $yahoosearch = $ua->get($url)->content;
$yahoosearch =~ s/[\f\t\n\r]//isg;
# And determine if there were any results.
$yahoosearch =~ m!Also try:(.*?)  !migs;
die "Sorry, there were no results!\n" unless $1;
my $recommended = $1;

# Now, add all our results into
# an array for Google processing.
my @googlequeries;
while ($recommended =~ m!<a href=".*?">(.*?)</a>!mgis) {
    my $searchitem = $1; 
    $searchitem =~ s/nobr|<[^>]*>|\///g;
    print "$searchitem\n";
    push (@googlequeries, $searchitem);

# Print our header for the results page.
print join "\n",
     h1("Your Scattersearch Results"),
     p("Your original search term was '$query'"),
     p("That search had " . scalar(@googlequeries). " recommended terms."),
     p("Here are result numbers from a Google search"),
     CGI::start_ol( );
# Create our Google object for API searches.
my $gsrch = SOAP::Lite->service("file:$google_wdsl");
# Running the actual Google queries.
foreach my $googlesearch (@googlequeries) {
    my $titlesearch = "allintitle:$googlesearch"; 
    my $count = $gsrch->doGoogleSearch($google_key, $titlesearch,
                                       0, 1, "false", "",  "false",
                                       "", "", "");
    my $url = $googlesearch; $url =~ s/ /+/g; $url =~ s/\"/%22/g;
    print li("There were $count->{estimatedTotalResultsCount} ".
             "results for the recommended search <a href=\"http://www.".
print CGI::end_ol( ), end_html;

Running the Hack

This script generates an HTML file, ready for you to upload to a publicly accessible web site. If you want to save the output of a search for siamese to a file called scattersearch.html in your Sites directory, run the following command ["How to Run the Hacks" in the Preface]:

% perl scattersearch.pl "siamese" > ~/Sites/scattersearch.html

Your final results, as rendered by your browser, will look similar to Figure 2-15.

Figure 2-15
Figure 2-15. Scattersearch results for siamese

You'll have to do a little experimenting to find out which terms have related searches. Broadly speaking, very general search terms are bad; it's better to zero in on terms that people would search for and that would be easy to group together. At the time of this writing, for example, heart has no related search terms, but blood pressure does.

Hacking the Hack

You have two choices: you can either hack the interaction with Yahoo! or expand it to include something in addition to or instead of Yahoo! itself. Let's look at Yahoo! first. If you take a close look at the code, you'll see we're passing an unusual parameter to our Yahoo! search results page:

$url->query_form(rs => "more", p => $query);

The rs=>"more" part of the search shows the related search terms. Getting the related search this way will show up to 10 results. If you remove that portion of the code, you'll get roughly four related searches when they're available. That might suit you if you want only a few, but perhaps you want dozens and dozens! In that case, replace more with all.

Beware, though: this can generate a lot of related searches, and it can certainly eat up your daily allowance of Google API requests. Tread carefully.

Kevin Hemenway and Tara Calishain

Search Engine Optimization

Essential Reading

Search Engine Optimization
Building Traffic and Making Money with SEO
By Harold Davis

SEO--short for Search Engine Optimization--is the art, craft, and science of driving web traffic to web sites.

Web traffic is food, drink, and oxygen--in short, life itself--to any web-based business. Whether your web site depends on broad, general traffic, or high-quality, targeted traffic, this PDF has the tools and information you need to draw more traffic to your site. You'll learn how to effectively use PageRank (and Google itself); how to get listed, get links, and get syndicated; SEO best practices; and much more.

When you approach SEO, you must take some time to understand the characteristics of the traffic that you need to drive your business. Then go out and use the techniques explained in this PDF to grab some traffic--and bring life to your business.

Read Online--Safari
Search this book on Safari:

Code Fragments only


Google Cartography: Street Art in Your Neighborhood

car·tog·ra·phy n. The art or technique of making maps or charts.

Google Cartography (found here: http://richard.jones.name/google-hacks/google-cartography/google-cartography.html) uses Google via the Google Search API [Chapter 9] to build a visual representation of the interconnectivity of streets in an area.

This application takes a starting street and finds streets that intersect with it. Traversing the streets in a breadth-first manner, the application discovers more and more intersections, eventually producing a graph that shows the interconnectivity of streets flowing from the starting street.

Figures Figure 3-5 and Figure 3-6 show maps generated for two of the world's great cities, New York and Melbourne, respectively.

Figure 3-5
Figure 3-5. New York, U.S., as determined by Google cartography

Figure 3-6
Figure 3-6. Melbourne, Australia, mapped by a little Google cartography

TIP: If you know the streets in the areas shown, you will be able to find inconsistencies introduced by the text parsing process, explained in more detail in the following section.

The Gory Details

Google Cartography uses the Google API to find web pages that refers to street names. Initial street and region criteria are combined to form a search query, which is then executed by the Google API. Each URL from the Google results is fetched and the content of the pages converted into text. The text is then processed using pattern matching (programmers, read: regular expressions) designed to capture information relating to the relationship between streets (for example, streets that cross each other or turn into other streets).

For each page a list of vertices is produced, where each vertex represents an intersection between two streets. After the results for the initial street have been processed, the list of vertices will hopefully contain some vertices that intersect with the initial street.

At this point, the mapper performs a breadth-first search through the streets that can be reached from the initial street. Each street traversed during this process is subjected to the same process as the initial street, expanding the list of known street intersections. This process continues until no more streets can be reached from the initial street or until the halting criteria is satisfied (for instance, reaching the maximum amount of Google API key usage).

Once the data collection phase has completed, the application converts the intersection vertices into a graph. Each street becomes a vertex of its own, with outgoing edges connecting to the vertices of intersecting streets. The connectivity of this graph is analyzed (using the Jung graph package at http://jung.sourceforge.net) to determine the largest maximal subgraph where all pairs of vertices are reachable from each other. This subgraph almost always contains the starting street; the probability of this not occurring should decrease proportionally to the number of streets traversed (which naturally selects for streets that are in the same subgraph as the starting street).

The largest connected subgraph is then visualized using a Radial Layout algorithm provided by the Prefuse graph visualization framework (http://prefuse.sourceforge.net). The graph is initially centered on the start street but will automatically adjust its focus to center around the most recently selected street.

Ignoring its general lack of usefulness (at the time of this writing), there are several problems with the application worth noting:

Running the Hack

Point your web browser at http://richard.jones.name/google-hacks/google-cartography/applet/mapping.html.

TIP: You will need a recent version of the Java plug-in (1.3.x is not new enough). You can download the JRE which includes the java plug-in from http://java.sun.com/j2se/1.4.2/download.html. You'll also need your own Google API key ["Using Your Google API Key" in Chapter 9].

When the "Enter parameters" applet window appears (shown in Figure 3-7), enter your Google API key and adjust the maximums per instructions on the Google Cartography page. Now, type in your starting street and any additional search criteria by which to narrow the search.

Figure 3-7
Figure 3-7. Enter a starting point of interest and anything that might narrow down the possibilities

After a little churning, the applet will display a map for your street and region of interest similar to those in Figures Figure 3-6.

WARNING Be warned that the applet may use large amounts of bandwidth, depending on the parameters you enter.

Richard Jones


Google on the Go

Being on the go and away from your laptop or desktop doesn't mean leaving Google behind.

As the saying goes, "You can't take it with you." Unless, that is, you're talking about Google. Just because you've left your laptop at home or at the office, that doesn't necessarily mean leaving the Web and Google behind. So long as you have your trusty cell phone or network-enabled PDA in your pocket, so too do you have Google.

Whether you have the top-of-the-line Treo 600, Blackberry, or Sidekick with integrated web browser; base-model cell phone that your carrier gave you for free; or anything in between, chances are that you're able to Google on the go.

Google caters to the "on the go" crowd with its Google wireless interfaces: a simpler, lighter, gentler PDA- and smartphone-friendly version of Google, a WAP (read: wireless Web) flavor for cell phones with limited web access, and an SMS gateway for messaging your query to and receiving an almost instantaneous response from Google. There's even a mobile interface to Google's Froogle (http://froogle.google.com) product search.

Google by PDA or Smartphone

Google PDA Search (http://www.google.com/palm) brings all the power of Google to the PDA in your palm, hiptop on your belt, or cell phone in your pocket.

TIP: Don't be fooled by the palm in the Google PDA Search URL, which is an artifact of Palm's majority mindshare at the time. The interface will work with your Pocket PC, Zaurus, Treo, or any other mobile device that benefits from lighter web pages.

Settle that "in like Flynn" versus "in like Flint" dinner-table argument without leaving your seat. Find quickie reviews and commentary on that Dustmeister 2000 vacuum before making the purchase. Figure out where you've seen that bit-part actor before without having to wait for the credits.

Your modern PDA and the smarter so-called smartphones sport a full-fledged web browser on which you can surf all the Web has to offer in living color—albeit substantially smaller. You find the usual Address Bar, Back and Forward buttons, Bookmarks or Favorites, and point-and-click (or point-and-tap, as the case may be) hyperlinks. While the onboard browser might just be able to handle the regular Google.com web pages, the Google PDA Search provides simpler, smaller, no-nonsense, plain HTML Pages. And results pages pack in fewer results for faster loading.

Just point your mobile browser at http://google.com/palm, enter your search terms, click the Google Search button, and up come your results, as shown in Figure 5-27.

Figure 5-27
Figure 5-27. Google PDA Search (left) and results (right) on a Nokia smartphone

You have the full range of Google search syntax [Chapter 1] and complete web index available to you, although it might be more than a little challenging to enter those quotes, colons, parentheses, and minus signs.

Google by Cell Phone

If you have a garden-variety cell phone—of the sort your mobile provider either gives away free with signup or charges on the order of $40 for—you may yet find you have a built-in browser...of a sort. Don't expect anything nearly as fast, colorful, or feature-filled as your computer's web browser. This is a text-only world, limited in both display and interactivity.

That all said, you have the wealth—if not the Technicolor—of the Web right in your very pocket.

Step one, however, is to find the browser in the first place. It's usually hidden in plain sight, cleverly hidden behind some (possibly meaningless) moniker such as WAP, Web, Internet, Services, Downloads, or a brand name such as mMode or T-Zones. If nothing of the sort leaps out at you, look for an icon sporting your cell phone provider's logo, take a stroll through the menus, dig out your manual, or give your provider a ring (usually 611 on your cell phone).

Texting Sure Ain't QWERTY

Whether you're a 70-word-per-minute touch typist or you hunt and peck your way through the QWERTY keyboard, you're initially going to find texting a pokey chore. Rather than the array of letters, numbers, symbols, and shift keys on your computer keyboard, everything you do on your cell phone is confined to twelve keys: 0-9, *, and #. Frankly, it's an annoying system to learn, but once you get used to it, it's not too painful to use; some folks actually become rather adept at it, rivaling their regular keyboarding speeds.

Look closely at your phone and you'll notice each button also holds either a set of three to four alphabetic characters or obscure symbols not unlike those you'd expect to find on the UFO landing in your back yard. Like your regular phone, the 1 button is devoid of letters while 2 holds ABC, 3 DEF, and so on to 9 WXYZ.

When you're in web-browsing mode on your phone, you can tap the 2 button once to type an A, twice in quick succession for a B, and thrice for a C. Four times nets you a 2. Keep going and you'll make it back through A, B, C, and 2 again—on some phones encountering strange and wonderful foreign letters along the way. Do this for each and every letter in the word you're trying to spell out, spelling the word "google" like so: 4666 666455533. Notice the gap between the 666 and 666? What you're after is two "o"s in a row, but typing 666666 will get you either a single "o" or an "ø" since your phone doesn't know when you want to move on to the next letter. To type two of the same characters one after another, either wait a second or so after tapping in the first "o" or jiggle your phone's joystick to the right or down.

When it comes to special characters like the dot (.) and slash (/) common in web addresses, you'll turn to the 1 button. A period or dot is a single tap. The slash is usually 15. For those of you keeping score at home, that'll leave you with 92714666 6664555331 11111111111111196555 for wap.google.com/wml.

The texting equivalent of the spacebar is the 0 button.

What of digits? Surely you don't need to type 17 or so 1s—scrolling through all the symbols associated with the 1 button ([.,-?!'@:;/( )])—just to get back around to the 1 you wanted in the first place. Thankfully, all it takes is holding down the button for a second or so to jump right to the numeral. So instead of tapping through WXYZ to get to 9, hold down that 9 key for a moment or so and you're there.

There are more efficient input techniques, such as T9 ("Text on 9 keys") and other predictive text systems, but they're not as useful for entering possibly obscure words like those in web addresses and Google searches.

Browser in hand, point it at wap.google.com/wml, tap in a search (without tripping over your fingers), and click the Search button or link (as shown in Figure 5-28, left). A few moments later, your first set of results show up (as shown in Figure 5-28, center). Scroll to the bottom of the results and click the Next link (shown in Figure 5-28, right) to move on to the next page of results.

Figure 5-28
Figure 5-28. Google wireless search (left), first results (center), and link to the next 5 results (right)

Click any of the results to visit the page in question, just as you would in a normal browser. You'll notice immediately that the pages you visit by clicking a result link are dumbed down—similar to Google's wireless search itself—to suit the needs of your mobile's display abilities.

Truth be told, you're not visiting the resulting page directly at all. What you see on your screen and in Figure 5-29 is courtesy of the Google WAP proxy, a service turning HTML pages into WAP/WML (think of it as HTML for wireless devices) on the fly. Click another link on the resulting page and you'll continue browsing via the Google proxy, Google essentially turning all the Web into a mobile Web.

Figure 5-29
Figure 5-29. An ordinary web page as seen through the lens of the Google WAP proxy

In fact, you can actually surf rather than search the Web using the Google WAP proxy. Find your mobile browser's Options menu and click the Go to URL link. In the resulting page, enter any web site URL into the Go to URL box and click the Go button to visit a mobile version of that page.

TIP: The Options menu is chock-full of additional options provided by the Google WAP site: search the full Web, the mobile Web (sites Google has found to be optimized for mobile devices), language ["Language Tools" in Chapter 1], and Help documentation.

Google by SMS

As a New York Times article ("All Thumbs, Without the Stigma"; http://tech2.nytimes.com/mem/technology/techreview.html?res=9E00E6DE163FF931A2575BC0A9629C8B63) suggested recently, the thumb is the power digit. While the thumboard of choice for executives tends to be the Blackberry mobile email device (http://www.blackberry.com/), for the rest of the world (and many of the kids in your neighborhood), it's the cell phone and Short Message Service (SMS).

SMS messages are quick-and-dirty text messages (think mobile instant messaging) tapped into a cell phone and sent over the airwaves to another cell phone for around 5 to 10 cents apiece.

But SMS isn't just for person-to-person messaging. In the UK, BBC Radio provides so-called shortcodes (really just short telephone numbers) to which you can SMS your requests to the DJ's automated request-tracking system. You can SMS bus and rail systems for travel schedules. Your airline will SMS you updates on the status of your flight. And now you can talk to Google via SMS as well.

Google SMS (http://www.google.com/sms/) provides an SMS gateway for querying the Google Web index, looking up phone numbers [Hack #6], seeking out definitions [Hack #10], and comparative shopping in the Froogle product catalog service (http://froogle.google.com).

Simply send an SMS message to U.S. shortcode 46645 (read: GOOGL), as shown later in Figure 5-30, with one of the following forms of query:

Web Search
Search the Google Web by prefixing your query with a G (upper- or lowercase). You'll receive the top two results in return, formatted as text snippets, hopefully containing some information of use to you.

g capital of south africa
G answer to life the universe and everything
Google Local Business Listing
Consult Google Local's business listings by passing it a business name or type and city, state combination, or Zip Code.

vegetarian restaurant Jackson MS
southern cooking 95472
scooters.New York NY

TIP: The Google SMS documentation suggests using a period (.) between your query and city name or zip code to be sure that you're triggering a Google Local Search.

Residential Phone Number
Find a residential phone number with some combination of first or last name, city, state, Zip Code, or area code.

augustus gloop Chicago il
violet beauregard 95472
mike teevee ny

TIP: As with any Google Phonebook [Hack #6] query, you'll find only listed numbers in your results.

Froogle Prices

Check the current prices of items for sale online through Froogle (http://froogle.google.com/). To trigger a Froogle lookup, prefix your query with an F (upper- or lowercase), price, or prices (the latter two will also work at the end of the query).

g nokia 6230 cellphone
price bmw 2002
ugg boots prices
Zip Code

Pass Google SMS a U.S. Zip Code to find out where one might find it in the country.


TIP: Google SMS is sure to sport more features by the time you read this. Be sure to consult the "Google SMS: How to Use" page at http://www.google.com/sms/howtouse.html for the latest or—for the real thumb jockeys among you—subscribe your email address to an announcement list from the Google SMS home page.

You'll receive your results as one or more SMS messages labeled, appropriately enough, (1of3, 2of3, etc.), as shown in Figure 5-30. Notice that there are no URLs or links in the responses: what's the point when you can't click on them?

Figure 5-30
Figure 5-30. A Google SMS query (left) and response (right)

WARNING While the cost of sending an SMS messages (typically between 5 and 10 cents apiece) is usually borne by the sender, automated messages like those sent by Google SMS are usually charged to you, the receiver. Unless you have an unlimited SMS plan, all that googling can add up. Be sure to check out what's included in your mobile plan, check your phone bill, or call your mobile operator before you spend a lot of time (and money) on this service.

Froogle on the Go

If you wish you could compare prices at that "One Day Sale" on kitchen gadgets without leaving the store, Wireless Froogle (http://froogle.google.com) is as much a part of the shopping experience as that credit card.

Point your mobile browser at wml.froogle.com, tap in the name of the product you're about to take to the checkout (Figure 5-31, left), and up pops a list of prices as advertised by online vendors (Figure 5-31, right).

Figure 5-31
Figure 5-31. Wireless Froogle Search (left) and results (right)

You'll find everything from cellular phones to yogurt makers, abacuses to faux yak fur coats on Froogle.

At the time of this writing, Wireless Froogle is nowhere near as complete as one might hope. You can't constrain your results by price, group them by store, or sort them in any way. Results don't link to anywhere. That said, it is a still a handy price-check tool as you're standing in that checkout line.

$49.99 for a pashmina—lemme at it! Sometimes instant gratification is worth it; sometimes paying only $49.99 for silk is well worth the wait.

Take a Walk on the Lighter Side

Gmail with grace from any web browser, whether JavaScript-disabled, not yet supported, text-only, or on a PDA or mobile phone.

Being a child of Google, Gmail hides all of its complexity behind a rich, deep, feature-packed yet user-friendly web mail interface, assuming you have the right browser—one of recent vintage—for the job. But what to do if your IT department hasn't upgraded your version of the Internet Explorer browser since Windows 95, you're quite happy with the text-only Lynx browser, you're running the latest nightly build of browser XYZ, which Gmail simply doesn't like, or you're trying to reach your mail from a PDA or smartphone?

Gmail-lite (home page: http://sourceforge.net/projects/gmail-lite, SourceForge project page: http://sourceforge.net/projects/gmail-lite; GNU Public License), as the name suggests, puts a plain HTML face on Gmail. It is a PHP application that proxies your interactions with Gmail, allowing you to surf using whatever browser you have at hand or just plain prefer just plain prefer, while keeping Gmail happy with its end of the conversation.

The authors of gmail-lite have done a fantastic job, affording you a plain HTML interface to just about every bit of functionality Gmail provides through its more interactive JavaScript-based frontend.

Installing the Hack

You have to marvel at the wonders of PHP-based applications and their simple installation. Assuming you have the prerequisites taken care of, it's just a matter of downloading, unpacking, and enjoying. I installed gmail-lite both on my local Mac OS X laptop and under my hosted ISP account in seconds each.

TIP: Gmail-lite assumes you have PHP installed on a web server. It relies upon the libgmailer library (http://gmail-lite.sourceforge.net), included for your convenience in the gmail-lite distribution. You also need the curl library (http://www.php.net/curl) with SSL support (http://www.openssl.org) since gmail-lite always talks to Gmail over a secure channel.

Download gmail-lite (http://sourceforge.net/projects/gmail-lite) and unpack the distribution (0.56 at the time of this writing, but yours is sure to be a later version) somewhere under your web server's document root, where the rest of your web site lives (ask your system administrator or service provider if you're not sure where this is):

$ tar -xvzf gmail-lite-0.56.tar.gz 
$ mv gmail-lite-0.56 gmail-lite

TIP: That last command simply renames the directory to something that will be a little friendlier and easier to remember when it comes to visiting in my browser.

To verify that everything went to plan, point your web browser at diagnose.php using the URL corresponding to the gmail-lite directory on your web site—e.g., http://www.example.com/~rael/gmail-lite/diagnose.php The resulting page should look like Figure 6-18.

Figure 6-18
Figure 6-18. The diagnose.php script makes sure that everything is installed as expected

If diagnose.php does indicate that something's gone wrong, consult the installation and troubleshooting documentation in the INSTALL text file in your gmail-lite folder.

Running the Hack

Point your computer's web browser at the URL corresponding to the gmail-mobile directory on your web site—e.g., http://www.example.com/~rael/gmail-lite (or just click the "Press here..." link on the diagnose.php page).

WARNING Depending on your setup, you may actually need to tack /index.php on to that URL, but most PHP-enabled servers know to look for and serve up index.php as a default when no filename is specified and there's no static index.html in sight. The gmail-lite package includes just such an index.php file.

Figure 6-19 shows the plain HTML login screen as it will appear in a typical browser window. Enter your Gmail login (e.g., username@gmail.com) and password, alter the time zone if you feel so inclined, and click the "sign-in" button.

Figure 6-19
Figure 6-19. A gloriously plain HTML Gmail login page in a typical browser window

You're greeted with a summary page with links to your Gmail Inbox, Sent, Trash, and Spam folders, Starred messages, and personal labels—shown in Figure 6-20 as it appears in a smartphone XHTML web browser.

Figure 6-20
Figure 6-20. A summary of the state of your Gmail account as it appears in a smartphone XHTML browser

Click the Inbox link and you'll be presented with a simple list of your incoming messages, as shown on a Pocket PC in Figure 6-21. At the top is a quick-link toolbar and pull-down menus for switching views, exploring labeled mail, and searching your Gmail messages ["Gmail Search Syntax" earlier in this chapter]. (Click the Get button after making your selections or entering a search query.) At the bottom are actions you can apply to any number of checked messages (check the associated checkbox to the right of a message subject to act upon it), including archive/unarchive, label, star/unstar, mark as read/unread, mark/unmark as spam, and trash/untrash. (Click the Do button to apply any action.)

Figure 6-21
Figure 6-21. Your Gmail inbox, as seen through a Pocket PC

Select any email message to open it. Figure 6-22 shows a typical email message viewed in the text-only Lynx browser. The layout of individual message pages is much like that of the Inbox (or any folder) view. As you can see in the figure, I'm about to take a look at all my "foo"-labeled messages.

Figure 6-22
Figure 6-22. An individual message in the Lynx text-only browser

So, there you have it: a plain old HTML interface to about anything you can do through Gmail proper. (In all likelihood, by the time you read this, Gmail will have built its own plain HTML version without all the browser and JavaScript requirements. Still, this is a great hack and worth fiddling about with.)

WARNING The gmail-lite author does caution that "GMail is a still in beta, and GMailer (along with gmail-lite) is, I would say, an `alpha hack' of a beta software. So don't expect it to work all the time, and do not build critical mission applications upon it" (http://gmail-lite.sourceforge.net/).

See Also

If you're wanting to Gmail from a mobile phone with only a very basic WAP browser on board, you can still Gmail on the go with gmail-mobile [Hack #77]. And be sure to check out [Hack #67] for taking Google search along too.


Serve Backup Ads

Use AdSense's built-in (and rather thoughtful) ability to serve ads from alternate URLs when there are no targeted ads to offer.

There's a time and place for public service announcements. You just might not think your web site is the place and certainly not if it happens more than occasionally. When you signed up for AdSense (while you're no doubt a good citizen who pays their public radio and television dues), your intent was to reap a revenue stream from all the hard work that you've put into your content.

Yet there are times when a new section of your site hasn't yet been noticed and indexed by Google, AdSense has nothing appropriately targeted in its inventory, or there's a temporary outage of some kind. The net result is that you'll be running public service ads for the Red Cross or the like rather than revenue-generating, targeted advertising. Google AdSense doesn't get paid and so doesn't pay you for click-throughs on public service advertisements.

Now, you can either simply be OK with this coming up every so often—I know I am—or you can make use of a backup system Google AdSense provides: alternate ad URLs.

Point your browser at Google AdSense (http://www.google.com/adsense) and click the Ad Settings tab at the top of the page. Then, scroll down until you see "Alternate ad URL or color," as shown in Figure 7-11.

Figure 7-11
Figure 7-11. Provide an alternate URL for ads when AdSense has only public service advertisements to offer your site

Google AdSense suggests (https://google.com/adsense/faq#basics10) four backup options:


Paste in the URL of an image somewhere on the Web, ad or not, static or dynamically generated. This can be an alternate image that you've created and are serving from your own site, one produced on-the-fly by another advertising service, or any other image that either has some revenue stream associated with it or simply tickles your fancy. For example, to serve up a static image named advert1.jpg residing on your web site, you'd provide a URL like http://www.example.com/images/advert1.jpg.

Clickable image

Provide the URL of an HTML page somewhere out on the Web that contains only a snippet of markup for a hyperlinked image. For example, you might have a file on your site called adsense_alternate.html that contains the following line:

<a href="http://www.example.com/storefront/"><img src="http://www.example.com/
images/advert2.jpg border="0" /></a>

TIP: That's all the file should have in it, mind you; leave off all the opening <html><head></head><body> and closing </body></html> bits and everything else you usually pack into your pages.

The URL you'd provide as an alternate would then be a pointer to that partial page, something like http://www.example.com/adsense_alternate.html.

HTML color code

If you have nothing to display as an alternative and are dead set against running public service ads, blank out the space where the AdSense ad would have gone by, providing the hexadecimal HTML color code of your page's background or that particular bit of real estate. For example, if your page had a background color of #160B35, a lovely dark blue that I use on my own site, you'd type that color code right into the "Alternate ad URL or color" field.

Collapse your ad

Google provides an HTML file you can download to and serve from your own web site that calls a bit of JavaScript to collapse your ad so that it doesn't show in the event you'd otherwise have seen a public service ad. For instructions and a link to download the file, visit https://google.com/adsense/faq#basics13.

Whichever you choose, when you click the "Update code" button, a smidgeon of JavaScript (the third line in Figure 7-12) will be added to the AdSense code that you paste into your web page. This additional line provides all AdSense needs to serve up your alternate ad choice when it has no targeted ad to run on your site.

Figure 7-12
Figure 7-12. An alternate ad URL embedded in Google AdSense JavaScript code

Then again, there is a fifth alternative...

Amazon/Google Ad Replacement (AGAR; http://www.bestdealsdiscounts.com/agar; GNU Public License) is a Perl script that supplements your Google AdSense ads with product advertisement drawn from Amazon's Web Services (AWS; http://webservices.amazon.com) and Associates (http://associates.amazon.com) programs. Not only does it supplement AdSense, but it also mimics it in appearance, supports all the AdSense ad sizes, and allows you to customize your color scheme to match what you've chosen for AdSense.

TIP: For AGAR to be useful (and financially rewarding), you'll need to have signed up as an Amazon Associate (http://associates.amazon.com) through which you make money on purchases resulting from click-throughs on your site.

Download AGAR and get it running as a CGI script ["How to Run the Hacks" in the Preface]. There's not much at all you need to change in the script itself, save replacing the default Amazon Associates ID with your own:

my $associate_id = "insert your amazon associates id here";

WARNING If you're in the United Kingdom rather than the United States, you'll also want to change the locale in my $locale = "us"; to uk and my$uk_associate_id = "coolstufftoown"; to your U.K. Amazon Associates ID. If you're neither in the U.S. nor the U.K., there is some further adjustment necessary, but we leave that as an exercise for the reader.

Point your browser directly at the CGI script to test it out and you should see an ad banner, as shown in Figure 7-13, easily confused for an AdSense ad at first blush, but clearly linked to Amazon products.

Figure 7-13
Figure 7-13. An AGAR-generated AdSense-like Amazon banner ad

The product category is chosen at random by default (go ahead and reload the page a few times to see this in action), but this can be customized either by altering the settings baked into the script itself or embedding settings into the agar.cgi URL. For example, instead of just pointing at agar.cgi, try agar.cgi??input_mode=kitchen&input_id=491864&ad_format=125x125_as. This produces a 125 by 125 pixel ad drawing from Amazon's "kitchen" category.

TIP: While the concept of mode and id, as expressed in the preceding URL is beyond the scope of this book, suffice it to say that you need to pass matching textual and numerical browse IDs. You'll find a detailed description of browse nodes and IDs in the Amazon Web Services documentation and a list of some of the text/number pairs in the AGAR code itself—look for %browse_ids =. For an introduction to Amazon Web Services and all other things Amazon, pick up this book's cousin, Amazon Hacks (http://www.oreilly.com/catalog/amazonhks; O'Reilly) by Paul Bausch.

(If I may, I'd like to end with a pitch to at least consider letting the AdSense public service advertisements run. Sorry, I just couldn't help myself.)

Tara Calishain is the creator of the site, ResearchBuzz. She is an expert on Internet search engines and how they can be used effectively in business situations.

Rael Dornfest is Founder and CEO of Portland, Oregon-based Values of n. Rael leads the Values of n charge with passion, unearthly creativity, and a repertoire of puns and jokes — some of which are actually good. Prior to founding Values of n, he was O'Reilly's Chief Technical Officer, program chair for the O'Reilly Emerging Technology Conference (which he continues to chair), series editor of the bestselling Hacks book series, and instigator of O'Reilly's Rough Cuts early access program. He built Meerkat, the first web-based feed aggregator, was champion and co-author of the RSS 1.0 specification, and has written and contributed to six O'Reilly books. Rael's programmatic pride and joy is the nimble, open source blogging application Blosxom, the principles of which you'll find in the Values of n philosophy and embodied in Stikkit: Little yellow notes that think.

View catalog information for Google Hacks, 2nd Edition

Return to the O'Reilly Network.

Copyright © 2009 O'Reilly Media, Inc.