Advertisement
celestialgod

severEleven location

Jun 23rd, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.10 KB | None | 0 0
  1. library(httr)
  2. library(xml2)
  3. library(pipeR)
  4. library(purrr)
  5. library(stringi)
  6. library(stringr)
  7. library(RCurl)
  8. library(jsonlite)
  9. library(dplyr)
  10.  
  11. url_postCodes <- "http://www.easytravel.com.tw/postid_search.asp"
  12. all_postCodes <- url_postCodes %>>% GET %>>% content(encoding = "big5") %>>%
  13.   xml_find_all("//td/font") %>>% xml_text %>>%
  14.   stri_conv(from = "UTF-8", to = "Big5") %>>% str_extract("\\d{3}") %>>%
  15.   `[`(!is.na(.)) %>>% as.integer
  16.  
  17. taiwanCityTown <- lapply(all_postCodes, function(postCode){
  18.   url_toPrint <- str_c("http://api.map.com.tw/net/GraphicsXY.aspx?",
  19.                        "search_class=Zip&Zip=%i&fun=getCityByZipReturn")
  20.   sprintf(url_toPrint, postCode) %>>%
  21.     GET(config(referer = "http://www.family.com.tw/marketing/inquiry.aspx")) %>>%
  22.     content("text") %>>% str_replace_all("getCityByZipReturn\\(|\\)", "") %>>% fromJSON
  23. }) %>>% do.call(what = rbind) %>>% filter(nchar(COUNTY) > 0, nchar(TOWN) > 0)
  24.  
  25. sevenElevenStoreLoc <- taiwanCityTown %>>% as.matrix %>>% apply(1, function(v){
  26.   url_toPrint <- str_c("https://emap.pcsc.com.tw/EMapSDK.aspx?",
  27.         "commandid=SearchStore&city=%s&town=%s&ID=&StoreName=&",
  28.         "SpecialStore_Kind=&isDining=False&isParking=False&isLavatory=False&",
  29.         "isATM=False&is7WiFi=False&isIce=False&isHotDog=False&",
  30.         "isHealthStations=False&isIceCream=False&isOpenStore=False&",
  31.         "isFruit=False&isCityCafe=False&isUp=False&isOrganic=False&",
  32.         "isCorn=False&isMakeUp=False&isMuji=False&isMD=False&",
  33.         "isStarBucks=False&isIbon=False&isTea=False")
  34.   storeLoc <- sprintf(url_toPrint, v[1], v[2]) %>>%
  35.     GET(config(referer = "https://emap.pcsc.com.tw")) %>>%
  36.     content("text") %>>% read_xml %>>% xml_find_all("GeoPosition")
  37.   if (length(storeLoc) == 0)
  38.     return(NULL)
  39.  
  40.   colNames <- storeLoc %>>% '[['(1) %>>% xml_children %>>% xml_name
  41.   storeLoc %>>% xml_children %>>% xml_text %>>% stri_conv("UTF-8", "BIG5") %>>%
  42.     matrix(length(colNames)) %>>% t %>>% data.frame(stringsAsFactors = FALSE) %>>%
  43.     `names<-`(colNames)
  44. }) %>>% do.call(what = rbind) %>>%
  45.   mutate_each(funs(str_replace_all(., "\\s", "")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement