An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 line
2.6KB

  1. source("../utils/helpers.R")
  2. source("config.R")
  3. library(RCurl)
  4. library(stringr)
  5. mk_absolute_url <- function(path) paste0("https://www.bundestag.de", path)
  6. mk_url <- function(offset) {
  7. mk_absolute_url %$% sprintf("/ajax/filterlist/de/services/opendata/543410-543410?offset=%d",
  8. offset)
  9. }
  10. download_protocol <- function(path, name) {
  11. fp <- paste0(DOWNLOAD_DIR, name)
  12. try %$% download.file(mk_absolute_url(path), fp, quiet=T)
  13. }
  14. fetch_batch <- function(offset) {
  15. url <- mk_url(offset)
  16. res <- getURL(url)
  17. paths <- str_match_all(res, "/resource/blob/.*?/([0-9]*-data\\.xml)")[[1]]
  18. mapply(download_protocol, paths[,1], paths[,2])
  19. return(length(paths) > 0)
  20. }
  21. # TODO: error handling
  22. # - what if: page not reachable
  23. # - wrong format, etc.
  24. fetch_all <- function() {
  25. offset <- 0
  26. while(fetch_batch(offset)) offset <- offset + 10
  27. }
  28. #______________________________________________________________________________
  29. #Auf diese Weise funktioniert das Programm bei mir, da kein RCurl mehr
  30. #verwendet wird. Es ist allerdings nicht mehr so schön und die Idee dazu hat
  31. #mir Daniela gegeben. Im Grunde ist es sehr ähnlich zu dem Live-Blatt L04.
  32. #Das Programm gibt mir eine Liste von allen Protokollen der 19. Wahlperiode
  33. #zurück (falls man return(all_protocols) entkommentiert).
  34. #Ich verstehe noch nicht so richtig wie man die Protokolle dann abspeichert
  35. #(die for-Schleife zum Download läuft deswegen noch nicht ganz, sollte aber
  36. #relativ leicht zum laufen gebracht werden, wenn man weiß, wie die Dateien
  37. #abzuspeichern sind).
  38. source("../utils/helpers.R")
  39. source("config.R")
  40. library(stringr)
  41. library("xml2")
  42. library(rvest)
  43. mk_absolute_url <- function(path) paste0("https://www.bundestag.de", path)
  44. mk_url <- function(offset) {
  45. mk_absolute_url %$% sprintf("/ajax/filterlist/de/services/opendata/543410-543410?offset=%d",
  46. offset)
  47. }
  48. read <- function(offset){
  49. read_html(mk_url(offset))
  50. }
  51. fetch_batch <- function(offset){
  52. read(offset) %>%
  53. html_elements("a") %>%
  54. html_attr("href") ->
  55. paths
  56. protocols <- mk_absolute_url(paths)
  57. all_protocols <<- c(all_protocols, protocols)
  58. return(length(paths) > 0)
  59. }
  60. fetch_all <- function(){
  61. all_protocols <<- c()
  62. offset <- 0
  63. while(fetch_batch(offset)) offset <- offset + 10
  64. for(i in 1:length(all_protocols)){
  65. download.file(all_protocols[i], paste0(DOWNLOAD_DIR, i)) #cannot open destfile '../data/1', reason 'No such file or directory'
  66. #verstehe noch nicht so richtig unter welchem Pfad ich das abspeichern muss
  67. }
  68. # return(all_protocols)
  69. }