-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from panukatan/dev
create functions for getting bulletins; fix #42
- Loading branch information
Showing
17 changed files
with
406 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,8 @@ Imports: | |
httr, | ||
rlang, | ||
rvest, | ||
stringr | ||
stringr, | ||
tibble | ||
Suggests: | ||
spelling, | ||
testthat (>= 3.0.0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#' | ||
#' Get PHIVOLCS earthquake information bulletins | ||
#' | ||
#' @param .url Base URL for PHIVOLCS earthquake bulletins. | ||
#' @param .year A vector for year (in YYYY format) for which earthquake | ||
#' bulletins are to be retrieved. The earliest year that can be specified is | ||
#' 2018. If set to NULL (default), all years starting from 2018 to present | ||
#' year are used. | ||
#' @param .month A vector for month for which earthquake bulletins are | ||
#' to be retrieved. This can be set as either an integer index (1 for January) | ||
#' or abbreviation (Jan for January) for full name. If set to NULL (default), | ||
#' all months are used. | ||
#' @param latest Logical. Should the latest table of earthquake information be | ||
#' retrieved? Only evaluated if `.year = NULL` and `.month = NULL`. If TRUE | ||
#' (default), table of earthquake information for current year and current | ||
#' month is retrieved. Otherwise, all months for all possible years are | ||
#' retrieved. | ||
#' | ||
#' @returns A character vector of URLs for PHIVOLCS earthquake information | ||
#' bulletins. | ||
#' | ||
#' @examples | ||
#' eq_get_bulletin_urls() | ||
#' | ||
#' @rdname eq_get_bulletin_urls | ||
#' @export | ||
#' | ||
|
||
eq_get_bulletin_urls <- function(.url = "https://earthquake.phivolcs.dost.gov.ph/", | ||
.year = NULL, .month = NULL, latest = TRUE) { | ||
## Build URLs ---- | ||
if (is.null(.year) & is.null(.month)) { | ||
if (latest) { | ||
urls <- .url | ||
} else { | ||
urls <- eq_build_url(.url = .url, .year = .year, .month = .month) | ||
} | ||
} else { | ||
urls <- eq_build_url(.url = .url, .year = .year, .month = .month) | ||
} | ||
|
||
## Quiet down error on SSL ---- | ||
httr::config(ssl_verifypeer = 0L) |> | ||
httr::set_config() | ||
|
||
## Retrieve URLs ---- | ||
lapply( | ||
X = urls, | ||
FUN = eq_get_bulletin_urls_ | ||
) |> | ||
unlist() | ||
} | ||
|
||
#' | ||
#' @rdname eq_get_bulletin_urls | ||
#' @export | ||
#' | ||
|
||
eq_get_bulletin_urls_ <- function(.url) { | ||
## Detect year and month from URL ---- | ||
.year <- stringr::str_extract(string = .url, pattern = "[0-9]{4}") |> | ||
as.integer() | ||
.month <- stringr::str_extract( | ||
string = .url, pattern = paste(month.name, collapse = "|") | ||
) | ||
|
||
## Quiet down error on SSL ---- | ||
httr::config(ssl_verifypeer = 0L) |> | ||
httr::set_config() | ||
|
||
## Retrieve links ---- | ||
if (.year == 2018 & .month %in% month.name[seq_len(5)]) { | ||
rvest::session(.url) |> | ||
rvest::html_elements(css = "tr td .auto-style49 a") |> | ||
rvest::html_attr(name = "href") |> | ||
(\(x) | ||
{ | ||
file.path( | ||
"https:/", | ||
stringr::str_split_fixed(.url, pattern = "/", n = 4)[ , 3], | ||
stringr::str_remove_all(string = x, pattern = "^../../") | ||
) | ||
} | ||
)() | ||
} else { | ||
rvest::session(.url) |> | ||
rvest::html_elements(css = ".auto-style91 a") |> | ||
rvest::html_attr(name = "href") |> | ||
(\(x) | ||
{ | ||
file.path( | ||
"https:/", | ||
stringr::str_split_fixed(.url, pattern = "/", n = 4)[ , 3], | ||
stringr::str_remove_all( | ||
string = x, pattern = "^../../|\\\\..\\\\..\\\\" | ||
) |> | ||
stringr::str_replace_all(pattern = "\\\\", replacement = "/") | ||
) | ||
} | ||
)() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#' | ||
#' Retrieve raw information from PHIVOLCS earthquake information bulletins | ||
#' | ||
#' @param .url A character value or vector of values for PHIVOLCS earthquake | ||
#' information bulletins | ||
#' | ||
#' @returns A tibble of earthquake information from PHIVOLCS bulletins | ||
#' | ||
#' @examples | ||
#' urls <- eq_get_bulletin_urls() | ||
#' eq_get_bulletins(urls[1:3]) | ||
#' | ||
#' @rdname eq_get_bulletin | ||
#' @export | ||
#' | ||
|
||
eq_get_bulletin <- function(.url) { | ||
rvest::session(url = .url) |> | ||
rvest::html_table() |> | ||
(\(x) x[[1]])() |> | ||
(\(x) | ||
{ | ||
tibble::tibble( | ||
date_time = x[2, 4], | ||
bulletin_number = stringr::str_extract( | ||
string = x[1, 1], pattern = "[0-9]{1,}" | ||
), | ||
depth = x[2, 8], | ||
magnitude = x[2, 12], | ||
location = x[2, 6], | ||
origin = x[2, 10], | ||
reported_intensity = x[9, 3], | ||
expect_damage = x[11, 4], | ||
expect_aftershocks = x[11, 6], | ||
date_time_issued = x[11, 8], | ||
prepared_by = x[11, 10] | ||
) |> | ||
dplyr::mutate( | ||
dplyr::across( | ||
.cols = dplyr::everything(), | ||
.fns = ~simplify_vectors(.x) | ||
) | ||
) | ||
} | ||
)() | ||
} | ||
|
||
#' | ||
#' @rdname eq_get_bulletin | ||
#' @export | ||
#' | ||
|
||
eq_get_bulletins <- function(.url) { | ||
lapply( | ||
X = .url, | ||
FUN = eq_get_bulletin | ||
) |> | ||
dplyr::bind_rows() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#' | ||
#' Process earthquake information from PHIVOLCS bulletins | ||
#' | ||
#' @param eq_df A tibble of earthquake information from PHIVOLCS bulletins | ||
#' retrieved using `eq_get_bulletins()`. | ||
#' | ||
#' @returns A tibble of processed earthquake information from PHIVOLCS | ||
#' bulletins. | ||
#' | ||
#' @examples | ||
#' urls <- eq_get_bulletin_urls() | ||
#' eq_get_bulletins(urls[1:3]) |> | ||
#' eq_process_bulletins() | ||
#' | ||
#' @rdname eq_process_bulletin | ||
#' @export | ||
#' | ||
|
||
eq_process_bulletins <- function(eq_df) { | ||
eq_df |> | ||
dplyr::mutate( | ||
date_time = strptime( | ||
.data$date_time, format = "%d %B %Y - %I:%M:%S %p", tz = "PST" | ||
), | ||
bulletin_number = as.integer(.data$bulletin_number), | ||
longitude = get_longitude(.data$location), | ||
latitude = get_latitude(.data$location), | ||
depth = as.integer(.data$depth), | ||
magnitude = stringr::str_remove_all( | ||
string = .data$magnitude, pattern = "Ms " | ||
) |> | ||
as.numeric(), | ||
location = get_location(.data$location), | ||
origin = tolower(.data$origin), | ||
reported_intensity = ifelse( | ||
.data$reported_intensity == "", NA_character_, .data$reported_intensity | ||
), | ||
expect_damage = tolower(.data$expect_damage), | ||
expect_aftershocks = tolower(.data$expect_aftershocks), | ||
date_time_issued = strptime( | ||
.data$date_time_issued, format = "%d %B %Y - %I:%M %p", tz = "PST" | ||
) | ||
) |> | ||
dplyr::select( | ||
.data$date_time, .data$bulletin_number, .data$longitude, .data$latitude, | ||
.data$depth, .data$magnitude, .data$reported_intensity, .data$location, | ||
.data$origin, .data$expect_damage, .data$expect_aftershocks, | ||
.data$date_time_issued, .data$prepared_by | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.