rOpenSpain / climaemet

R Climate AEMET Tools
https://ropenspain.github.io/climaemet/
GNU General Public License v3.0
42 stars 2 forks source link

Handle several API keys #53

Closed dieghernan closed 7 months ago

dieghernan commented 7 months ago

Hi @dominicroye

This is the best I can think of: now several API Keys can be used:

climaemet::aemet_api_key(c("API1", "API2"), overwrite = TRUE, install = TRUE)

This would choose randomly "API1" or "API2" on each API call, so the API rate limit is not reached so easily, since the main problem is to wait 1 minute after 50 calls.

As a side note, I think the API key is linked to the email account, so it is better to use several API keys requested with several emails... πŸ˜‰

An example, all the forecast for municipalities in Pontevedra (61) in less than 5 minutes. No throttling with 2 API keys, but the overall speed of the AEMET API is still slow.

# remotes::install_github("ropenspain/climaemet@speedup")
# Set up several keys as
# climaemet::aemet_api_key(c("API1", "API2"), overwrite = TRUE, install = TRUE)

library(climaemet)
library(tidyverse)
library(tictoc)
munis <- aemet_munic %>%
  filter(cpro_nombre == "Pontevedra")

nrow(munis)
#> [1] 61

# How many keys?
length(climaemet:::aemet_hlp_get_allkeys())
#> [1] 2

start <- Sys.time()
getfor <- aemet_forecast_daily(munis$municipio)
end <- Sys.time()
difftime(end, start, units = "mins")
#> Time difference of 4.774951 mins

temps <- aemet_forecast_tidy(getfor, "temperatura")
temps
#> # A tibble: 427 Γ— 14
#>    elaborado           municipio nombre provincia id    version uvMax fecha     
#>    <dttm>              <chr>     <chr>  <chr>     <chr>   <dbl> <int> <date>    
#>  1 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1     5 2024-04-23
#>  2 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1     5 2024-04-24
#>  3 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1     5 2024-04-25
#>  4 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1     5 2024-04-26
#>  5 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1     5 2024-04-27
#>  6 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1    NA 2024-04-28
#>  7 2024-04-23 09:14:08 36001     Arbo   Ponteved… -295…       1    NA 2024-04-29
#>  8 2024-04-23 09:14:08 36002     Barro  Ponteved… -295…       1     5 2024-04-23
#>  9 2024-04-23 09:14:08 36002     Barro  Ponteved… -295…       1     5 2024-04-24
#> 10 2024-04-23 09:14:08 36002     Barro  Ponteved… -295…       1     5 2024-04-25
#> # β„Ή 417 more rows
#> # β„Ή 6 more variables: temperatura_maxima <int>, temperatura_minima <int>,
#> #   temperatura_6 <int>, temperatura_12 <int>, temperatura_18 <int>,
#> #   temperatura_24 <int>

length(unique(temps$municipio))
#> [1] 61

Created on 2024-04-23 with reprex v2.1.0

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.3 (2024-02-29 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate Spanish_Spain.utf8 #> ctype Spanish_Spain.utf8 #> tz Europe/Madrid #> date 2024-04-23 #> pandoc 3.1.12.2 @ C:/PROGRA~1/Pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.2) #> climaemet * 1.2.1.9000 2024-04-23 [1] Github (ropenspain/climaemet@f55d696) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0) #> curl 5.2.1 2024-03-01 [1] CRAN (R 4.3.3) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.3) #> dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.2) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.2) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.2) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> ggplot2 * 3.5.1 2024-04-23 [1] CRAN (R 4.3.3) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.2) #> gtable 0.3.5 2024-04-22 [1] CRAN (R 4.3.3) #> hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.0) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.3) #> httr2 1.0.1 2024-04-01 [1] CRAN (R 4.3.3) #> jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.3.2) #> knitr 1.46 2024-04-06 [1] CRAN (R 4.3.3) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> munsell 0.5.1 2024-04-01 [1] CRAN (R 4.3.3) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) #> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0) #> R.oo 1.26.0 2024-01-24 [1] CRAN (R 4.3.2) #> R.utils 2.12.3 2023-11-18 [1] CRAN (R 4.3.2) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> rappdirs 0.3.3 2021-01-31 [1] CRAN (R 4.3.0) #> readr * 2.1.5 2024-01-10 [1] CRAN (R 4.3.2) #> reprex 2.1.0 2024-01-11 [1] CRAN (R 4.3.2) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.2) #> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.3.3) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.3.3) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.2) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) #> stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.2) #> stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.2) #> styler 1.10.3 2024-04-07 [1] CRAN (R 4.3.3) #> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> tictoc * 1.2.1 2024-03-18 [1] CRAN (R 4.3.3) #> tidyr * 1.3.1 2024-01-24 [1] CRAN (R 4.3.2) #> tidyselect 1.2.1 2024-03-11 [1] CRAN (R 4.3.3) #> tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0) #> timechange 0.3.0 2024-01-18 [1] CRAN (R 4.3.2) #> tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.0) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.2) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.2) #> xfun 0.43 2024-03-25 [1] CRAN (R 4.3.3) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.2) #> #> [1] C:/Users/diego/AppData/Local/R/win-library/4.3 #> [2] C:/Program Files/R/R-4.3.3/library #> #> ────────────────────────────────────────────────────────────────────────────── ```