/
geonames.py
56 lines (47 loc) · 1.74 KB
/
geonames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import httplib
import simplejson
import sys
from urllib import urlencode
from responsehandlers.element_tree import ElementTreeResponseHandler
# Default GeoNames server to connect to.
DEFAULT_GEONAMES_SERVER = 'ws.geonames.org'
DEFAULT_RESPONSE_HANDLER = ElementTreeResponseHandler
class GeoNames():
"""
Accessor class for the online GeoNames geographical database.
"""
def __init__(self, server=DEFAULT_GEONAMES_SERVER, default_handler=DEFAULT_RESPONSE_HANDLER):
"""
Create a GeoNames object.
"""
self.server = server
self.response_handler = default_handler()
def _api_call(self, method, resource, **kwargs):
"""
Makes a generic API call to geonames webservice.
"""
uri = "/%s?%s" %(resource, urlencode(kwargs))
c = self.get_connection()
c.request(method, uri)
response = c.getresponse()
if not 200 == response.status:
raise GeoNameException("Expected a 200 reponse but got %s." %(response.status))
return self.response_handler.get_processed_data(response.read())
def get_connection(self):
"""
Return a connection object to the webservice.
"""
c = httplib.HTTPConnection(self.server)
return c
# TODO: move this to a helper object.
def search(self, name, country):
"""
Perform a search for a country's information.
"""
# we only want exact matches, and we only want one possible match.
return self._api_call('GET', 'search', name_equals=name, country=country, maxRows=1)
class GeoNameException(Exception):
"""
Error initializing GeoNames accessor.
"""
pass