R/get_districts.R

#' @title Get the list of districts 
#' @description Collect data on districts 
#' @param address_town id of the country referring to get_towns()
#' @param print_steps Print out proress 
#' @return data.frame of districts
#' @export
#' @examples 
#' get_districts(address_town = 438) # list of districts in Adalar, Istanbul, Turkey
#' get_districts(address_town = 1)
#' get_districts(address_town = 257, print_steps = TRUE)
#' get_districts(address_town = 1:2)
#' @importFrom magrittr "%>%"
get_districts <- function(address_town = 421, print_steps = F){
  # notice that one quarter might belong to more than 1 district such as 
  # quarter 22677 belongs both district 999028 and 1995
  
  base_url <- "https://www.sahibinden.com/ajax/location/loadDistrictsByTownIds"
  final_url <- httr::parse_url(base_url)
  final_url$query <- c(
    sapply(address_town, function(x) list(address_town = 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("Districts are retrieved with success\n")
    content <- httr::content(response)

    flatten_district <- function(d){
      district <- flatten(d[names(d)!="quarters"])
      quarters <- lapply(unlist(d[names(d)=="quarters"], recursive = F), flatten) %>% 
        dplyr::bind_rows() %>%
        dplyr::rename_all(~paste0("quarter_", . ))
      combined <- district %>% 
        dplyr::left_join(quarters, by = c("id" = "quarter_districtId"))
      return(combined)
    }
      
    districts_with_quarters <-  lapply(unlist(content$data, recursive = F), flatten_district) %>%
      dplyr::bind_rows() %>% 
      dplyr::as.tbl() %>%
      dplyr::select(dplyr::matches("^[^_]+$"), 
                    dplyr::contains("town"), 
                    dplyr::contains("town_city"), 
                    dplyr::contains("quarter"),
                    dplyr::everything()
      )
    
    return(districts_with_quarters)
  } else {
    if(print_steps) cat("Data not found!\n")
    return(NULL)
  }
}
bhakyuz/sahibinden documentation built on June 12, 2019, 2:28 p.m.