-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrstudio-global-2021-calendar.Rmd
214 lines (160 loc) · 4.78 KB
/
rstudio-global-2021-calendar.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
---
title: "Scrape-Your-Own Calendar"
subtitle: "[rstudio::global(2021) online schedule](https://global.rstudio.com/student/all_events)"
author: Silvia Canelón, Natalia Morandeira & Pao Corrales
output:
html_notebook:
theme: flatly
toc: true
toc_float: true
highlight: pygments
code_download: true
---
# Setup
```{r, warning=FALSE}
# loading libraries --
library(tidyverse)
library(rvest)
library(robotstxt)
library(calendar)
library(lubridate)
library(here)
```
# Checking if we can scrape the page
Check the page's terms of service first, and then verify with `{robotstxt}`
```{r}
get_robotstxt("https://global.rstudio.com/student/all_events")
```
# Getting the html
Good to check if you can do it in one page, and _then_ if it works, scale.
```{r}
html_1 <- read_html("https://global.rstudio.com/student/all_events?page=1")
```
# Extracting information from tags
```{r}
# event title --
title_1 <- html_1 %>%
html_nodes(".session__name") %>%
html_text() # specifies what format we want
# double check if the information you want was saved --
title_1
# dates --
datetime_1 <- html_1 %>%
html_nodes(".session__dates.session__dates--index") %>%
html_text()
datetime_1
```
# Creating scraping functions
```{r}
# scraping event titles --
get_titles <- name <- function(page_number) {
Sys.sleep(2)
link <- paste0("https://global.rstudio.com/student/all_events?page=", page_number)
read_html(link) %>%
html_nodes(".session__name") %>%
html_text()
}
# scraping event dates and times --
get_dates <- name <- function(page_number) {
Sys.sleep(2)
link <- paste0("https://global.rstudio.com/student/all_events?page=", page_number)
read_html(link) %>%
html_nodes(".session__dates.session__dates--index") %>%
html_text()
}
```
# Testing the function
```{r}
get_titles(3)
```
# Iterating with the function
This step will try out the `get_titles()` function on pages 2, 3, 4
```{r}
map(2:4, get_titles)
# then check with the website to see if it matches
```
# Scraping all the pages
```{r}
# titles --
titles_all <- map(1:7, get_titles) %>%
unlist()
# dates --
dates_all <- map(1:7, get_dates) %>%
unlist()
```
```{r}
# creating tibble from scrapes --
schedule <-
tibble(event_name = titles_all,
date_time = dates_all)
schedule
```
# Cleaning the date-time column
```{r}
# defining string patterns --
str_at <- "\\s+at\\s+"
str_to <- "\\s+to\\s+"
str_EST <- " EST"
str_day <- "\\w+\\,\\s+"
# wrangling the date-time strings --
schedule_new_times <-
schedule %>%
mutate(date = str_replace_all(date_time, pattern = str_to, "-"),
date = str_replace_all(date, str_EST, "")) %>%
tidyr::separate(date, sep = str_at, c("day_date", "time")) %>%
mutate(date = str_replace(day_date, pattern = str_day, ""),
date = lubridate::mdy(date, tz = "US/Eastern")) %>%
mutate(date_time_new = str_c(date, time, sep = " ")) %>%
tidyr::separate(time, sep = "-", c("start_time", "end_time"))
```
# Parsing the date-time
```{r}
# wrangling the dates and times --
schedule_new <-
schedule_new_times %>%
mutate(start_datetime = str_c(date, start_time, sep = " "),
end_datetime = str_c(date, end_time, sep = " ")) %>%
mutate(across(c(start_datetime, end_datetime),
~lubridate::ymd_hm(.x, tz = "US/Eastern"))) %>%
select(-c(date_time, day_date, date_time_new))
# writing to CSV and RDS files --
write_csv(schedule_new,
here("schedule-files", "schedule_new_EST.csv"))
saveRDS(schedule_new,
here("schedule-files", "schedule_new_EST.Rds"))
```
# Select your timezone and convert to local time
```{r}
timezone <- Sys.timezone()
schedule_new_timezone <- schedule_new %>%
mutate(start_datetime = lubridate::with_tz(start_datetime, tzone = timezone),
end_datetime = lubridate::with_tz(end_datetime, tzone = timezone))
# writing to CSV and RDS files --
write_csv(schedule_new_timezone,
here("schedule-files", "schedule_new_localtz.csv"))
saveRDS(schedule_new_timezone,
here("schedule-files", "schedule_new_localtz.Rds"))
```
# Creating an .ics object
```{r}
# creating a function --
make_calendar <- function(event) {
event_subset <- schedule_new_timezone[event, ]
calendar_event <-
calendar::ic_event(start_time = event_subset$start_datetime,
end_time = event_subset$end_datetime,
summary = event_subset$event_name)
return(calendar_event)
}
number_events <- length(schedule_new_timezone$event_name)
# creating ics objects for all events --
events_all <- map(1:number_events, make_calendar) %>%
bind_rows()
# writing to .ics file --
calendar::ic_write(events_all,
here("calendar-files-ics", "all_events_localtime.ics"))
```
# Saving as an R script
```{r, warning=FALSE}
knitr::purl("rstudio-global-2021-calendar.Rmd")
```