# Zillow API Key: https://www.zillow.com/howto/api/APIOverview.htm


# 0.1 Install ----
#library(devtools)
#devtools::install_github("sptrsn/IDX",force = TRUE)

# clear environment variables
rm(list=ls())

# 1.0 Load LIBRARIES ----
library(IDX)

# unsure how many of these libs are actually required
library(config)

# EDA
library(DataExplorer)
library(skimr)

# Modeling
library(recipes)
library(parsnip)
library(yardstick)
library(DALEX)
library(iBreakDown)

# Core
library(tidyverse)

library(lubridate)
library(leaflet)
library(plotly)
library(tidyquant)
library(plyr)
library(stringr)
library(dplyr)
library(rvest)
library(sp)

# 2.0 Zillow API Keys ----
keys <- c(
    'yourkey1',
    'yourkey2'
    )

# 3.0 Fetch properties ---- 
#######################
#
# fetch your file
# file needs headers. 
# make sure header name are cased and spelled the same as here below
# inside the trycatch, make sure the column numbers are set to match your file.
#
#######################
properties <- read.csv("pathtoyourfile.csv",header=TRUE)
properties<-properties %>% 
    select(address,city,state,zip)

properties$address<-str_trim(properties$address)
properties$city<-str_trim(properties$city)
properties$state<-str_trim(properties$state)


# 3.1 create containers for Zillow & error data ----
zillowData = data.frame()
failed = data.frame()

# 3.2 create counter to track key usage ----
keycounter <- 1

# 4.0 Loop properties ----
for(i in 1:nrow(properties)){

    tryCatch(
        {

            # 4.1 Modify col numbers (2,3,4,5) to correspond to address, city, state & zip ----
            address <-as.character(properties[i,1])
            city    <-as.character(properties[i,2])
            state   <-as.character(properties[i,3])
            zipcode <-as.numeric(properties[i,4])

            current<-data.frame(address,city,state,zipcode)

            # 4.2 call zillow api for property i ----
            response <- GetDeepSearchResults(
                address = address, 
                city    = city,
                state   = state,
                zipcode = zipcode,
                api_key = keys[keycounter],
                raw = FALSE
            ) 

            # 4.3 increment | reset api key ----
            if(keycounter == length(keys)){ 
                keycounter <- 1
            }else{
                keycounter <- keycounter + 1
            }

            if(length(response)==1){

                x<-c("Failed: ",address)
                print(x)

                df <- data.frame(matrix(unlist(current), nrow=1, byrow=T),stringsAsFactors=FALSE)
                failed <- rbind.fill(failed,current)

                next
            }else{

                #apend zillow response to the final zillowData container
                x<-c("Success: ",address)
                print(x)

                zillowData <- rbind.fill(zillowData,response)
            }

        },
        error=function(e){
            cat("ERROR :",conditionMessage(e), "\n")
        }
    )

}# !End for loop

# 5.0 View final zillowData output ----
View(zillowData)

# 6.0 display properties if errors ----
if(nrow(failed) == 0){

    print("No Errors")

}else{

    #print("Failed properties being displayed")
    # fieldNames<-c("address","city","state","zipcode")
    # names(failed)<-fieldNames
    View(failed)

}

# 7.0 End ----


sptrsn/IDX documentation built on May 1, 2020, 4:03 p.m.