Kaynağa Gözat

add first fetching utilities

package
flavis 4 yıl önce
işleme
65573634b1
3 değiştirilmiş dosya ile 36 ekleme ve 0 silme
  1. +1
    -0
      .gitignore
  2. +33
    -0
      scraping/fetch.R
  3. +2
    -0
      utils/helpers.R

+ 1
- 0
.gitignore Dosyayı Görüntüle

@@ -0,0 +1 @@
*.xml

+ 33
- 0
scraping/fetch.R Dosyayı Görüntüle

@@ -0,0 +1,33 @@
source("../utils/helpers.R")
library(RCurl)
library(stringr)

DOWNLOAD_DIR = "../data/" # warning: this is not created (should maybe)

mk_absolute_url <- function(path) paste0("https://www.bundestag.de", path)

mk_url <- function(offset) {
mk_absolute_url %$% sprintf("/ajax/filterlist/de/services/opendata/543410-543410?offset=%d",
offset)
}

download_protocol <- function(path, name) {
fp <- paste0(DOWNLOAD_DIR, name)
try %$% download.file(mk_absolute_url(path), fp, quiet=T)
}

fetch_batch <- function(offset) {
url <- mk_url(offset)
res <- getURL(url)
paths <- str_match_all(res, "/resource/blob/.*?/([0-9]*-data\\.xml)")[[1]]
mapply(download_protocol, paths[,1], paths[,2])
return(length(paths) > 0)
}

# TODO: error handling
# - what if: page not reachable
# - wrong format, etc.
fetch_all <- function() {
offset <- 0
while(fetch_batch(offset)) offset <- offset + 10
}

+ 2
- 0
utils/helpers.R Dosyayı Görüntüle

@@ -0,0 +1,2 @@
`%$%` <- function(f, x) f(x)
`%.%` <- function(f, g) function(...) f(g(...))

Yükleniyor…
İptal
Kaydet