Created
September 17, 2019 18:12
-
-
Save bfatemi/c9139c228f7acb8da5bef54a1b9c0d18 to your computer and use it in GitHub Desktop.
get comparable homes given a zillow home ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getCompChain <- function(id, n = 100, asDT=FALSE, store = TRUE){ | |
fn_comp <- function(id){ | |
r <- ZillowR::GetComps(zpid = id, count = 25, zws_id = zws) | |
xll <- XML::xmlToList(r$response) | |
count <- ncol(xll["comparables", ][[1]]) | |
res <- lapply(1:count, function(iter){ | |
list( | |
zpid = xll['comparables', ][[1]]['zpid', ][[iter]], | |
address = xll['comparables', ][[1]]['address', ][[iter]] | |
) | |
}) | |
return(res) | |
} | |
env_comp$cList <- NULL | |
env_comp$total <- 0 | |
env_comp$ZID <- NULL | |
## First Iteration | |
ll <- fn_comp(id) | |
new_zids <- sapply(1:length(ll), function(i) ll[[i]]$zpid) | |
index <- which(!new_zids %in% env_comp$ZID) | |
env_comp$ZID <- c(env_comp$ZID, new_zids[index]) | |
env_comp$cList <- c(env_comp$cList, ll[index]) | |
total <- length(env_comp$ZID) | |
## Run remaining iterations | |
tryCatch({ | |
iter <- 1 | |
while(total < n){ | |
ll <- fn_comp(env_comp$ZID[iter]) | |
new_zids <- sapply(1:length(ll), function(i) ll[[i]]$zpid) | |
index <- which(!new_zids %in% env_comp$ZID) | |
env_comp$ZID <- c(env_comp$ZID, new_zids[index]) | |
env_comp$cList <- c(env_comp$cList, ll[index]) | |
total <- length(env_comp$ZID) | |
iter <- iter + 1 | |
# print update | |
cat("\n") | |
print(paste0("iteration: ", iter)) | |
print(paste0("total comps: ", total)) | |
cat("\n") | |
} | |
}, error = function(c){ | |
warning("parsing failed... returning completed data", call. = FALSE) | |
}) | |
if(asDT) | |
return(chainToDT(env_comp$cList)) | |
return(env_comp$cList) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment