Skip to content

Commit

Permalink
make cyclones html access more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Apr 17, 2024
1 parent b2e9b91 commit b009d83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions R/pagasa_cyclones.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
#'
#'

cyclones_get_report_links <- function(url = "https://www.pagasa.dost.gov.ph/tropical-cyclone/publications/annual-report") {
## Quiet down error on SSL ----
httr::config(ssl_verifypeer = 0L) |>
httr::set_config()

cyclones_get_report_links <- function(.url = "https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/tamss/weather/tcsummary/") {
## Initiate an HTML session ----
url_session <- rvest::session(url)
pagasa_session <- rvest::session(.url)

## Retrieve links ----
url_session |>
rvest::html_elements(css = ".panel .panel-body li a") |>
rvest::html_attr(name = "href")
pagasa_session |>
rvest::html_elements(css = "pre a") |>
rvest::html_attr(name = "href") |>
(\(x) x[stringr::str_detect(string = x, pattern = "PAGASA_ARTC")])() |>
(\(x) paste0(.url, x))()
}


Expand All @@ -48,12 +46,15 @@ cyclones_get_report_links <- function(url = "https://www.pagasa.dost.gov.ph/trop
#'

cyclones_download_report <- function(url_link, directory) {
## Create directory path to cyclones ----
dir_path <- file.path(directory, "cyclones")

## Check if directory exists; if not create ----
if (!dir.exists(directory))
dir.create(directory, showWarnings = FALSE, recursive = TRUE)
if (!dir.exists(dir_path))
dir.create(dir_path, showWarnings = FALSE, recursive = TRUE)

## Create file path to download ----
path <- file.path(directory, basename(url_link))
path <- file.path(dir_path, basename(url_link))

## Download file/s ----
Map(
Expand All @@ -67,7 +68,6 @@ cyclones_download_report <- function(url_link, directory) {
}



#'
#' Process cyclones data from reports
#'
Expand Down
2 changes: 1 addition & 1 deletion _targets_cyclones.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ download_targets <- tar_plan(
name = cyclone_reports_download_files,
command = cyclones_download_report(
url_link = cyclone_reports_links,
directory = "data-raw/cyclones"
directory = "data-raw"
),
pattern = map(cyclone_reports_links),
format = "file"
Expand Down

0 comments on commit b009d83

Please sign in to comment.