Gleaning Phonebook Stats

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?

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(  ),
}

Running the Hack

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

Results

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

KinCount search for Bush

Figure 6-11. KinCount search for Bush

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

KinCount search for “George Bush”

Figure 6-12. KinCount search for “George Bush”

Hacking the Hack

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.

Results of bphonebook:pizza

Figure 6-13. Results of bphonebook:pizza

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.

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

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.179.100