R/get_pxmart.R

#' PX mart
#'
#' 全聯
#'  
#' @seealso 
#' url: \url{http://www.pxmart.com.tw/px/map.px} \cr
#' data: \url{http://www.pxmart.com.tw/px/store1?name_short=\%25}
#' 
#' @examples
#' \dontrun{
#' tp_metro <- get_pxmart()
#' }
#' 
#' @return data.table
#' @import jsonlite geocode
#' @export
get_pxmart <- function() {
  url <- "http://www.pxmart.com.tw/px/store1?name_short=%25"
  res_list <- GET(url) %>% content()
  out_dt <- rbindlist(res_list, fill = TRUE)
  out_dt %>% setnames(c("name_large", "name_short", "address", "mapx", "mapy"),
                      c("full_nm", "store_nm", "addr", "lon_x", "lat_y"))
  out_dt[lon_x == 0, 
         c("lon_x", "lat_y") := (addr %>% 
           str_replace("^(\\d|[^\\w])+", "") %>% 
           geocode %>% 
           .[, c("lat", "lng")])]
  
  # add brand name
  out_dt[, `:=`(brand_nm = "全聯", keyword = "pxmart")]
  
  ## add url, time, full name
  out_dt[, store_url := "http://www.pxmart.com.tw/"][
    , data_time := format(Sys.time(), "%Y-%m-%dT%H:%M:%S%z")][
      , full_nm := paste0(brand_nm, store_nm)]
  
  key_var <- c("full_nm", "brand_nm", "keyword", "store_nm", "addr",
               "lon_x", "lat_y", "store_url")
  ## move brand_nm, key_word to first two colmuns
  setcolorder(out_dt, c(key_var, setdiff(names(out_dt), key_var)))
  
  out_dt
}
leoluyi/address_crawler documentation built on May 21, 2019, 5:09 a.m.