community-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1559848 - /comdev/nearby_people/lib/geoname.py
Date Mon, 20 Jan 2014 22:58:36 GMT
Author: nick
Date: Mon Jan 20 22:58:36 2014
New Revision: 1559848

URL: http://svn.apache.org/r1559848
Log:
Switch to a newer version of the geoname library, to work round issues on our old zone which
runs the code

Modified:
    comdev/nearby_people/lib/geoname.py

Modified: comdev/nearby_people/lib/geoname.py
URL: http://svn.apache.org/viewvc/comdev/nearby_people/lib/geoname.py?rev=1559848&r1=1559847&r2=1559848&view=diff
==============================================================================
--- comdev/nearby_people/lib/geoname.py (original)
+++ comdev/nearby_people/lib/geoname.py Mon Jan 20 22:58:36 2014
@@ -1,18 +1,38 @@
 # New GeoNames library from Greg Robbins
 # See https://github.com/gregrobbins/geonames-python
+# This is the fork https://github.com/Gagravarr/geonames-python
 
 import sys
 import urllib
 import urllib2
-import simplejson as json
+try:
+    import simplejson as json
+except ImportError:
+    import json
+
 DOMAIN = 'http://api.geonames.org/'
 USERNAME = '' #enter your geonames username here
+DEBUG = 0
 
-def fetchJson(method, params):
+def fetchJson(method, dparams):
+    # GeoNames supports the same arguments multiple times, eg 
+    #  featureClass=["P","T"] -> featureClass=P&featureClass=T
+    # We need to switch from a dict to array so that urllib will send that
+    params = []
+    for key, value in dparams.items():
+         # One or many values?
+         if isinstance(value, basestring) or isinstance(value, int):
+             params.append((key,value))
+         else:
+             for v in value:
+                 params.append((key,v))
     uri = DOMAIN + '%s?%s&username=%s' % (method, urllib.urlencode(params), USERNAME)
-    print uri
+    if DEBUG:
+        print uri
     resource = urllib2.urlopen(uri).readlines()
     js = json.loads(resource[0])
+    if DEBUG:
+        print js
     return js
 
 def get(geonameId, **kwargs):
@@ -35,22 +55,18 @@ def children(geonameId, **kwargs):
 
     if('geonames' in results):
         return results['geonames']
+    elif('status' in results and 'message' in results['status']):
+        raise IOError(results['status']['message'])
     else:
         return None
 
 def search(**kwargs):
     method = 'searchJSON'
     valid_kwargs = ('q', 'name', 'name_equals', 'name_startsWith', 'maxRows', 'startRow',
'country', 'countryBias', 'continentCode', 'adminCode1', 'adminCode2', 'adminCode3', 'featureClass',
'featureCode', 'lang', 'type', 'style', 'isNameRequired', 'tag', 'operator', 'charset',)
-    params = []
+    params = {}
     for key in kwargs:
         if key in valid_kwargs:
-            # One or many values?
-            value = kwargs[key]
-            if isinstance(value, basestring) or isinstance(value, int):
-               params.append((key,value))
-            else:
-               for v in value:
-                  params.append((key,v))
+            params[key] = kwargs[key]
     results = fetchJson(method, params)
 
     if('geonames' in results):
@@ -71,6 +87,8 @@ def postalCodeSearch(**kwargs):
 
     if('postalCodes' in results):
         return results['postalCodes']
+    elif('status' in results and 'message' in results['status']):
+        raise IOError(results['status']['message'])
     else:
         return None
 
@@ -85,6 +103,8 @@ def findNearbyPostalCodes(**kwargs):
 
     if('postalCodes' in results):
         return results['postalCodes']
+    elif('status' in results and 'message' in results['status']):
+        raise IOError(results['status']['message'])
     else:
         return None
 
@@ -99,5 +119,7 @@ def hierarchy(geonameId, **kwargs):
 
     if('geonames' in results):
         return results['geonames']
+    elif('status' in results and 'message' in results['status']):
+        raise IOError(results['status']['message'])
     else:
         return None



Mime
View raw message