Skip to content

Commit

Permalink
minimal scraping example combined with automation setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbannert committed Oct 6, 2021
1 parent 01be275 commit b240bd8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 1 deletion.
16 changes: 16 additions & 0 deletions demo/README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: "Automation Demo"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo=FALSE}
library(tstools)
w <- window(baro,start=c(2008,1))
tsplot("KOF Economic Barometer" = w)
```

1 change: 1 addition & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![](README_files/figure-gfm/unnamed-chunk-1-1.png)<!-- -->
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
23 changes: 22 additions & 1 deletion solutions/assign_groups.R → demo/assign_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ create_group <- function(x, group_size = 4){
#' @param group_size integer from 1 to 5.
shuffle_cards <- function(x, group_size = 4){
e <- new.env()
e$av <- nms
e$av <- x
no_grps <- ceiling(length(x) / group_size)
groups <- list()
for (i in 1:no_grps){
Expand All @@ -34,3 +34,24 @@ shuffle_cards <- function(x, group_size = 4){
nms <- paste("student", c(LETTERS,letters)[1:33], sep = " ")
shuffle_cards(nms, 5)


students <- c(
"Bérubé Caterina",
"Chandra Adelina",
"Ho Wan Ri",
"Javanmard Hoda",
"Koller Daniela",
"Krizakova Viola",
"Lenzner Andrea Elisabeth",
"Lichtin Florian Maurus",
"Mühlebach Nina",
"Schneider Lena",
"Seiler Pascal Raphael",
"Siegrist Anne Stefanie",
"Toetzke Malte Lorenz",
"Wey Simon",
"Zachmann Lucca",
"David"
)

shuffle_cards(students,4)
File renamed without changes.
58 changes: 58 additions & 0 deletions demo/scrape.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
library(rvest)

url <- "https://kof.ethz.ch/en/news-and-events/media/media-agenda.html"

table_list <- url %>%
read_html() %>%
html_nodes(xpath = '//*[@id="contentMain"]/div[2]/div/div[2]/div/div/div/div/div/div/table') %>%
html_table()

agenda_table <- table_list[[1]]

# extract KOF barometer lines
pub_date <- agenda_table[grep("KOF Economic Barometer",agenda_table$X3),]


# Extract single elements of a date to
# create proper POSIX dates.
d <- gsub("([0-9]{2})(.+)","\\1",pub_date$X1)
m <- gsub("([0-9]{2} )(.+)","\\2",pub_date$X1)
m <- match(m, month.name)

posix_dates <- as.POSIXct(
paste(
paste("2021",m,d,sep="-"),
pub_date$X2
)
)


# automate updating a barometer graph.
library(kofdata)
library(rmarkdown)

# do I need to download the barometer ?

last_update <- as.POSIXct(readLines("status"))

# position of the first publication date that is
# greater than the current last update
dp <- which(cumsum(last_update < posix_dates) == 1)

# run
if(posix_dates[dp] < Sys.time()){
baro <- get_time_series("ch.kof.barometer")[[1]]
render("demo/README.Rmd",
md_document(variant = "gfm"))
writeLines(as.character(Sys.time()),"status")
message("KOF Barometer updated.")
} else {

message("Nil novi sub sole.")

}





1 change: 1 addition & 0 deletions status
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2021-10-06 13:55:19

0 comments on commit b240bd8

Please sign in to comment.