|
| 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