bhubbard
10/31/2016 - 9:34 PM

Get Zillow Estimate and Image from Zillow's API, Quick and Dirty

Get Zillow Estimate and Image from Zillow's API, Quick and Dirty

/* Quick and dirty, should be wrapped better but this is just a demo */

var app = app || {};
app.apikeys = app.apikeys || {};
app.urls = app.urls || {};
app.methods = app.methods || {};

app.apikeys.zillow = 'YOUR API KEY HERE';

app.urls.zillow = {
  getSearchResults: 'http://www.zillow.com/webservice/GetSearchResults.htm',
  getUpdatedPropertyDetails: 'http://www.zillow.com/webservice/GetUpdatedPropertyDetails.htm'
};

/*
  @input: address, city, state, zip,
  @callback: function(error, output)
  @return: valid input
*/
app.methods.getZillowEstimate = function(input, callback) {
    var key = app.apikeys.zillow;

    var address, city, state, citystate, zip;
    var address = input.address;
    var city = input.city;
    var state = input.state;
    var citystate = city+', '+state;
    var zip = input.zip;

    var response = {};

    var citystatezip = zip;
    if(!zip && !city && !state) {
      return false;
    } else if(city && state) {
      citystatezip = citystate;
    }

    $.post(app.urls.zillow.getSearchResults, 
      { 
        'zws-id': app.apikeys.zillow, 
        'address': address, 
        'citystatezip': citystatezip, 
        'rentzestimate': true 
      }, 
    function(xml){ 
      var $xml = $(xml);

      var amount = $xml
        .find('response')
        .find('results')
        .find('result:eq(0)')
        .find('zestimate')
        .find('amount').text();

      response.marketValue = amount;

      var zpid = $xml
        .find('response')
        .find('results')
        .find('result:eq(0)')
        .find('zpid').text();

      if(!zpid) {
        return callback(null, response);
      }

      $.post(app.urls.zillow.getUpdatedPropertyDetails, 
        {
          'zws-id': key,
          'zpid': zpid
        },
      function(x) {
        $x = $(x);

        var img = $x
          .find('response')
          .find('images')
          .find('image:eq(0)')
          .find('url').text();

        response.image = img;

        return callback(null, response);

      })
    });
  };