-
Notifications
You must be signed in to change notification settings - Fork 64
/
api.py
309 lines (260 loc) · 12.5 KB
/
api.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import requests
import xmltodict
try:
# python 3
from urllib.parse import urlparse, urlunparse, urlencode
except ImportError:
from urlparse import urlparse, urlunparse
from urllib import urlencode
from .error import ZillowError
from .place import Place
class ValuationApi(object):
"""
A python interface into the Zillow API
By default, the Api caches results for 1 minute.
Example usage:
To create an instance of the zillow.ValuationApi class:
>>> import zillow
>>> api = zillow.ValuationApi()
All available methods include:
>>> data = api.GetSearchResults("<your key here>", "<your address here>", "<your zip here>")
"""
def __init__(self):
self.base_url = "https://www.zillow.com/webservice"
self._input_encoding = None
self._request_headers=None
self.__auth = None
self._timeout = None
def GetSearchResults(self, zws_id, address, citystatezip, retnzestimate=False):
"""
The GetSearchResults API finds a property for a specified address.
The content returned contains the address for the property or properties as well as the Zillow Property ID (ZPID) and current Zestimate.
It also includes the date the Zestimate was computed, a valuation range and the Zestimate ranking for the property within its ZIP code.
The GetSearchResults API Web Service is located at: http://www.zillow.com/webservice/GetSearchResults.htm
:param zws_id: The Zillow Web Service Identifier. Each subscriber to Zillow Web Services is uniquely identified by an ID sequence and every request to Web services requires this ID.
:param address: The address of the property to search. This string should be URL encoded.
:param citystatezip: The city+state combination and/or ZIP code for which to search. This string should be URL encoded. Note that giving both city and state is required. Using just one will not work.
:param retnzestimat: Return Rent Zestimate information if available (boolean true/false, default: false)
:return:
"""
url = '%s/GetSearchResults.htm' % (self.base_url)
parameters = {'zws-id': zws_id}
if address and citystatezip:
parameters['address'] = address
parameters['citystatezip'] = citystatezip
else:
raise ZillowError({'message': "Specify address and citystatezip."})
if retnzestimate:
parameters['retnzestimate'] = 'true'
resp = self._RequestUrl(url, 'GET', data=parameters)
data = resp.content.decode('utf-8')
xmltodict_data = xmltodict.parse(data)
place = Place()
try:
place.set_data(xmltodict_data.get('SearchResults:searchresults', None)['response']['results']['result'])
except:
raise ZillowError({'message': "Zillow did not return a valid response: %s" % data})
return place
def GetZEstimate(self, zws_id, zpid, retnzestimate=False):
"""
The GetZestimate API will only surface properties for which a Zestimate exists.
If a request is made for a property that has no Zestimate, an error code is returned.
Zillow doesn't have Zestimates for all the homes in its database.
For such properties, we do have tax assessment data, but that is not provided through the API.
For more information, see our Zestimate coverage.
:zws_id: The Zillow Web Service Identifier.
:param zpid: The address of the property to search. This string should be URL encoded.
:param retnzestimate: Return Rent Zestimate information if available (boolean true/false, default: false)
:return:
"""
url = '%s/GetZestimate.htm' % (self.base_url)
parameters = {'zws-id': zws_id,
'zpid': zpid}
if retnzestimate:
parameters['retnzestimate'] = 'true'
resp = self._RequestUrl(url, 'GET', data=parameters)
data = resp.content.decode('utf-8')
xmltodict_data = xmltodict.parse(data)
place = Place()
try:
place.set_data(xmltodict_data.get('Zestimate:zestimate', None)['response'])
except:
raise ZillowError({'message': "Zillow did not return a valid response: %s" % data})
return place
def GetDeepSearchResults(self, zws_id, address, citystatezip, retnzestimate=False):
"""
The GetDeepSearchResults API finds a property for a specified address.
The result set returned contains the full address(s), zpid and Zestimate data that is provided by the GetSearchResults API.
Moreover, this API call also gives rich property data like lot size, year built, bath/beds, last sale details etc.
:zws_id: The Zillow Web Service Identifier.
:param address: The address of the property to search. This string should be URL encoded.
:param citystatezip: The city+state combination and/or ZIP code for which to search.
:param retnzestimate: Return Rent Zestimate information if available (boolean true/false, default: false)
:return:
Example:
"""
url = '%s/GetDeepSearchResults.htm' % (self.base_url)
parameters = {'zws-id': zws_id,
'address': address,
'citystatezip': citystatezip
}
if retnzestimate:
parameters['retnzestimate'] = 'true'
resp = self._RequestUrl(url, 'GET', data=parameters)
data = resp.content.decode('utf-8')
xmltodict_data = xmltodict.parse(data)
place = Place(has_extended_data=True)
try:
place.set_data(xmltodict_data.get('SearchResults:searchresults', None)['response']['results']['result'])
except:
raise ZillowError({'message': "Zillow did not return a valid response: %s" % data})
return place
def GetDeepComps(self, zws_id, zpid, count=10, rentzestimate=False):
"""
The GetDeepComps API returns a list of comparable recent sales for a specified property.
The result set returned contains the address, Zillow property identifier, and Zestimate for the comparable
properties and the principal property for which the comparables are being retrieved.
This API call also returns rich property data for the comparables.
:param zws_id: The Zillow Web Service Identifier.
:param zpid: The address of the property to search. This string should be URL encoded.
:param count: The number of comparable recent sales to obtain (integer between 1 and 25)
:param rentzestimate: Return Rent Zestimate information if available (boolean true/false, default: false)
:return:
Example
>>> data = api.GetDeepComps("<your key here>", 2100641621, 10)
"""
url = '%s/GetDeepComps.htm' % (self.base_url)
parameters = {'zws-id': zws_id,
'zpid': zpid,
'count': count}
if rentzestimate:
parameters['rentzestimate'] = 'true'
resp = self._RequestUrl(url, 'GET', data=parameters)
data = resp.content.decode('utf-8')
# transform the data to an dict-like object
xmltodict_data = xmltodict.parse(data)
# get the principal property data
principal_place = Place()
principal_data = xmltodict_data.get('Comps:comps')['response']['properties']['principal']
try:
principal_place.set_data(principal_data)
except:
raise ZillowError({'message': 'No principal data found: %s' % data})
# get the comps property_data
comps = xmltodict_data.get('Comps:comps')['response']['properties']['comparables']['comp']
comp_places = []
for datum in comps:
place = Place()
try:
place.set_data(datum)
comp_places.append(place)
except:
raise ZillowError({'message': 'No valid comp data found %s' % datum})
output = {
'principal': principal_place,
'comps': comp_places
}
return output
def GetComps(self, zws_id, zpid, count=25, rentzestimate=False):
"""
The GetComps API returns a list of comparable recent sales for a specified property.
The result set returned contains the address, Zillow property identifier,
and Zestimate for the comparable properties and the principal property for which the comparables are being retrieved.
:param zpid: The address of the property to search. This string should be URL encoded.
:param count: The number of comparable recent sales to obtain (integer between 1 and 25)
:param retnzestimate: Return Rent Zestimate information if available (boolean true/false, default: false)
:return:
"""
url = '%s/GetComps.htm' % (self.base_url)
parameters = {'zws-id': zws_id,
'zpid': zpid,
'count': count}
if rentzestimate:
parameters['rentzestimate'] = 'true'
resp = self._RequestUrl(url, 'GET', data=parameters)
data = resp.content.decode('utf-8')
# transform the data to an dict-like object
xmltodict_data = xmltodict.parse(data)
# get the principal property data
principal_place = Place()
principal_data = xmltodict_data.get('Comps:comps')['response']['properties']['principal']
try:
principal_place.set_data(principal_data)
except:
raise ZillowError({'message': 'No principal data found: %s' % data})
# get the comps property_data
comps = xmltodict_data.get('Comps:comps')['response']['properties']['comparables']['comp']
comp_places = []
for datum in comps:
place = Place()
try:
place.set_data(datum)
comp_places.append(place)
except:
raise ZillowError({'message': 'No valid comp data found %s' % datum})
output = {
'principal': principal_place,
'comps': comp_places
}
return output
def _RequestUrl(self, url, verb, data=None):
"""
Request a url.
:param url: The web location we want to retrieve.
:param verb: GET only (for now).
:param data: A dict of (str, unicode) key/value pairs.
:return:A JSON object.
"""
if verb == 'GET':
url = self._BuildUrl(url, extra_params=data)
try:
return requests.get(
url,
auth=self.__auth,
timeout=self._timeout
)
except requests.RequestException as e:
raise ZillowError(str(e))
return 0
def _BuildUrl(self, url, path_elements=None, extra_params=None):
"""
Taken from: https://github.com/bear/python-twitter/blob/master/twitter/api.py#L3814-L3836
:param url:
:param path_elements:
:param extra_params:
:return:
"""
# Break url into constituent parts
(scheme, netloc, path, params, query, fragment) = urlparse(url)
# Add any additional path elements to the path
if path_elements:
# Filter out the path elements that have a value of None
p = [i for i in path_elements if i]
if not path.endswith('/'):
path += '/'
path += '/'.join(p)
# Add any additional query parameters to the query string
if extra_params and len(extra_params) > 0:
extra_query = self._EncodeParameters(extra_params)
# Add it to the existing query
if query:
query += '&' + extra_query
else:
query = extra_query
# Return the rebuilt URL
return urlunparse((scheme, netloc, path, params, query, fragment))
def _EncodeParameters(self, parameters):
"""
Return a string in key=value&key=value form.
:param parameters: A dict of (key, value) tuples, where value is encoded as specified by self._encoding
:return:A URL-encoded string in "key=value&key=value" form
"""
if parameters is None:
return None
else:
return urlencode(dict([(k, self._Encode(v)) for k, v in list(parameters.items()) if v is not None]))
def _Encode(self, s):
if self._input_encoding:
return str(s, self._input_encoding).encode('utf-8')
else:
return str(s).encode('utf-8')