Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 70 Gleaning Phonebook Stats

figs/moderate.giffigs/hack70.gif

The Google API doesn't return data from a search using the phonebook syntaxes, but that doesn't mean you can't have some fun with it.

The Google API doesn't return results for queries using the phonebook: [Hack #17] syntaxes. It does, however, provide a result count!

Because it doesn't actually get you any phone numbers, passing a phonebook query to the Google API has minimal value. Nevertheless, this little hack makes the most of it. Ever wonder how many people with a particular surname one might find in various U.S. cities—the 15 most populated, at least?

70.1 The Code

#!/usr/local/bin/perl
# kincount.cgi
# How many people share a surname in the 15 most populated
# US cities?
# kincount.cgi is called as a CGI with form input

# Your Google API developer's key
my $google_key='insert key here';

# Location of the GoogleSearch WSDL file

my $google_wdsl = "./GoogleSearch.wsdl";

# 15 most populated US cities 
my @cities = ("New York NY", "Los Angeles CA", "Chicago IL", 
"Houston TX", "Philadelphia PA", "Phoenix AZ", "San Diego CA", 
"Dallas TX", "San Antonio TX", "Detroit MI", "San Jose CA", 
"Indianapolis IN", "San Francisco CA", "Jacksonville FL", 
"Columbus OH");

use strict;

use SOAP::Lite;
use CGI qw/:standard *table/;

print
  header(  ),
  start_html("KinCount"),
  h1("KinCount"),
  start_form(-method=>'GET'),
  'Surname: ', textfield(-name=>'query', -default=>'John Doe'),
  '   ',
  submit(-name=>'submit', -value=>'Search'),
  end_form(  ), p(  );

my $google_search  = SOAP::Lite->service("file:$google_wdsl");

if (param('query')) {
  print 
    start_table({-cellspacing=>'5'}),
    Tr([th({-align=>'left'}, ['City', 'Count'])]);

  foreach my $city (@cities) {
    my $cityquery = "rphonebook:" . param('query') . " $city";
    my $results = $google_search -> 
      doGoogleSearch(
        $google_key, $cityquery, 0, 10, "false", "",  "false",
        "", "latin1", "latin1"
      );

    my $resultcount = "$results->{'estimatedTotalResultsCount'}";
  
    print Tr([ td([
      $city,
      $resultcount >= 600
      ? "Too many for an accurate count."
      : $resultcount
      ])
    ]);
  }

  print 
    end_table(  ),
}

70.2 Running the Hack

This hack runs as a CGI script; call it from your browser and fill in the form.

70.3 Results

Figure 6-11 the results of a phonebook search for Bush.

Figure 6-11. KinCount search for Bush
figs/gooH_0611.gif

Notice that this script works equally well if fed a full name, "George Bush", as Figure 6-12 shows.

Figure 6-12. KinCount search for "George Bush"
figs/gooH_0612.gif

70.4 Hacking the Hack

70.4.1 Residential, business, or both

Notice that the script uses the rphonebook: syntax, guaranteeing only residential phonebook results. To restrict results to business listings, use bphonebook: instead, altering only one line (change in bold) in the code, like so:

my $cityquery = "bphonebook:" . param('query') . " $city";

A search for pizza provides a rundown of the number of pizza joints across U.S. cities. Searching for rphonebook:pizza, as one would expect, returns very few results. bphonebook:pizza behaves as expected.

The same holds true for replacing bphonebook: with phonebook:, thereby removing restriction by type of listing and returning all results, residential and business alike.

Of course you could always add a field to the form, allowing users to decide which type of survey they prefer. The following code (changes in bold) will do the trick nicely:

#!/usr/local/bin/perl
# kincount.cgi
# How many people share a surname in the 15 most populated
# US cities?
# kincount.cgi is called as a CGI with form input

# Your Google API developer's key
my $google_key='insert key here';

# Location of the GoogleSearch WSDL file
my $google_wdsl = "./GoogleSearch.wsdl";

# 15 most populated US cities 
my @cities = ("New York NY", "Los Angeles CA", "Chicago IL", 
"Houston TX", "Philadelphia PA", "Phoenix AZ", "San Diego CA", 
"Dallas TX", "San Antonio TX", "Detroit MI", "San Jose CA", 
"Indianapolis IN", "San Francisco CA", "Jacksonville FL", 
"Columbus OH");

use strict;

use SOAP::Lite;
use CGI qw/:standard *table/;

print
  header(  ),
  start_html("KinCount"),
  h1("KinCount"),
  start_form(-method=>'GET'),
  'Query: ', textfield(-name=>'query', -default=>'John Doe'),
  '   ',
  popup_menu(
    -name=>'listing_type',
    -values=>['rphonebook:', 'bphonebook:', 'phonebook:'],
    -labels=&t;{ 'rphonebook:'=>'Residential', 
      'bphonebook:'=>'Business', 'phonebook:'=>'All Listings' }
  ),
  '   ',
  submit(-name=>'submit', -value=>'Search'),
  end_form(  ), p(  );

my $google_search  = SOAP::Lite->service("file:$google_wdsl");

if (param('query')) {
  print 
    start_table({-cellspacing=>'5'}),
    Tr([th({-align=>'left'}, ['City', 'Count'])]);

  foreach my $city (@cities) {
    my $cityquery = param('listing_type') . param('query') . " $city";
    my $results = $google_search -> 
      doGoogleSearch(
        $google_key, $cityquery, 0, 10, "false", "",  "false",
        "", "latin1", "latin1"
      );

    my $resultcount = "$results->{'estimatedTotalResultsCount'}";
  
    print Tr([ td([
      $city,
      $resultcount >= 600
      ? "Too many for an accurate count."
      : $resultcount
      ])
    ]);
  }

  print 
    end_table(  ),
}

The results of a search for bphonebook:pizza using this altered form look something like Figure 6-13.

Figure 6-13. Results of bphonebook:pizza
figs/gooH_0613.gif

And it doesn't just count the number of pizza joints, either! How about calculating a geek index based on the number of geek landmarks—business listings for: electronics stores, computer shops, Internet companies, cyber cafes, etc.

70.4.2 The cities

This script holds its list of cities in an array. Of course, you don't have to do it this way. You could create a form field that accepts user-entered city, state, or both. Just be sure to remind your users that the phonebook syntaxes require either the entire state name or the postal code abbreviation; either of these two will work:

bphonebook:pizza los angeles california
bphonebook:pizza los angeles ca

This will not:

bphonebook:pizza los angeles cali

70.5 The 600-Foot Ceiling

A phonebook syntax search via the Google Web API will consistently return a ceiling of 600 for any count higher than that; thus, the "Too many for an accurate count" error message. Without that error check, you'd find the 600s that kept showing up rather repetitive, not to mention useless.

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    Chapter 6. Google Web API Applications
    6.1 Hacks #60-85
    6.2 The Ingenuity of Millions
    6.3 Learning to Code
    6.4 What You'll Find Here
    6.5 Finding More Google API Applications
    6.6 The Possibilities Aren't Endless, but They're Expanding
    Hack 60 Date-Range Searching with a Client-Side Application
    Hack 61 Adding a Little Google to Your Word
    Hack 62 Permuting a Query
    Hack 63 Tracking Result Counts over Time
    Hack 64 Visualizing Google Results
    Hack 65 Meandering Your Google Neighborhood
    Hack 66 Running a Google Popularity Contest
    Hack 67 Building a Google Box
    Hack 68 Capturing a Moment in Time
    Hack 69 Feeling Really Lucky
    Hack 70 Gleaning Phonebook Stats
    Hack 71 Performing Proximity Searches
    Hack 72 Blending the Google and Amazon Web Services
    Hack 73 Getting Random Results (On Purpose)
    Hack 74 Restricting Searches to Top-Level Results
    Hack 75 Searching for Special Characters
    Hack 76 Digging Deeper into Sites
    Hack 77 Summarizing Results by Domain
    Hack 78 Scraping Yahoo! Buzz for a Google Search
    Hack 79 Measuring Google Mindshare
    Hack 80 Comparing Google Results with Those of Other Search Engines
    Hack 81 SafeSearch Certifying URLs
    Hack 82 Syndicating Google Search Results
    Hack 83 Searching Google Topics
    Hack 84 Finding the Largest Page
    Hack 85 Instant Messaging Google
    Chapter 7. Google Pranks and Games
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele