Amazon Hacks Free Open Book

Amazon Hacks

Previous Section Next Section

Hack 45 Find Purchase Circles by Zip Code

figs/expert.giffigs/hack45.gif

Combining two different Web Services can create a new feature.

Finding purchase circles is pretty easy, and with a bit of scraping, you can tie them into geographic data. Amazon doesn't offer the ability to look up purchase circles by U.S. zip code, but using the USPS web site and some simple pattern matching, you can find purchase circles for all cities within a given zip code.

45.1 The Code

This PHP script combines two different services with screen scraping. It looks for all of the cities within a particular zip code at the U.S. Postal Service web site. Then it finds matching purchase circles at Amazon and provides links to them. Create zip_circle.php with the following code:

<?php
$strZip = $_GET['zipcode'];
$zipPage = "";
$indexPage = "";
$cntCity = 0;
$myCity = "";

set_time_limit(60);

//get a certain number of characters
function left($str_in,$num) {
    return ereg_replace("^(.{1,$num})[ .,].*","\\1", $str_in); 
}

function findCircle($city) {
    $indexPage = "";
    //Find purchase cirlce brose codes
    $abc = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
    $a_abc = split(",", $abc);
    $thisLetter = strtolower(substr($city,0,1));
    for($j = 0;$j < count($a_abc)-1; $j++) {
        if ($thisLetter == $a_abc[$j]) {
            $thisCode = 226120 + ($j + 1);
        }
    }
    $url = "http://www.amazon.com/exec/obidos/tg/cm/browse-communities/-/" . 
           $thisCode . "/t/";
    $contents = fopen($url,"r");
    do {
       $data = fread ($contents, 4096);                                     
       if (strlen($data) == 0) {                                            
          break;                                                             
       }                                                                    
       $indexPage .= $data;                                                 
   } while(1);
    fclose ($contents);
    $k = 0;
    if (preg_match_all('/i>.*?<a href=/exec/obidos/tg/browse/-/(.*?)/t/[RETURN]
.*?>(.*?)</a>.*?<l/s',$indexPage,$cities)) {
      foreach ($cities[2] as $cityName) {
        if (strtolower($city) == strtolower($cityName)) {
          $link = "http://www.amazon.com/exec/obidos/tg/cm/[RETURN]
browse-communities/-/";
          $link = $link . $cities[1][$k];
          $link = "<a href=" . $link . ">";
          $link = $link . $cityName . "</a>";
          return $link;
        }
        $k++;
       } // foreach
       return "No purchase circle found for " . $city;
    } //if
} //function

//Get cities associated with zip codes from USPS
$url = "http://www.usps.com/zip4/zip_response.jsp?zipcode=" . $strZip;
$contents = fopen($url,"r");
while (!feof ($contents))
    $zipPage .= fgets($contents, 4096);
fclose ($contents);

if (preg_match_all('/<tr valign="top" bgcolor=".*?">(.*?)</tr>/
s',$zipPage,$cityState)) {
    foreach ($cityState[0] as $cs) {
        $cntCity++;
        if (preg_match_all('/\n\n(.*?)</font></td>\n/',$cs,$c)) {
            foreach ($c[1] as $d) {
                $myCity = $myCity . $d . ", ";
            }
        $myCity = $myCity . Chr(8);
        $myCity = ereg_replace(", ".chr(8), chr(8), $myCity);
        } else {
            echo "city not found.";
        }
    }
}

echo "<h2>Purchase Circles</h2>";

$a_myCity = split(chr(8), $myCity);
for($i = 0;$i < count($a_myCity)-1; $i++) {
    $thisCity = $a_myCity[$i];
    echo findCircle($thisCity) . "<br>";
}
?>

Scraping both the USPS site and Amazon can take some time, so the time limit for the script has been increased to 60 seconds.

This script relies on the fact that certain purchase circle pages have predictable IDs. Armed with the knowledge that every city that starts with A is in Purchase Circle 226121, we can assume that cities that start with B will be in Purchase Circle ID 226122, etc. As cities are found at the USPS site, their first letter is matched up with a Purchase Circle ID, and the page is scraped to find an exact match.

45.2 Running the Hack

Upload this script to a web server and access the page providing a zip code in the querystring, like so:

http://your.server/zip_circle.php?zipcode=zip code

The result should be a list of links to Amazon purchase circles for cities in that code.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Credits
    Foreword
    Preface
    Chapter 1. Browsing and Searching
    Chapter 2. Controlling Your Information
    Chapter 3. Participating in the Amazon Community
    3.1 Hacks #27-48
    3.2 Community Features
    3.3 Accessing Community Features
    Hack 27 Write a Review
    Hack 28 Link Directly to Reviews of a Product
    Hack 29 Post a Review from a Remote Site
    Hack 30 Add Pop-up Amazon Reviews to Your Web Site
    Hack 31 Send an Email Alert if a Review Is Added to a Product
    Hack 32 Sort Books by Average Customer Rating
    Hack 33 Sort Your Recommendations by Average Customer Rating
    Hack 34 Scrape Product Reviews
    Hack 35 Publish Your Amazon Reviews on Your Site
    Hack 36 Share the Love (and Savings!) with Your Friends
    Hack 37 Create a Guide
    Hack 38 Post a Guide Remotely
    Hack 39 Add Product Advice Remotely
    Hack 40 Scrape Customer Advice
    Hack 41 Create a Listmania! List
    Hack 42 Gather Your Friends on Amazon
    Hack 43 Gather Your Friends' Amazon IDs
    Hack 44 Get Purchase Circle Products with Screen Scraping
    Hack 45 Find Purchase Circles by Zip Code
    Hack 46 Track the Ranks of Books Over Time
    Hack 47 Group Conversations About Books
    Hack 48 Add a 'Currently Reading' List to Your Web Site
    Chapter 4. Selling Through Amazon
    Chapter 5. Associates Program
    Chapter 6. Amazon Web Services
    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