Assignment 5

Author

Brennan Stout

Published

October 13, 2026

library(httr2)
Warning: package 'httr2' was built under R version 4.5.3
api_key <- Sys.getenv("GOVINFO_API_KEY")

body <- list(
  query = 'collection:(CHRG) AND "Committee on Foreign Relations"',
  pageSize = 10,
  offsetMark = "*",
  sorts = list(list(field = "publishdate", sortOrder = "DESC"))
)

found <- request("https://api.govinfo.gov/search") |>
  req_url_query(api_key = api_key) |>
  req_body_json(body) |>
  req_perform() |>
  resp_body_json()

save_dir <- "C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo"

records <- head(found$results, 10)

download_log <- lapply(records, function(item) {
  pkg_id <- item$packageId
  pdf_url <- if (!is.null(item$download$pdfLink)) {
    item$download$pdfLink
  } else {
    paste0("https://api.govinfo.gov/packages/", pkg_id, "/pdf")
  }
  
  file_name <- paste0(pkg_id, ".pdf")
  dest_path <- file.path(save_dir, file_name)
  download_status <- tryCatch({
    request(pdf_url) |>
      req_url_query(api_key = api_key) |>
      req_perform(path = dest_path)
    "Success"
  }, error = function(e) {
    conditionMessage(e)
  })
  
  data.frame(
    title        = if (!is.null(item$title)) item$title else NA_character_,
    package_id   = pkg_id,
    date_issued  = if (!is.null(item$dateIssued)) item$dateIssued else NA_character_,
    file_name    = file_name,
    file_path    = dest_path,
    size_mb      = if (file.exists(dest_path)) round(file.size(dest_path) / (1024^2), 2) else NA_real_,
    status       = download_status,
    stringsAsFactors = FALSE
  )
})

download_table <- do.call(rbind, download_log)
print(download_table)
                                                                                           title
1         Arms Control and Transforming International Security Functions at the State Department
2                                                          A Pathway to European Energy Security
3                             Countering Terrorism in North Africa: Opportunities and Challenges
4                          NATO Summit 2025: An Assessment of Transatlantic Security Cooperation
5                                  East Africa & the Horn: At a Turning Point or Breaking Point?
6  Shared Threats: Indo Pacific Alliances and Burden Sharing in Today's Geopolitical Environment
7           PRC Influence and the Status of Taiwan's Diplomatic Allies in the Western Hemisphere
8                                         Advancing American Interests in the Western Hemisphere
9                             Interdicting Illicit Drug Trafficking: A View from the Front Lines
10                                                          After Assad: Navigating Syria Policy
          package_id date_issued             file_name
1  CHRG-119shrg63909  2026-03-24 CHRG-119shrg63909.pdf
2  CHRG-119shrg63707  2026-02-04 CHRG-119shrg63707.pdf
3  CHRG-119shrg64059  2026-02-03 CHRG-119shrg64059.pdf
4  CHRG-119shrg61565  2025-06-18 CHRG-119shrg61565.pdf
5  CHRG-119shrg60820  2025-05-13 CHRG-119shrg60820.pdf
6  CHRG-119shrg60819  2025-03-26 CHRG-119shrg60819.pdf
7  CHRG-119shrg60060  2025-03-26 CHRG-119shrg60060.pdf
8  CHRG-119shrg60032  2025-03-05 CHRG-119shrg60032.pdf
9  CHRG-119shrg61202  2025-02-26 CHRG-119shrg61202.pdf
10 CHRG-119shrg59893  2025-02-13 CHRG-119shrg59893.pdf
                                                                  file_path
1  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg63909.pdf
2  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg63707.pdf
3  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg64059.pdf
4  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg61565.pdf
5  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg60820.pdf
6  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg60819.pdf
7  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg60060.pdf
8  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg60032.pdf
9  C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg61202.pdf
10 C:\\Users\\brist\\Documents\\WebOut_Stout\\govInfo/CHRG-119shrg59893.pdf
   size_mb  status
1     0.33 Success
2     8.85 Success
3     0.30 Success
4     0.42 Success
5     3.77 Success
6     6.25 Success
7     0.37 Success
8     0.38 Success
9    10.07 Success
10    5.34 Success
file_path <- "govinfo-search-results-2026-10-12T21_12_40.csv"

raw_data <- read.csv(file_path, stringsAsFactors = FALSE, fill = TRUE)
Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
: embedded nul(s) found in input
clean_data <- raw_data[!is.na(raw_data$publishdate) & raw_data$publishdate != "", ]

clean_data$publishdate <- as.Date(clean_data$publishdate)
sorted_data <- clean_data[order(clean_data$publishdate, decreasing = TRUE), ]

top10 <- head(sorted_data[, c("index", "publishdate", "collection", "packageId", "title")], 10)

print(top10, row.names = FALSE)
 index publishdate              collection                packageId
     1  2027-01-01   Privacy Act Issuances    PAI-2027-NASA-interim
     2  2027-01-01   Privacy Act Issuances   PAI-2027-STATE-interim
     3  2027-01-01   Privacy Act Issuances PAI-2027-JUSTICE-interim
     4  2026-10-09 Congressional Calendars  CCAL-119hcal-2026-10-09
     5  2026-10-09 Congressional Calendars  CCAL-119hcal-2026-10-09
     6  2026-10-09 Congressional Calendars  CCAL-119hcal-2026-10-09
     7  2026-10-09 Congressional Calendars  CCAL-119hcal-2026-10-09
     8  2026-10-09        History of Bills                 HOB-2026
     9  2026-10-09        History of Bills                 HOB-2026
    10  2026-10-09        History of Bills                 HOB-2026
                                                                                                                                                                                                                                                                              title
                                                                                                                                                                                                   Privacy Act Issuance for the National Aeronautics and Space Administration, 2027
                                                                                                                                                                                                                             Privacy Act Issuance for the Department of State, 2027
                                                                                                                                                                                                                           Privacy Act Issuance for the Department of Justice, 2027
                                                                                                                                                                                                                                     History of Bills and Resolutions - House Bills
                                                                                                                                                                                                                    History of Bills and Resolutions - House Concurrent Resolutions
                                                                                                                                                                                                                                    History of Bills and Resolutions - Senate Bills
                                                                                                                                                                                                                        History of Bills and Resolutions - Senate Joint Resolutions
 A resolution expressing concern about the increasing influence of the People’s Republic of China in Latin America and the Caribbean and calling for strengthened United States economic, security, and diplomatic engagement in the region; to the Committee on Foreign Relations.
                                                                                                                                               A resolution expressing the sense of the Senate in support of general elections in Venezuela; to the Committee on Foreign Relations.
                                                                               A resolution supporting the United States dollar as the reserve currency of the world and combating the economic influence of the People’s Republic of China; to the Committee on Foreign Relations.

The manual download and the API query get different results, likely due to different keywords being entered in the website.

Report

Gov Info provide very good information, being that they are digital documents and not photo scans or otherwise physical to digital documents. It would take a little work through quanteda or some other semantic tool to process all the documents. A good way to improve the process in question could be in processing a combination of the title and governmentAuthor to better specify what documents are being searched for. This could also be done with the small description included with the document that as well, but would need more implementation of a semantic search tool.

The advantage of an API over a search page or scraping is the level of control if offers. The data searched by the API will be structured and sorted intentionally for quick, accurate, and repeatable access.