An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

34 řádky
948B

  1. source("../utils/helpers.R")
  2. library(RCurl)
  3. library(stringr)
  4. DOWNLOAD_DIR = "../data/" # warning: this is not created (should maybe)
  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. }