aboutsummaryrefslogtreecommitdiffstats
path: root/ramaskrik-program.rb
blob: 8bda384ce80e62633bd8d5726089926d7e5c9edf (plain) (blame)
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
#!/usr/bin/env ruby

# Ramaskrik Program Schedule plotter.
# Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
#
# 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 <http://www.gnu.org/licenses/>.

$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"))