forked from richaben/PRR_ONDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_verifier_maj.R
65 lines (55 loc) · 1.95 KB
/
01_verifier_maj.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
##%##################%##
# #
#### Vérifier MAJ ####
# #
##%##################%##
'%>%' <- dplyr::'%>%'
date_jour <- as.character(format(Sys.time(),"%Y-%m-%d"))
date_jour_heure <- as.character(format(Sys.time(),"%Y-%m-%d_%Hh%m"))
source("_config.R")
if (!file.exists("data/onde_data/onde.csv") |
!file.exists("data/onde_data/dernieres_obs.csv") |
!file.exists("data/processed_data/donnees_pour_graphiques.rda") |
!file.exists("data/processed_data/graphiques.rda") |
!file.exists("data/processed_data/donnees_cartes.rda")) {
to_update <- TRUE
} else {
old_data <- read.csv2("data/onde_data/dernieres_obs.csv", colClasses = "character") %>%
dplyr::mutate(date_observation = lubridate::as_date(date_observation, format = "%Y-%m-%d")) %>%
dplyr::rename(old = date_observation)
new_data <- purrr::map_df(
.x = conf_dep,
.f = function(d) {
data.frame(
code_departement = d,
date_observation = readLines(
paste0(
"https://hubeau.eaufrance.fr/api/v1/ecoulement/observations?format=json&code_departement=",
d, "&size=1&fields=date_observation&sort=desc"
)
) %>%
stringr::str_extract(pattern = "\\d{4}-\\d{2}-\\d{2}")
)
}
) %>%
dplyr::rename(new = date_observation)
if (
any(! old_data$code_departement %in% new_data$code_departement) |
any(! new_data$code_departement %in% old_data$code_departement)
) {
to_update <- TRUE
} else {
to_update <- dplyr::left_join(
x = old_data,
y = new_data,
by = "code_departement"
) %>%
dplyr::select(old, new) %>%
t() %>%
duplicated() %>%
any() %>%
'!'()
}
}
save(to_update, date_jour, date_jour_heure, file = "data/onde_data/to_update.rda")
print(switch(as.character(to_update), `TRUE` = "Mise-à-jour requise", `FALSE` = "Pas de mise-à-jour requise"))