-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_session_delegations.R
89 lines (74 loc) · 2.46 KB
/
get_session_delegations.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#' Parliamentary delegations in specified session
#'
#' A function for retrieving delegations for a specified parliamentary session.
#'
#' @usage get_session_delegations(sessionid = NA, good_manners = 0)
#'
#' @param sessionid Character string indicating the id of the parliamentary session to retrieve.
#' @param good_manners Integer. Seconds delay between calls when making multiple calls to the same function
#'
#' @return A data.frame with the following variables:
#'
#' | | |
#' |:------------------|:--------------------------|
#' | **response_date** | Date of data retrieval |
#' | **version** | Data version from the API |
#' | **id** | Delegation id |
#' | **name** | Name of delegation |
#' | **session_id** | Session id |
#'
#' @seealso [get_session_committees] [get_all_committees]
#'
#'
#' @examples
#'
#' \dontrun{
#'
#' deleg <- get_session_delegations("2015-2016")
#' deleg
#'
#' }
#'
#' @import rvest httr2
#' @export
#'
get_session_delegations <- function(sessionid = NA, good_manners = 0){
url <- paste0("https://data.stortinget.no/eksport/delegasjoner?sesjonid=", sessionid)
base <- request(url)
resp <- base |>
req_error(is_error = function(resp) FALSE) |>
req_perform()
if(resp$status_code != 200) {
stop(
paste0(
"Response of ",
url,
" is '",
resp |> resp_status_desc(),
"' (",
resp$status_code,
")."
),
call. = FALSE)
}
if(resp_content_type(resp) != "text/xml") {
stop(
paste0(
"Response of ",
url,
" returned as '",
resp_content_type(resp),
"'.",
" Should be 'text/xml'."),
call. = FALSE)
}
tmp <- resp |>
resp_body_html(check_type = FALSE, encoding = "utf-8")
tmp <- data.frame(response_date = tmp |> html_elements("delegasjoner_liste > delegasjon > respons_dato_tid") |> html_text(),
version = tmp |> html_elements("delegasjoner_liste > delegasjon > versjon") |> html_text(),
id = tmp |> html_elements("delegasjoner_liste > delegasjon > id") |> html_text(),
name = tmp |> html_elements("delegasjoner_liste > delegasjon > navn") |> html_text(),
session_id = tmp |> html_elements("sesjon_id") |> html_text())
Sys.sleep(good_manners)
return(tmp)
}