#!/usr/bin/env ruby # Ramaskrik Program Schedule plotter. # Copyright (C) 2018 Harald Eilertsen # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . $LOAD_PATH << File.join(File.dirname( __FILE__ ), "lib") require 'date' require 'delegate' require 'erb' require 'events' require 'json' require 'scrapers/ramaskrik' require 'nokogiri' require 'open-uri' require 'uri' class EventDecorator < SimpleDelegator attr_reader :offset attr_reader :height def calc_offsets(time_offset) @offset = 5 + (start_time - time_offset).to_i / 50 @height = 5 + duration / 50; start_time + duration end end class SortedEventList attr_reader :venues attr_reader :events attr_reader :start_time attr_reader :end_time def initialize(events) @venues = events.map{ |e| e.venue }.uniq @events = events .sort{ |a,b| a.start_time - b.start_time } .map{ |e| EventDecorator.new(e) } @start_time = @events.first.start_time - (@events.first.start_time.min * 60) @events.group_by{|e| e.venue}.each do |venue, events| time_offset = start_time time_end = start_time events.each do |event| time_end = event.calc_offsets(time_offset) end @end_time = time_end if @end_time.nil? || @end_time < time_end end end end def import_events_from_json(input) JSON .parse(input) .map{ |obj| Events::Event.new(obj) } .group_by{ |e| e.date } end def make_sorted_event_lists_by_date(events_by_date) new_list = {} events_by_date.each do |date, events| new_list[date] = SortedEventList.new(events) end new_list end year = Date::today.year title = "Ramaskrik #{year} - Program" eventlist = make_sorted_event_lists_by_date(import_events_from_json(IO.read(ARGV[0]))) Dir::mkdir('build') unless Dir::exist?('build') t_html = ERB.new(IO.read("index.html.erb"), trim_mode: '>') IO.write("build/index.html", t_html.result(binding)) t_ics = ERB.new(IO.read("program.ics.erb"), trim_mode: '<>') IO.write("build/program-ramaskrik-#{year}.ics", t_ics.result(binding).gsub(/\n/, "\r\n"))