From 3e5bf5797652aa244a224f840f6850297343d137 Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 15 Jul 2022 01:37:10 +0200 Subject: [PATCH] staatsoperette: init --- default.nix | 1 + staatsoperette/scrape.rb | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 staatsoperette/scrape.rb diff --git a/default.nix b/default.nix index 9480a6d..92d6d45 100644 --- a/default.nix +++ b/default.nix @@ -41,4 +41,5 @@ in { kunsthaus = wrapScript "kunsthaus" ./kunsthaus/scrape.rb; hfmdd = wrapScript "hfmdd" ./hfmdd/scrape.rb; hfbk-dresden = wrapScript "hfbk-dresden" ./hfbk-dresden/scrape.rb; + staatsoperette = wrapScript "staatsoperette" ./staatsoperette/scrape.rb; } diff --git a/staatsoperette/scrape.rb b/staatsoperette/scrape.rb new file mode 100644 index 0000000..eb3b11f --- /dev/null +++ b/staatsoperette/scrape.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# coding: utf-8 + +require 'open-uri' +require 'json' +require 'nokogiri' +require 'erb' + +events = [] + +url = "https://www.staatsoperette.de/spielplan/" +STDERR.puts "GET #{url}" +doc = Nokogiri::HTML URI.open(url) + +doc.css(".performance").each do |content| + event_link = URI::join url, content.css("h2 a").attr("href") + ical_link = URI.join url, content.css("a[href$='.ics']").attr('href') + + event_line = false + has_url = false + STDERR.puts "GET #{ical_link}" + ical = URI.open(ical_link).read + events.push ical.lines.collect { |line| + case line.chomp + when "BEGIN:VEVENT" + event_line = true + line + when "END:VEVENT" + event_line = false + if has_url + line + else + ["URL:#{event_link}\n", line] + end + when /URL:/ + if event_line + has_url = true + line + else + [] + end + else + event_line ? line : [] + end + }.flatten.join +end + +ical = ERB::new <<~EOF + BEGIN:VCALENDAR + VERSION:2.0 + METHOD:PUBLISH + X-WR-TIMEZONE;VALUE=TEXT:Europe/Berlin + + <%= events.join "\n" %> + + END:VCALENDAR +EOF + +puts ical.result