Code
env_file <- path.expand("~/.Renviron")
file.exists(env_file)[1] TRUE
Code
any(grepl("^NYT_API_KEY=", readLines(env_file, warn = FALSE)))[1] TRUE
Code
readRenviron(env_file)
nzchar(Sys.getenv("NYT_API_KEY"))[1] TRUE
env_file <- path.expand("~/.Renviron")
file.exists(env_file)[1] TRUE
any(grepl("^NYT_API_KEY=", readLines(env_file, warn = FALSE)))[1] TRUE
readRenviron(env_file)
nzchar(Sys.getenv("NYT_API_KEY"))[1] TRUE
library(httr2)
library(jsonlite)
library(dplyr)
library(purrr)
library(tibble)
library(ggplot2)
install.packages("httr2")
library(httr2)nyt_api_key <- Sys.getenv("NYT_API_KEY")endpoint <- "https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json"
response <- request(endpoint) |>
req_headers(
Accept = "application/json",
"User-Agent" = "DATA607 NYT API Project"
) |>
req_url_query(`api-key` = nyt_api_key) |>
req_error(is_error = function(resp) FALSE) |>
req_perform()
status <- resp_status(response)# Check the HTTP status and parse JSON only if successful
status <- resp_status(response)
if (status == 200) {
message("200 OK")
nyt_json <- resp_body_json(response, simplifyVector = FALSE)
} else if (status == 401) {
message("401: NYT did not accept the API key.")
} else if (status == 403) {
message("403: Access forbidden. Check that the Most Popular API is enabled for your app.")
} else if (status == 404) {
message("404: Endpoint not found. Check the URL.")
} else {
message("Unexpected HTTP status: ", status)
}
## This code chunk validates the connection and returns the status. A status of "200" indicates sucess."401" means invalid API Key. "403" means access forbidden. "404" means Endpoint not found.resp_content_type(response)[1] "application/json"
nyt_json <- resp_body_json(response, simplifyVector = FALSE)
articles <- purrr::map_dfr(nyt_json$results, function(article) {
tibble::tibble(
title = article$title,
section = article$section,
published_date = article$published_date,
url = article$url
)
})
section_counts <- articles |>
dplyr::count(section, sort = TRUE)
section_counts <- articles |>
dplyr::count(section, name = "article_count", sort = TRUE) |>
dplyr::mutate(percent = round(article_count / sum(article_count) * 100, 2))
section_counts# A tibble: 4 × 3
section article_count percent
<chr> <int> <dbl>
1 U.S. 15 75
2 New York 2 10
3 Opinion 2 10
4 Technology 1 5
ggplot2::ggplot(
section_counts,
ggplot2::aes(x = reorder(section, article_count), y = article_count)
) +
ggplot2::geom_col(fill = "#2878B5") +
ggplot2::coord_flip() +
ggplot2::labs(
title = "Sections Among NYT's Most-Viewed Articles",
x = "Section",
y = "Number of returned articles"
) +
ggplot2::theme_minimal()## 16 of the 20 returned articles (80%) were in the U.S. section