PER MØLDRUP-DALUM

library(dplyr)
library(lubridate)
library(stringr)
library(readr)
library(rvest)
library(uuid)

create_ical_event_df <- function(title, day, uuid) {
  sprintf(
    "BEGIN:VEVENT
UID:%s@%s
DTSTAMP:%s
DTSTART:%s
DTEND:%s
SUMMARY:%s
URL:%s
END:VEVENT
", uuid,
    "17053.dk", 
    format(Sys.time(), "%Y%m%dT%H%M%SZ", tz="GMT"), 
    day, 
    day, 
    title,
    "http://fafid.dk/movies.php"
  )
}

prologue <- "BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//rstats//NONSGML v1.0//EN
"

epilouge <- "
END:VCALENDAR"

"http://fafid.dk/movies.php" %>% 
  read_html() %>% 
  html_node("table") %>% 
  html_table() %>% 
  # next line is to remedy that every second line is metadata that we don't need
  filter(row_number() %% 2 == 1) %>% 
  # remove the movie company from the title
  mutate(Titel = str_remove(Titel, "-.+$")) %>% 
  rowwise() %>% 
  mutate(uuid = uuid::UUIDgenerate()) %>% 
  select(Titel, Premiere, uuid) %>% 
  mutate(Premiere = dmy(Premiere)) %>% 
  filter(Premiere < "2023-01-01") %>% 
  mutate(Premiere = format(Premiere, "%Y%m%d")) -> premierer

write_lines(
  c(
    prologue,
    premierer %>% 
      mutate(event = create_ical_event_df(Titel, Premiere, uuid)) %>% 
      pull(event),
    epilouge) %>% str_c(collapse = "\n"
    ),
  path = "danske-biograf-premierer.ics"
)