R/get_towns.R

#' @title Get the list of towns 
#' @description Collect data on towns  
#' @param address_city id of the city referring to get_cities()
#' @param print_steps Print out proress 
#' @return data.frame of towns
#' @export
#' @examples 
#' get_towns(address_city = 34) # list of towns in Istanbul, Turkey
#' get_towns(address_city = 10001, print_steps = TRUE)
#' get_towns(address_city = 1:2)
#' @importFrom magrittr "%>%"
get_towns <- function(address_city = 34, print_steps = F){
  
    base_url <- "https://www.sahibinden.com/ajax/location/loadTownsByCityIds"
    final_url <- httr::parse_url(base_url)
    final_url$query <- c(
      sapply(address_city, function(x) list(address_city = x)),
      vcIncluded = T)
    
    response <- httr::GET(
      url = httr::build_url(final_url), 
      if(print_steps) httr::verbose() else NULL
    )
    
    if(httr::status_code(response) == 200) {
      if(print_steps) cat("Towns are retrieved with success\n")
      content <- httr::content(response)
      
      towns <-  lapply(unlist(content$data, recursive = F), flatten) %>%
        dplyr::bind_rows() %>% 
        dplyr::as.tbl() %>%
        dplyr::select(dplyr::matches("^[^_]+$"), 
                      dplyr::contains("city"), 
                      dplyr::contains("detail"),
                      dplyr::everything()
                      )
      return(towns)
    } else {
      if(print_steps) cat("Data not found!\n")
      return(NULL)
    }

}
bhakyuz/sahibinden documentation built on June 12, 2019, 2:28 p.m.