-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_case.R
206 lines (192 loc) · 10 KB
/
get_case.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#' Retreive a parliamentary case
#'
#' @description A function for retrieving single parliamentary case by id.
#'
#' @usage get_case(caseid = NA, good_manners = 0)
#'
#' @param caseid Character string indicating the id of the case to request
#' @param good_manners Integer. Seconds delay between calls when making multiple calls to the same function
#'
#' @return A list with seven data frame elements:
#'
#' 1. **$root** (main data on the case)
#'
#' | | |
#' |:---------------------|:----------------------------------|
#' | **response_date** | Date of data retrieval |
#' | **version** | Data version from the API |
#' | **document_group** | Case document group type |
#' | **finalized** | Whether the case finalized |
#' | **reference** | Relevant publication references |
#' | **id** | Case id |
#' | **req_text** | Recommendation (proposal) text |
#' | **committee_id** | Id of committee handling the case |
#' | **title_short** | Case short title |
#' | **decision_short** | Case decision_short |
#' | **parenthesis_text** | Case parenthesis text |
#' | **case_number** | Case number |
#' | **session_id** | Session id |
#' | **proceedings_id** | Type of proceeding id |
#' | **proceedings_name** | Type of proceeding name |
#' | **status** | Status for case |
#' | **title** | Case title (long) |
#' | **type** | Case type |
#' | **decision_text** | Decision text |
#'
#' 2. **$topic** (the topics related to the case)
#'
#' | | |
#' |:------------------|:------------------------------|
#' | **is_main_topic** | Is this (row) the main topic? |
#' | **main_topic_id** | Id for main topic |
#' | **id** | Topic id |
#' | **navn** | Topic name |
#'
#' 3. **$publication_references** (references for publications on the case)
#'
#' | | |
#' |:------------------|:----------------------------------------------------|
#' | **export_id** | Id for export of publication (used in ?get_publication) |
#' | **link_text** | Publication title |
#' | **link_url** | URL to publication |
#' | **type** | Publication type |
#' | **subtype** | Publication subtype (chamber) |
#'
#' 4. **$proposers** (MPs behind case proposal, when relevant)
#'
#' | | |
#' |:-------------|:----------------------------|
#' | **mp_id** | MPs id |
#' | **party_id** | Party id of MPs |
#' | **sub_mp** | Whether MPs are substitutes |
#'
#' 5. **$proceeding_steps** (case proceeding steps)
#'
#' | | |
#' |:----------------|:----------------------------------|
#' | **step_name** | Name of steps |
#' | **step_number** | Step order for case |
#' | **outdated** | Whether the step type is outdated |
#'
#' 6. **$spokespersons** (all MPs that are spokespersons for the case)
#'
#' | | |
#' |:-------------|:----------------------------|
#' | **mp_id** | MPs id |
#' | **party_id** | Party id of MPs |
#' | **sub_mp** | Whether MPs are substitutes |
#'
#' 7. **$keywords** (all keywords associated with the case)
#'
#' | | |
#' |:-----------|:----------------------|
#' | **keyword**| Keywords for the case |
#'
#' @md
#'
#' @seealso [get_session_cases]
#'
#' @examples
#'
#' \dontrun{
#'
#' # Get one case
#' case <- get_case("30233")
#' case
#'
#' # Get multiple cases
#' cases <- lapply(c("30233", "30362", "30234", "30236"), get_case, good_manners = 2)
#' cases_root <- lapply(cases, function(x) x$root)
#' cases_root <- do.call(rbind, cases_root)
#' cases_root
#'
#' cases_keywords <- lapply(1:nrow(cases_root), function(x){
#' tmp <- cases[[x]]$keywords
#' tmp$case_id <- cases_root$id[x]
#' return(tmp)
#' })
#' cases_keywords <- do.call(rbind, cases_keywords)
#' cases_keywords
#'
#' }
#' @import httr2 rvest
#'
#' @export
get_case <- function(caseid = NA, good_manners = 0){
url <- paste0("https://data.stortinget.no/eksport/sak?sakid=", caseid)
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")
if(identical(tmp |> html_elements("komite > id") |> html_text(), character())) {
committee_id <- NA
} else {
committee_id <- tmp |> html_elements("komite > id") |> html_text()
}
tmp2 <- list(root = data.frame(response_date = tmp |> html_elements("detaljert_sak > respons_dato_tid") |> html_text(),
version = tmp |> html_elements("detaljert_sak > versjon") |> html_text(),
document_group = tmp |> html_elements("detaljert_sak > dokumentgruppe") |> html_text(),
finalized = tmp |> html_elements("ferdigbehandlet") |> html_text(),
reference = tmp |> html_elements("henvisning") |> html_text(),
id = tmp |> html_elements("detaljert_sak > id") |> html_text(),
req_text = tmp |> html_elements("innstillingstekst") |> html_text(),
committee_id = committee_id,
title_short = tmp |> html_elements("korttittel") |> html_text(),
decision_short = tmp |> html_elements("kortvedtak") |> html_text(),
parenthesis_text = tmp |> html_elements("parentestekst") |> html_text(),
case_number = tmp |> html_elements("sak_nummer") |> html_text(),
session_id = tmp |> html_elements("sak_sesjon") |> html_text(),
proceedings_id = tmp |> html_elements("saksgang > id") |> html_text(),
proceedings_name = tmp |> html_elements("saksgang > navn") |> html_text(),
status = tmp |> html_elements("detaljert_sak > status") |> html_text(),
title = tmp |> html_elements("detaljert_sak > tittel") |> html_text(),
type = tmp |> html_elements("detaljert_sak > type") |> html_text(),
decision_text = tmp |> html_elements("detaljert_sak > vedtakstekst") |> html_text()),
topic = data.frame(is_main_topic = tmp |> html_elements("emne > er_hovedemne") |> html_text(),
main_topic_id = tmp |> html_elements("emne > hovedemne_id") |> html_text(),
id = tmp |> html_elements("emne > id") |> html_text(),
navn = tmp |> html_elements("emne > navn") |> html_text()),
proposers = data.frame(mp_id = tmp |> html_elements("forslagstiller_liste > representant > id") |> html_text(),
party_id = tmp |> html_elements("forslagstiller_liste > representant > parti > id") |> html_text(),
sub_mp = tmp |> html_elements("forslagstiller_liste > representant > vara_representant") |> html_text()),
publication_references = data.frame(export_id = tmp |> html_elements("publikasjon_referanse > eksport_id") |> html_text(),
link_text = tmp |> html_elements("publikasjon_referanse > lenke_tekst") |> html_text(),
link_url = tmp |> html_elements("publikasjon_referanse > lenke_url") |> html_text(),
type = tmp |> html_elements("publikasjon_referanse > type") |> html_text(),
subtype = tmp |> html_elements("publikasjon_referanse > undertype") |> html_text()),
proceeding_steps = data.frame(step_name = tmp |> html_elements("saksgang_steg > navn") |> html_text(),
step_number = tmp |> html_elements("saksgang_steg > steg_nummer") |> html_text(),
outdated = tmp |> html_elements("saksgang_steg > uaktuell") |> html_text()),
spokespersons = data.frame(mp_id = tmp |> html_elements("saksordfoerer_liste > representant > id") |> html_text(),
party_id = tmp |> html_elements("saksordfoerer_liste > representant > parti > id") |> html_text(),
sub_mp = tmp |> html_elements("saksordfoerer_liste > representant > vara_representant") |> html_text()),
keywords = data.frame(keyword = tmp |> html_elements("stikkord_liste > string") |> html_text()))
Sys.sleep(good_manners)
return(tmp2)
}