#' @title Get the list of cities
#' @description Collect data on cities
#' @param address_country id of the country referring to get_countries()
#' @param print_steps Print out proress
#' @return data.frame of cities
#' @export
#' @examples
#' get_cities(address_country = 1) # list of cities in Turkey
#' get_cities(address_country = 257, print_steps = TRUE)
#' @importFrom magrittr "%>%"
get_cities <- function(address_country = 1, print_steps = F){
base_url <- "https://www.sahibinden.com/ajax/location/loadCitiesByCountryId"
final_url <- httr::parse_url(base_url)
final_url$query <- list(address_country = address_country, 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("Cities are retrieved with success\n")
content <- httr::content(response)
cities <- lapply(unlist(content$data, recursive = F), flatten) %>%
dplyr::bind_rows() %>%
dplyr::as.tbl()
return(cities)
} else {
if(print_steps) cat("Data not found!\n")
return(NULL)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.