R Advent Calendar 2014に記事が追加されたらメールが届くようにした

R Advent Calendar 2014 @ Qiita 7日目)
R Advent Calendar 2014は今回2種類ある。
ATNDの方
Qiitaの方

Qiitaの方は通知をくれるが、ATNDの方は通知をくれない。
ということで記事が追加されたらその旨gmailでメールを送るコードを書いた。
これをcronとかでまわしてやれば良い。

library(rvest)
result <- html("https://atnd.org/events/58648") %>% html_nodes("td") %>% html_nodes("a") %>% html_attr("href")
if(!file.exists("result_last.rds")){
  saveRDS(result, file = "result_last.rds")  
} else{
  result_last <- readRDS("result_last.rds")
  saveRDS(result, file = "result_last.rds")
  URL <- result %>% setdiff(result_last)
  if(length(URL)>0){  
    # gmail で URLを送る
    library(mailR) # Java runtimeが無いと突然落ちるので注意
    send.mail(from = "dichika<hoge@gmail.com>",
              to = "xxxxxxxxxxxxxxx@i.softbank.jp",
              subject = "カレンダーが更新されました!!!!!! ",
              body = URL,
              encoding = "utf-8",
              html = FALSE,
              smtp = list(host.name = "smtp.gmail.com", 
                          port = 465, 
                          user.name = "hoge@gmail.com",
                          # 2段階認証にしてる場合はhttps://security.google.com/settings/security/apppasswordsでpasswordを生成しておくこと
                          passwd = "xxxxxxxxxxxxxxx", 
                          ssl = TRUE),
              authenticate = TRUE,
              send = TRUE)    
  }
}

enjoy!!!