From e0589ee4d68e5aa600f5a98f21b95aca9cc6992b Mon Sep 17 00:00:00 2001 From: JustinMaxwell Date: Tue, 27 Mar 2018 06:08:23 -0400 Subject: [PATCH] Update api.py Fixed typos. It works with Python3 for me, now (mostly changed printing method). Added handling for rent Zestimate. Added simple test for existence of 'localRealEstate' data. Skip the whole section if it's not present. --- zillow/api.py | 89 ++++++++++----------------------------------------- 1 file changed, 17 insertions(+), 72 deletions(-) diff --git a/zillow/api.py b/zillow/api.py index 1dd5c71..83cb6e4 100644 --- a/zillow/api.py +++ b/zillow/api.py @@ -28,13 +28,13 @@ class ValuationApi(object): >>> data = api.GetSearchResults("", "", "") """ def __init__(self): - self.base_url = "https://www.zillow.com/webservice" + self.base_url = "http://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): + def GetSearchResults(self, zws_id, address, citystatezip, rentzestimate=True): """ 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. @@ -43,7 +43,7 @@ def GetSearchResults(self, zws_id, address, citystatezip, retnzestimate=False): :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) + :param rentzestimate: Return Rent Zestimate information if available (boolean true/false, default: false) :return: """ url = '%s/GetSearchResults.htm' % (self.base_url) @@ -53,23 +53,24 @@ def GetSearchResults(self, zws_id, address, citystatezip, retnzestimate=False): parameters['citystatezip'] = citystatezip else: raise ZillowError({'message': "Specify address and citystatezip."}) - if retnzestimate: - parameters['retnzestimate'] = 'true' + if rentzestimate: + parameters['rentzestimate'] = 'true' resp = self._RequestUrl(url, 'GET', data=parameters) data = resp.content.decode('utf-8') xmltodict_data = xmltodict.parse(data) + message = xmltodict_data.get('SearchResults:searchresults', None)['message']['text'][:5] place = Place() try: place.set_data(xmltodict_data.get('SearchResults:searchresults', None)['response']['results']['result']) - except: + except Exception as e: raise ZillowError({'message': "Zillow did not return a valid response: %s" % data}) return place - def GetZEstimate(self, zws_id, zpid, retnzestimate=False): + def GetZEstimate(self, zws_id, zpid, rentzestimate=True): """ 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. @@ -78,14 +79,14 @@ def GetZEstimate(self, zws_id, zpid, retnzestimate=False): 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) + :param rentzestimate: 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' + if rentzestimate: + parameters['rentzestimate'] = 'true' resp = self._RequestUrl(url, 'GET', data=parameters) data = resp.content.decode('utf-8') @@ -100,7 +101,7 @@ def GetZEstimate(self, zws_id, zpid, retnzestimate=False): return place - def GetDeepSearchResults(self, zws_id, address, citystatezip, retnzestimate=False): + def GetDeepSearchResults(self, zws_id, address, citystatezip, rentzestimate=True): """ 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. @@ -108,10 +109,8 @@ def GetDeepSearchResults(self, zws_id, address, citystatezip, retnzestimate=Fals :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) + :param rentzestimate: 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, @@ -119,8 +118,8 @@ def GetDeepSearchResults(self, zws_id, address, citystatezip, retnzestimate=Fals 'citystatezip': citystatezip } - if retnzestimate: - parameters['retnzestimate'] = 'true' + if rentzestimate: + parameters['rentzestimate'] = 'true' resp = self._RequestUrl(url, 'GET', data=parameters) data = resp.content.decode('utf-8') @@ -135,69 +134,15 @@ def GetDeepSearchResults(self, zws_id, address, citystatezip, retnzestimate=Fals 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("", 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): + def GetComps(self, zws_id, zpid, count=25, rentzestimate=True): """ 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) + :param rentzestimate: Return Rent Zestimate information if available (boolean true/false, default: false) :return: """ url = '%s/GetComps.htm' % (self.base_url)