Cleanup geocode.
authorScott Gasch <[email protected]>
Wed, 27 Apr 2022 02:36:56 +0000 (19:36 -0700)
committerScott Gasch <[email protected]>
Wed, 27 Apr 2022 02:36:56 +0000 (19:36 -0700)
geocode.py

index 1ee7f20574e98b4ce7123eeab06c84e9d832e3f0..023fe336f3a775908476cacc35bf1167573a0980 100644 (file)
@@ -9,7 +9,7 @@ from typing import Dict, Optional
 
 import requests
 from bs4 import BeautifulSoup
-from requests.utils import quote
+from requests.utils import requote_uri
 
 logger = logging.getLogger(__name__)
 
@@ -22,14 +22,14 @@ def geocode_address(address: str) -> Optional[Dict[str, str]]:
     '5 SHELBERN DR, LINCROFT, NJ, 07738'
 
     """
-    encoded_address = quote(address)
     url = 'https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress'
-    url += f'?address={encoded_address}'
+    url += f'?address={address}'
     url += '&layers=all&benchmark=4&vintage=4'
+    url = requote_uri(url)
     logger.debug('GET: %s', url)
     r = requests.get(url)
     if r.status_code != 200:
-        logger.error(f'Unexpected response code {r.status_code}, wanted 200.  Fail.')
+        logger.error('Unexpected response code %d, wanted 200.  Fail.', r.status_code)
         return None
     else:
         soup = BeautifulSoup(r.text, 'html.parser')
@@ -40,7 +40,8 @@ def geocode_address(address: str) -> Optional[Dict[str, str]]:
         out = {}
         for line in output.split('\n'):
             if re.match(r'.*: *$', line):
-                label = line[:-2].strip()
+                line = line.strip()
+                label = line[:-1]
                 logger.debug('Label is: "%s"', label)
             else:
                 if label: