Skip to content

Commit 9731d88

Browse files
author
Stefan Jansen
committed
Merge pull request #1 from steffex/v0.1
version 0.1
2 parents 4999b07 + d49487e commit 9731d88

File tree

4 files changed

+258
-1
lines changed

4 files changed

+258
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv
2+
*.pyc

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
11
pyPostcode
22
==========
33

4-
Python wrapper for the API of postcodeapi.nu
4+
##Introduction
5+
6+
This is a Python library to request information from the PostcodeApi.nu API.
7+
This API allows you to search for Dutch addresses using zipcodes.
8+
9+
For more information about this API, please visit http://postcodeapi.nu
10+
11+
12+
##Installation
13+
14+
###Manually
15+
16+
pyPostcode consists of a single file (pyPostcode.py) that you can put in your python search path or in site-packages (or dist-packages depending on the platform)
17+
You can also simply run it by putting it in the same directory as you main script file or start a python interpreter in the same directory.
18+
pyPostcode works with Python 2.7.x (you're welcome to test other versions)
19+
20+
###API-key
21+
22+
The API can only be used when you have your own API-key.
23+
You can request this key by visiting: http://www.postcodeapi.nu/#request
24+
25+
26+
##Example
27+
28+
###Basic usage
29+
30+
Get the address by using the zipcode and the house number
31+
32+
```python
33+
#!/usr/bin/python
34+
35+
from pyPostcode import pyPostcodeApi
36+
37+
postcodeapi = pyPostcodeApi('{YOUR_API_KEY}') # Set your own API-key
38+
result = postcodeapi.getaddress('1011AC', 154) # use address search
39+
print result.street, result.house_number, result.town
40+
```
41+
42+
###Result data
43+
44+
the following information can be gathered from the result:
45+
46+
* street
47+
* house_number
48+
* postcode
49+
* town
50+
* municipality
51+
* province
52+
* latitude
53+
* longitude
54+
* x ([Rijksdriehoek]/[Trigonometrical] coordinate)
55+
* y ([Rijksdriehoek]/[Trigonometrical] coordinate)
56+
57+
##License
58+
59+
"PostcodeApi" is owned by freshheads, see http://postcodeapi.nu and http://freshheads.com for more information.
60+
I am in no way affiliated with PostcodeAPI or the freasheads organization.
61+
62+
[Rijksdriehoek]: http://nl.wikipedia.org/wiki/Rijksdriehoekscoördinaten
63+
[Trigonometrical]: http://en.wikipedia.org/wiki/Triangulation_station
64+

example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pyPostcode import pyPostcodeApi
2+
3+
postcodeapi = pyPostcodeApi('{YOUR_API_KEY}')
4+
result_street = postcodeapi.getaddress('1011AC') # use p6 search
5+
result_address = postcodeapi.getaddress('1011AC', 154) # use address search
6+
print result_street._data, result_address._data

pyPostcode.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
'''
6+
pyPostcode by Stefan Jansen
7+
pyPostcode is an api wrapper for http://postcodeapi.nu
8+
'''
9+
10+
import sys
11+
import json
12+
import httplib
13+
14+
15+
__version__ = '0.1'
16+
17+
18+
class pyPostcodeException(Exception):
19+
20+
def __init__(self, id, message):
21+
self.id = id
22+
self.message = message
23+
24+
25+
class pyPostcodeApi(object):
26+
27+
def __init__(self, api_key):
28+
if api_key is None or api_key is '':
29+
raise pyPostcodeException(0, "Please request an api key on http://postcodeapi.nu")
30+
31+
self.api_key = api_key
32+
self.url = 'api.postcodeapi.nu'
33+
34+
def handleresponseerror(self, status):
35+
if status == 401:
36+
msg = "Access denied! Api-key missing or invalid"
37+
elif status == 404:
38+
msg = "No result found"
39+
elif status == 500:
40+
msg = "Unknown API error"
41+
else:
42+
msg = "dafuq?"
43+
44+
raise pyPostcodeException(status, msg)
45+
46+
def request(self, path=None):
47+
'''Helper function for HTTP GET requests to the API'''
48+
49+
headers = {"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
50+
"Accept": "application/json",
51+
"Accept-Language": "en",
52+
"Api-Key": self.api_key}
53+
54+
conn = httplib.HTTPConnection(self.url)
55+
'''Only GET is supported by the API at this time'''
56+
conn.request('GET', path, None, headers)
57+
58+
result = conn.getresponse()
59+
60+
if result.status is not 200:
61+
conn.close()
62+
self.handleresponseerror(result.status)
63+
64+
resultdata = result.read()
65+
conn.close()
66+
67+
jsondata = json.loads(resultdata)
68+
data = jsondata['resource']
69+
70+
return data
71+
72+
def getaddress(self, postcode, house_number=None):
73+
if house_number == None:
74+
house_number = ''
75+
76+
path = '/{0}/{1}'.format(
77+
str(postcode),
78+
str(house_number))
79+
80+
try:
81+
data = self.request(path)
82+
except Exception:
83+
data = None
84+
85+
if data is not None:
86+
return pyPostcodeResource(data)
87+
else:
88+
return False
89+
90+
91+
class pyPostcodeResource(object):
92+
93+
def __init__(self, data):
94+
self._street = None
95+
self._house_number = None
96+
self._postcode = None
97+
self._town = None
98+
self._municipality = None
99+
self._province = None
100+
self._latitude = None
101+
self._longitude = None
102+
self._x = None
103+
self._y = None
104+
105+
if data is not None:
106+
self.setdata(data)
107+
108+
109+
def setdata(self, data):
110+
self._data = data
111+
data_keys = self._data.keys()
112+
for key in data_keys:
113+
if hasattr(self, key):
114+
setattr(self, key, self._data[key])
115+
116+
@property
117+
def street(self):
118+
return self._street
119+
@street.setter
120+
def street(self, value):
121+
self._street = value
122+
123+
@property
124+
def house_number(self):
125+
'''
126+
House number can be empty when postcode search
127+
is used without house number
128+
'''
129+
return self._house_number
130+
@house_number.setter
131+
def house_number(self, value):
132+
self._house_number = value
133+
134+
@property
135+
def postcode(self):
136+
return self._postcode
137+
@postcode.setter
138+
def postcode(self, value):
139+
self._postcode = value
140+
141+
@property
142+
def town(self):
143+
return self._town
144+
@town.setter
145+
def town(self, value):
146+
self._town = value
147+
148+
@property
149+
def municipality(self):
150+
return self._municipality
151+
@municipality.setter
152+
def municipality(self, value):
153+
self._municipality = value
154+
155+
@property
156+
def province(self):
157+
return self._province
158+
@province.setter
159+
def province(self, value):
160+
self._province = value
161+
162+
@property
163+
def latitude(self):
164+
return self._latitude
165+
@latitude.setter
166+
def latitude(self, value):
167+
self._latitude = value
168+
169+
@property
170+
def longitude(self):
171+
return self._longitude
172+
@longitude.setter
173+
def longitude(self, value):
174+
self._longitude = value
175+
176+
@property
177+
def x(self):
178+
return self._x
179+
@x.setter
180+
def x(self, value):
181+
self._x = value
182+
183+
@property
184+
def y(self):
185+
return self._y
186+
@y.setter
187+
def y(self, value):
188+
self._y = value
189+

0 commit comments

Comments
 (0)