Scripting for the zip code

Peter M. Brigham pmbrig at gmail.com
Fri Dec 13 13:02:23 EST 2013


On Dec 12, 2013, at 10:53 AM, James Hurley wrote:

> Is there a way to *script* for the zip code given
> 
> Street address
> City
> State
> 
> It used to be possible using the USPS web site, it was even possible to get the nine digit zip, but no longer.
> 
> But for now, all I need is the 5 digit zip.

I struggled with this for a long while, unable to find a website that didn't use javascript to return the zip code. Here's what I settled on -- it requires an internet connection. In brief, I load a properly formatted query to google maps and then parse the html to extract the zip code. With a reasonable connection it takes less than half a second. Works most of the time, but I've found that very occasionally google maps returns the wrong zip code, unless my patients are mistaken about their own zip!

If you are doing wholesale queries (hundreds) within a short period of time then google maps may shut you down, I don't know if they track such things to keep bots away. This seems to do fine for isolated queries.

---------------

function getZip tAddr
   -- expects a standard line-delimited address, eg,
   --      123 Main St.
   --      Boston, MA
   if word -1 of tAddr is a number then return word -1 of tAddr
   -- already have zip code
   put formatAddressForGoogle(tAddr) into tURL
   try
      put URL tURL into thePage
   catch tError
      setupicons
      answer "Problem fetching the Google Maps webpage!" as sheet
      exit getZip
   end try
   put offset("sxpo:",thePage) into ofs
   -- look for a string like … sxpr:"MA",sxpo:"02135",sxcn:"US" ...
   put char ofs+6 to ofs+10 of thePage into tZip
   if ofs <> 0 and tZip is a number then
      return tZip
   else
      return "not a valid address"
   end if
end getZip

function formatAddressForGoogle tAddr
  -- used for the getZip handler
  put line 1 of tAddr into addr1
  if char 1 of addr1 is not a number and word 1 of addr1 <> "One" then
    -- probably a company name, or some such
    delete line 1 of tAddr
    put line 1 of tAddr into addr1
  end if
  if word 1 of addr1 = "One" then put "1" into word 1 of addr1
  -- google expects '1' not the 'One' that some snootier places use
  -- the following is to handle things like '110 Main St. - Suite 200'
  -- delete those before sending address to google
  if " - " is in addr1 then -- n-dash
    delete char (offset(" - ",addr1)) to -1 of addr1
  else if " – " is in addr1 then -- option-dash
    delete char (offset(" – ",addr1)) to -1 of addr1
  else if " — " is in addr1 then -- m-dash
    delete char (offset(" — ",addr1)) to -1 of addr1
  else if "#" is in addr1 then
    -- to handle apartment numbers
    -- delete those before sending address to google
    delete char (offset("#",addr1)) to -1 of addr1
  end if
  put sr(addr1) into line 1 of tAddr
  replace cr with ", " in tAddr
  put "http://maps.google.com/maps?f=q&hl=en&geocode=&q=" into tURL
  replace space with "+" in tAddr
  replace "++" with "+" in tAddr
  put tAddr after tURL
  return tURL
end formatAddressForGoogle

function sr tString
   return word 1 to -1 of tString
end sr

-------------------

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list