diff --git a/.github/workflows/deploy-targets-downloads.yml b/.github/workflows/deploy-targets-downloads.yml index bfde6e1..56869fd 100644 --- a/.github/workflows/deploy-targets-downloads.yml +++ b/.github/workflows/deploy-targets-downloads.yml @@ -4,8 +4,6 @@ on: schedule: - cron: "0 7 * * *" workflow_dispatch: - branches: - - '*' jobs: deploy-targets-download: diff --git a/.github/workflows/deploy-targets-releases.yml b/.github/workflows/deploy-targets-releases.yml index 87dbc0d..6f3dd7b 100644 --- a/.github/workflows/deploy-targets-releases.yml +++ b/.github/workflows/deploy-targets-releases.yml @@ -4,8 +4,6 @@ on: schedule: - cron: "0 17 * * SUN" workflow_dispatch: - branches: - - '*' jobs: deploy-targets-releases: diff --git a/.github/workflows/test-targets-climate.yml b/.github/workflows/test-targets-climate.yml index 01366ff..11d356b 100644 --- a/.github/workflows/test-targets-climate.yml +++ b/.github/workflows/test-targets-climate.yml @@ -4,8 +4,6 @@ on: pull_request: branches: [main, master] workflow_dispatch: - branches: - - '*' jobs: test-targets-climate: diff --git a/.github/workflows/test-targets-forecasts-agriculture.yml b/.github/workflows/test-targets-forecasts-agriculture.yml new file mode 100644 index 0000000..cb6e7ca --- /dev/null +++ b/.github/workflows/test-targets-forecasts-agriculture.yml @@ -0,0 +1,36 @@ +name: test targets forecasts agriculture + +on: + pull_request: + branches: [main, master] + workflow_dispatch: + +jobs: + test-targets-forecasts-agriculture: + runs-on: ubuntu-latest + container: rocker/tidyverse:4.4.1 + steps: + - uses: actions/checkout@v4 + + - name: Install system dependencies + run: | + apt-get update && apt-get install -y --no-install-recommends \ + libxt6 libglpk-dev libpoppler-cpp-dev libmagick++-dev \ + libtesseract-dev libleptonica-dev tesseract-ocr-eng + + - name: Install packages from renv.lock (with cache) + if: ${{ !env.ACT }} + uses: r-lib/actions/setup-renv@v2 + with: + cache-version: 2 + + - name: Install packages from renv.lock (local, no cache) + if: ${{ env.ACT }} + run: | + renv::restore() + shell: Rscript {0} + + - name: Run workflow + run: | + targets::tar_make(dplyr::starts_with("forecasts_agriculture")) + shell: Rscript {0} diff --git a/.github/workflows/test-targets-forecasts.yml b/.github/workflows/test-targets-forecasts.yml index d6b0b9b..b397f60 100644 --- a/.github/workflows/test-targets-forecasts.yml +++ b/.github/workflows/test-targets-forecasts.yml @@ -4,8 +4,6 @@ on: pull_request: branches: [main, master] workflow_dispatch: - branches: - - '*' jobs: test-targets-forecasts: diff --git a/R/pagasa_forecasts.R b/R/pagasa_forecasts.R index a481006..f6db723 100644 --- a/R/pagasa_forecasts.R +++ b/R/pagasa_forecasts.R @@ -24,7 +24,7 @@ forecasts_create_urls <- function() { #' #' Download a PAGASA regional weather forecast PDF #' -#' @param url A URL to a PAGASA regional weather forecasts PDF. +#' @param .url A URL to a PAGASA regional weather forecasts PDF. #' @param directory A direcotry path to download the PAGASA regional weather #' forecasts PDF to. Default is to current working directory. #' @param overwrite Logical. Should an existing file with the same file path be @@ -41,7 +41,7 @@ forecasts_create_urls <- function() { #' @export #' -forecasts_download <- function(url, +forecasts_download <- function(.url, directory = "data-raw/forecasts", overwrite = FALSE) { ## Quiet down ssl verification ---- @@ -52,7 +52,7 @@ forecasts_download <- function(url, destfile <- file.path( directory, Sys.Date(), stringr::str_remove( - string = url, + string = .url, pattern = "https://src.panahon.gov.ph/pubfiles/prsd/" ) ) @@ -76,12 +76,12 @@ forecasts_download <- function(url, ) { ## Download file ---- #download.file(url = url, destfile = destfile) - curl::curl_download(url = url, destfile = destfile, handle = h) + curl::curl_download(url = .url, destfile = destfile, handle = h) } else { if (overwrite) { ## Download file ---- #download.file(url = url, destfile = destfile) - curl::curl_download(url = url, destfile = destfile, handle = h) + curl::curl_download(url = .url, destfile = destfile, handle = h) } } diff --git a/R/pagasa_forecasts_agriculture.R b/R/pagasa_forecasts_agriculture.R new file mode 100644 index 0000000..64ff0c0 --- /dev/null +++ b/R/pagasa_forecasts_agriculture.R @@ -0,0 +1,54 @@ +#' +#' Download PAGASA daily agriculture forecasts +#' +#' @param directory Path to directory to download the daily agriculture +#' forecast PDF to. +#' @param overwrite Logical. Should an existing file with the same file path be +#' overwritten? Default is FALSE. +#' +#' @returns The path to the downloaded daily agriculture forecast PDF. +#' +#' @examples +#' forecasts_agriculture_download() +#' +#' @rdname forecasts_agriculture +#' @export +#' + +forecasts_agriculture_download <- function(directory = "data-raw/forecasts_agriculture", + overwrite = FALSE) { + if (weekdays(Sys.Date()) %in% c("Saturday", "Sunday")) { + .url <- "https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/agriculture/weekend_special_farm_forecast/weekend_special_farm_weather_forecast.pdf" + } else { + .url = "https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/agriculture/farm_weather_forecast/farm_weather_forecast.pdf" + } + + ## Quiet down ssl verification ---- + #h <- curl::new_handle() + #curl::handle_setopt(h, .list = list(ssl_verifypeer = 0L)) + + ## Create file path to download ---- + destfile <- file.path(directory, paste0(Sys.Date(), ".pdf")) + + ## Create directories as needed ---- + if (!dir.exists(directory)) dir.create(directory) + + ## Download PDF ---- + if ( + !destfile %in% list.files(directory, full.names = TRUE) | + length(list.files(directory, full.names = TRUE)) == 0 + ) { + ## Download file ---- + download.file(url = .url, destfile = destfile) + #curl::curl_download(url = url, destfile = destfile, handle = h) + } else { + if (overwrite) { + ## Download file ---- + download.file(url = .url, destfile = destfile) + #curl::curl_download(url = url, destfile = destfile, handle = h) + } + } + + ## Return path to downloaded file ---- + destfile +} \ No newline at end of file diff --git a/_targets_forecasts.R b/_targets_forecasts.R index e6ee0e9..0a27583 100644 --- a/_targets_forecasts.R +++ b/_targets_forecasts.R @@ -21,6 +21,12 @@ forecasts_download_targets <- tar_plan( ), pattern = map(forecasts_pubfiles_urls), format = "file" + ), + ### Download PAGASA agriculture forecasts PDF ---- + tar_target( + name = forecasts_agriculture_download_files, + command = forecasts_agriculture_download(), + format = "file" ) ) @@ -39,6 +45,14 @@ forecasts_data_targets <- tar_plan( name = forecasts_data_raw, command = forecasts_get_data(forecasts_archive_pdfs), pattern = map(forecasts_archive_pdfs) + ), + ### List PAGASA agriculture forecasts data files ---- + tar_target( + name = forecasts_agriculture_archive_pdfs, + command = list.files( + path = "data-raw/forecasts_agriculture", + full.names = TRUE, recursive = TRUE + ) ) ) diff --git a/data-raw/forecasts_agriculture/2024-09-08.pdf b/data-raw/forecasts_agriculture/2024-09-08.pdf new file mode 100644 index 0000000..92ac82e Binary files /dev/null and b/data-raw/forecasts_agriculture/2024-09-08.pdf differ