Skip to content

Commit 8655078

Browse files
committed
add missing functions
1 parent c9f98ef commit 8655078

5 files changed

Lines changed: 236 additions & 0 deletions

File tree

R/getDatasetRaw.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#' Download dataset archive as a ZIP file
2+
#'
3+
#' @description
4+
#' Download a specific dataset as a binary ZIP archive written directly to disk,
5+
#' containing all bills, votes, and people data for the specified session
6+
#'
7+
#' @param sessionID session_id integer value (use session_id from getDatasetList)
8+
#'
9+
#' @param accessKey access_key string value (use access_key from getDatasetList)
10+
#'
11+
#' @param format File format of the ZIP contents, either "json" or "csv"
12+
#'
13+
#' @param file File path to write the ZIP archive to. Defaults to legiscan_dataset_<sessionID>.zip in the working directory
14+
#'
15+
#' @param legiKey 32 character string provided by legiscan
16+
#'
17+
#' @returns File path of the downloaded ZIP archive, invisibly
18+
#'
19+
#' @examples
20+
#' \dontrun{
21+
#' getDatasetRaw(sessionID = 1234, accessKey = "abc123def456")
22+
#' getDatasetRaw(sessionID = 1234, accessKey = "abc123def456", format = "csv")
23+
#' }
24+
#'
25+
#' @export
26+
getDatasetRaw <- function(sessionID = NULL, accessKey = NULL, format = "json", file = NULL, legiKey = NULL){
27+
28+
if (is.null(sessionID) || is.null(accessKey)){
29+
stop("Specify both a sessionID and accessKey from `getDatasetList` to download a dataset")
30+
}
31+
32+
response <- legiRequest(
33+
op = "getDatasetRaw",
34+
id = sessionID,
35+
access_key = accessKey,
36+
format = format,
37+
legiKey = legiKey,
38+
raw = TRUE
39+
)
40+
41+
if (is.null(file)){
42+
file <- paste0("legiscan_dataset_", sessionID, ".zip")
43+
}
44+
writeBin(response, file)
45+
print(paste0("Dataset saved to ", file))
46+
return(invisible(file))
47+
}

R/getMasterListRaw.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' Return legiscan Master List for change detection
2+
#'
3+
#' @description
4+
#' Returns a Master List of bill_id and change_hash values for a specified session
5+
#' or the most recent regular session if only state is provided. Optimized for
6+
#' detecting which bills have changed and need updating via `getBill`.
7+
#'
8+
#' @param session Session id integer value. Can be found with `getSessions`
9+
#'
10+
#' @param state US state abbreviation
11+
#'
12+
#' @param legiKey 32 character string provided by legiscan
13+
#'
14+
#' @returns Master List of bill_id and change_hash in dataframe format
15+
#'
16+
#' @examples
17+
#' \dontrun{
18+
#' getMasterListRaw(session = 2108)
19+
#' getMasterListRaw(state = "TX")
20+
#' }
21+
#'
22+
#' @export
23+
getMasterListRaw <- function(session = NULL, state = NULL, legiKey = NULL){
24+
25+
if (is.null(session) && is.null(state)){
26+
stop("Specify a Session or a State to return a Master List")
27+
}
28+
29+
response <- legiRequest(
30+
op = "getMasterListRaw",
31+
state = state,
32+
id = session,
33+
legiKey = legiKey
34+
)
35+
36+
masterlist <- response$masterlist
37+
if (!is.null(masterlist$session)){
38+
print(masterlist$session$session_name)
39+
masterlist$session <- NULL
40+
}
41+
return(dplyr::bind_rows(masterlist))
42+
}

R/getMonitorListRaw.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' Return associated monitor list for change detection
2+
#'
3+
#' @description
4+
#' Return bill_id and change_hash values from the monitor list of the account
5+
#' associated with the legiKey provided. Optimized for detecting which bills
6+
#' have changed and need updating via `getBill`.
7+
#'
8+
#' @param record Record filter: "current" or "archived", or an exact year >= 2010
9+
#'
10+
#' @param legiKey 32 character API key from legiscan
11+
#'
12+
#' @returns Monitored bills with bill_id and change_hash in dataframe format
13+
#'
14+
#' @examples
15+
#' \dontrun{
16+
#' getMonitorListRaw()
17+
#' getMonitorListRaw(record = "archived")
18+
#' }
19+
#'
20+
#' @export
21+
getMonitorListRaw <- function(record = "current", legiKey = NULL){
22+
23+
response <- legiRequest(
24+
op = "getMonitorListRaw",
25+
record = record,
26+
legiKey = legiKey
27+
)
28+
29+
return(dplyr::bind_rows(response$monitorlist))
30+
}

R/legiSearchRaw.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#' Return raw search results from legiscan's database
2+
#'
3+
#' @description
4+
#' Return search results from legiscan's full text engine with simplified details,
5+
#' 2000 results at a time. Returns relevance, bill_id, and change_hash only,
6+
#' appropriate for automated keyword monitoring. Check legiscan.com for specific search syntax for more details.
7+
#'
8+
#' @param query Enter your search text. Your query may use grammatically correct spacing. Use legiscan's search syntax for more powerful results.
9+
#'
10+
#' @param state Search the entire nation by default with 'ALL' or specify state using letter abbreviations
11+
#'
12+
#' @param year Should be an integer. 1=All, 2=Current, 3=Recent, 4=Prior, >1900=Exact Year
13+
#'
14+
#' @param session Limit search to specific session with a session_id
15+
#'
16+
#' @param page Default is set to return Page 1. `legiSearchRaw` will paginate and include results.
17+
#'
18+
#' @param legiKey 32 character string provided by legiscan
19+
#'
20+
#' @returns Search results with relevance, bill_id, and change_hash in dataframe format
21+
#'
22+
#' @examples
23+
#' \dontrun{
24+
#' legiSearchRaw(
25+
#' query = "intro:month AND wage theft",
26+
#' state = "TX"
27+
#' )
28+
#' }
29+
#'
30+
#' @export
31+
legiSearchRaw <- function(query = NULL, state = "ALL", year = 2, session = NULL, page = 1, legiKey = NULL){
32+
33+
all_data <- list()
34+
35+
while (TRUE) {
36+
response <- legiRequest(
37+
op = "getSearchRaw",
38+
state = state,
39+
query = query,
40+
year = year,
41+
id = session,
42+
page = page,
43+
legiKey = legiKey
44+
)
45+
46+
results <- response$searchresult$results
47+
if (is.null(results) || length(results) == 0) {
48+
break
49+
}
50+
51+
all_data <- dplyr::bind_rows(all_data, dplyr::bind_rows(results))
52+
53+
if (page >= response$searchresult$summary$page_total) {
54+
break
55+
}
56+
57+
page <- page + 1
58+
}
59+
if (length(all_data) == 0) {
60+
warning("No results found. Reference <https://legiscan.com/fulltext-search> for help with search syntax.")
61+
return(NULL)
62+
} else {
63+
message(paste0(nrow(all_data), " Results Found"))
64+
return(all_data)
65+
}
66+
}

R/setMonitor.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#' Add or remove bills from the monitor list
2+
#'
3+
#' @description
4+
#' Interact with GAITS to add or remove bills from the monitor list of the
5+
#' account associated with the legiKey provided, or set a stance on monitored bills
6+
#'
7+
#' @param billIDs One or more bill_id integer values to operate on
8+
#'
9+
#' @param action Action to take on the bill list: "monitor", "remove", or "set"
10+
#'
11+
#' @param stance Position on the bill: "watch", "support", or "oppose"
12+
#'
13+
#' @param legiKey 32 character API key from legiscan
14+
#'
15+
#' @returns Status of each bill_id and the action taken in dataframe format
16+
#'
17+
#' @examples
18+
#' \dontrun{
19+
#' setMonitor(billIDs = c(150334, 141690), action = "monitor")
20+
#' setMonitor(billIDs = 1533377, action = "set", stance = "support")
21+
#' }
22+
#'
23+
#' @export
24+
setMonitor <- function(billIDs = NULL, action = NULL, stance = "watch", legiKey = NULL){
25+
26+
if (is.null(billIDs)){
27+
stop("Specify one or more billIDs to operate on")
28+
}
29+
if (is.null(action) || !action %in% c("monitor", "remove", "set")){
30+
stop("action must be one of 'monitor', 'remove', or 'set'")
31+
}
32+
if (!stance %in% c("watch", "support", "oppose")){
33+
stop("stance must be one of 'watch', 'support', or 'oppose'")
34+
}
35+
36+
response <- legiRequest(
37+
op = "setMonitor",
38+
list = paste(billIDs, collapse = ","),
39+
action = action,
40+
stance = stance,
41+
legiKey = legiKey
42+
)
43+
44+
return(
45+
data.frame(
46+
bill_id = names(response$`return`),
47+
result = unlist(response$`return`),
48+
row.names = NULL
49+
)
50+
)
51+
}

0 commit comments

Comments
 (0)