Commits

yourkin committed ece9e29

Updated docstrings. Minimized bandwidth by not fetching unused results. Brought API to correspond with original repo.

Comments (0)

Files changed (1)

yandex_maps/api.py

 """
 
 import xml.dom.minidom
-import urllib
+from urllib import urlencode
 from yandex_maps import http
 
 STATIC_MAPS_URL = 'http://static-maps.yandex.ru/1.x/?'
 GEOCODE_URL = 'http://geocode-maps.yandex.ru/1.x/?'
 
 def get_map_url(api_key, longitude, latitude, zoom, width, height):
-    ''' returns URL of static yandex map '''
+    """
+    Returns URL of static Yandex map.
+    """
     params = [
        'll=%0.7f,%0.7f' % (float(longitude), float(latitude),),
        'size=%d,%d' % (width, height,),
     ]
     return '%s%s' % (STATIC_MAPS_URL, '&'.join(params))#efficient concatenation
 
-def geocode(api_key, location, reverse=False, timeout=2, results=1):
-    ''' returns coordinates for address or vice versa'''
+def geocode(api_key, location, reverse=False, timeout=2):
+    """
+    Returns a coordinate tuple corresponding to 
+    location address. Alternatevily, returns place name,
+    given location coordinates in string format, 
+    separated by comma or space.  
+    """
+    try:
+        xml = _get_geocode_xml(api_key, location, timeout)
+        return _get_coords(xml, reverse)
+    except IOError:
+        return None, None
+
+def _get_geocode_xml(api_key, location, timeout=2):
+    url = _get_geocode_url(api_key, location)
+    status_code, response = http.request('GET', url, timeout=timeout)
+    return response
+
+def _get_geocode_url(api_key, location, n=1):
     if isinstance(location, unicode):
         location = location.encode('utf8')
-    params = urllib.urlencode({'geocode': location, 
-        'key': api_key, 'results': results})
-    url = '%s%s' % (GEOCODE_URL, params)
-    try:
-        status_code, response = http.request('GET', url, timeout=timeout)
-    except IOError:
-        return None, None
-    return _get_coords(response, reverse)
+    params = urlencode({'geocode': location, 'key': api_key, 'results': n})
+    return '%s%s' % (GEOCODE_URL, params)
 
 def _get_coords(response, reverse):
-    '''extract coordinates or place name if reverse is set'''
     try:
         dom = xml.dom.minidom.parseString(response)
         pos_elem = dom.getElementsByTagName(reverse and 'text' or 'pos')[0]
         else:
             return tuple(pos_data.split())
     except IndexError:
-        return None, None
+        return None, None