Prof. Walmes Marques Zeviani
21 Mar 2017
# Lista a tabela de execução programada.
crontab -l
Min | Hora | DM | Mês | DS | Comando |
---|---|---|---|---|---|
0-59 | 0-23 | 1-31 | 1-12 | 0-7 | /caminh/comando.sh |
Jan-Dec | Sun-Sat |
Usando o Open Weather Map
url <- paste0("http://samples.openweathermap.org/data/2.5/",
"weather?id=%d&appid=%s")
url <- sprintf(url,
6322752,
"6eb4d970f22e507866028152781d8dd5")
# Pega a hora do sistema no formato inteiro (segundos desde 1970-01-01).
i <- as.integer(Sys.time())
# Hora de Greenwich.
as.POSIXct(i, origin = "1970-01-01", tz = "UTC")
# A time zone do sistema operacional.
Sys.timezone()
# Coonvertendo a data no formato inteiro para o cronológico.
as.POSIXct(i, origin = "1970-01-01", tz = "America/Sao_Paulo")
# Opção 1: Lê e escreve.
h <- readLines(url, warn = FALSE)
writeLines(h, con = sprintf("%s.json", i))
# Opção 2: Download.
download.file(url = url,
destfile = sprintf("%s.json", i))
# Opção 3. Lê e anexa ao final.
h <- readLines(url, warn = FALSE)
write.table(h,
file = "clima.json",
append = TRUE,
quote = FALSE,
col.names = FALSE,
row.names = FALSE,
sep = "")
# ATTENTION: isso vai exigir processamento para ser transformado em um
# array JSON, mas é coisa que dá para fazer elegantemente com Shell.
#---- weather-cwb.R ----------------------------------------------------
setwd("~/repos/mintex/slides")
url <- paste0("http://samples.openweathermap.org/data/2.5/",
"weather?id=%d&appid=%s")
url <- sprintf(url,
6322752,
"6eb4d970f22e507866028152781d8dd5")
i <- as.integer(Sys.time())
try(download.file(url = url,
destfile = sprintf("%s.json", i)),
silent = TRUE)
# Notificação de aúdio.
if (Sys.info()["sysname"] == "Linux") {
# Testa se o espeak está presente.
i <- system("espeak --version > /dev/null 2>&1; echo $?",
inter = TRUE)
if (i == "0") {
system("espeak -vpt -s 90 'Leitura concluída.'")
}
}
#-----------------------------------------------------------------------
# Abrir o contrab (-e = editar).
crontab -e
# m h dom mon dow command
*/15 * * * * Rscript /home/walmes/repos/mintex/slides/weather-cwb.R
Ler as manchetes de um Jornal eletrônico a cada hora.
# m h dom mon dow command
0 * * * * Rscript ~/noticias.R
# m h dom mon dow command
*/5 11-15 * * 1,4,6 Rscript ~/porta-dos-fundos.R