-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_Request_API_.R
127 lines (91 loc) · 4.16 KB
/
1_Request_API_.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
library(httr)
library(tidyverse)
library(tidyjson)
## Set working directory ####
setwd("WORKINGDIRECTORY")
getwd()
## Alle Geschäfte der Legislaturperiode 49/50/51 ####
get_votes <- function(legislative_period, welche){
url_vote <- str_c("https://ws.parlament.ch/odata.svc/Vote?$top=10000&$filter=Language%20eq%20'DE'%20and%20IdLegislativePeriod%20eq%20", legislative_period, "&$format=json")
r <- GET(url_vote)
r$status_code
temp_a <- jsonlite::fromJSON(content(r, "text"))
df_Ids <- as_tibble(temp_a$d) %>%
select(c(ID, RegistrationNumber, BusinessNumber,
BusinessShortNumber, BusinessTitle, BusinessAuthor,
BillNumber, BillTitle,
IdLegislativePeriod, IdSession, SessionName,
Subject))
## Nur Schlussabstimmungen oder alle?
print(welche)
if (welche == "gesamt"){
df_Ids <- df_Ids %>% filter(grepl("gesamtabstimmung|Vote sur l'ensemble|Votazione sul complesso|schlussabstimmung|Vote final|Votazione finale", Subject, ignore.case = TRUE))
}
## get all votes
df_votes <- tibble()
for (i in seq_along(row.names(df_Ids))){
IdVote <- df_Ids$ID[i]
print(i)
print(IdVote)
url <- str_c("https://ws.parlament.ch/odata.svc/Voting?$top=10000&$filter=Language%20eq%20'DE'%20and%20IdVote%20eq%20", IdVote, "&$format=json")
r <- GET(url)
r$status_code
df_temp <- jsonlite::fromJSON(content(r, "text"))$d %>% tibble() %>%
select(c("IdVote", "ID", "RegistrationNumber",
"PersonNumber", "FirstName", "LastName", "ParlGroupName",
"DecisionText", "BusinessTitle",
"IdLegislativePeriod", "IdSession", "Subject"))
df_votes <- rbind(df_votes, df_temp)
}
return(list(df_Ids, df_votes))
}
## Alle Schluss- und Gesamtabstimmungen holen
obj_49 <- get_votes(49, "gesamt")
obj_50 <- get_votes(50, "gesamt")
obj_51 <- get_votes(51, "gesamt")
df_Ids_49 <- obj_49[[1]]
df_votes_49 <- obj_49[[2]]
df_Ids_50 <- obj_50[[1]]
df_votes_50 <- obj_50[[2]]
df_Ids_51 <- obj_51[[1]]
df_votes_51 <- obj_51[[2]]
## ALLE Abstimmungen holen (langsam!)
obj_49_alle <- get_votes(49, "alle")
obj_50_alle <- get_votes(50, "alle")
obj_51_alle <- get_votes(51, "alle")
df_Ids_49_alle <- obj_49_alle[[1]]
df_votes_49_alle <- obj_49_alle[[2]]
df_Ids_50_alle <- obj_50_alle[[1]]
df_votes_50_alle <- obj_50_alle[[2]]
df_Ids_51_alle <- obj_51_alle[[1]]
df_votes_51_alle <- obj_51_alle[[2]]
## Daten speichern
write_csv(df_Ids_49, "Data_output/Ids_49.csv")
write_csv(df_Ids_50, "Data_output/Ids_50.csv")
write_csv(df_Ids_51, "Data_output/Ids_51.csv")
write_csv(df_votes_49, "Data_output/Votes_49.csv")
write_csv(df_votes_50, "Data_output/Votes_50.csv")
write_csv(df_votes_51, "Data_output/Votes_51.csv")
write_csv(df_Ids_49_alle, "Data_output/Ids_49_alle.csv")
write_csv(df_Ids_50_alle, "Data_output/Ids_50_alle.csv")
write_csv(df_Ids_51_alle, "Data_output/Ids_51_alle.csv")
write_csv(df_votes_49_alle, "Data_output/Votes_49_alle.csv")
write_csv(df_votes_50_alle, "Data_output/Votes_50_alle.csv")
write_csv(df_votes_51_alle, "Data_output/Votes_51_alle.csv")
## Infos zu allen Parlamentariern in diesen Legislaturperioden holen ####
PersonNumbers <- unique(rbind(df_votes_49, df_votes_50, df_votes_51)[, "PersonNumber"])$PersonNumber
df_parls <- tibble()
for (PersonNumber in PersonNumbers){
print(PersonNumber)
url_parl <- str_c("https://ws.parlament.ch/odata.svc/Person?$top=20&$filter=Language%20eq%20'DE'%20and%20PersonNumber%20eq%20", PersonNumber, "&$format=json")
r <- GET(url_parl)
r$status_code
temp_a <- jsonlite::fromJSON(content(r, "text"))
df_parls_temp <- as_tibble(temp_a$d) %>% select(c(ID, Language,
PersonNumber, PersonIdCode, LastName,
FirstName, GenderAsString, DateOfBirth, MaritalStatusText,
NumberOfChildren))
df_parls <- rbind(df_parls, df_parls_temp)
}
## Parlamentarier Daten speichern
df_parls %>% write_csv("Data_output/alle_parlamentarier.csv")