-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minimal scraping example combined with automation setup
- Loading branch information
Showing
8 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- --> |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2021-10-06 13:55:19 |