-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevents.rb
35 lines (26 loc) · 863 Bytes
/
events.rb
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
require 'net/http'
require 'icalendar'
require 'open-uri'
# List of calendars
calendars = {events: "https://raumzeitlabor.de/events/ical?accept=ics"}
SCHEDULER.every '5m', :first_in => 0 do |job|
calendars.each do |cal_name, cal_uri|
ics = open(cal_uri, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}) { |f| f.read }
cal = Icalendar.parse(ics).first
puts cal
events = cal.events
# select only current and upcoming events
now = Time.now.utc
events = events.select{ |e| e.dtend.to_time.utc > now }
# sort by start time
events = events.sort{ |a, b| a.dtstart.to_time.utc <=> b.dtstart.to_time.utc }[0..1]
events = events.map do |e|
{
title: e.summary,
start: e.dtstart.to_time.to_i,
end: e.dtend.to_time.to_i
}
end
send_event("cal_#{cal_name}", {events: events})
end
end