-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-ical
executable file
·212 lines (178 loc) · 5.12 KB
/
parse-ical
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env ruby
# coding: utf-8
XMLFILE="/srv/www.das-labor.org/htdoc/termine.xml"
NRXMLFILE="/srv/www.das-labor.org/htdoc/talksworkshops.xml"
ANNXMLFILE="/srv/www.das-labor.org/htdoc/announce.xml"
ICALFILE="/srv/www.das-labor.org/htdoc/termine.ics"
#ICALFILE="./termine.ics"
#############################################################################
# Starts Here
require 'rubygems'
require 'ri_cal'
require 'date'
class LaborTermine
attr_reader "termine"
attr_reader "nrtermine"
def initialize()
@termine = Array.new
@nrtermine = Array.new
end
def parse()
term = Array.new # array to return (all events)
nrterm = Array.new # array to return (non-recurring events only)
cals= File.open(ICALFILE, "r:UTF-8")
Object.instance_eval do
const_set "OldDate", const_get("Date")
remove_const "Date"
const_set "Date", DateTime
end
cal = RiCal.parse(cals)
Object.instance_eval do
remove_const "Date"
const_set "Date", OldDate
end
## sort recurring from non-recurring events
cal.first.events.each{|e|
# push non-reccurring event parameters into hash table
if !e.recurs? then
e.occurrences(:starting => DateTime.now).each{|nre|
nrt = Hash.new
#puts ":::::::..NON-RECURRING EVENT..:::::::"
#puts nre.dtstart
nrt["date"] = nre.dtstart
#puts nre.summary
nrt["text"] = nre.summary
if nre.url != nil then
#puts nre.url
nrt["link"] = nre.url
else
#puts nre.attach
nrt["link"] = nre.attach
end
# push hash table into array
nrterm.push(nrt)
}
end
# put all relevant occurances of an event
e.occurrences(:before => DateTime.now+(14) >> 1, :starting => DateTime.now).each{|re|
t = Hash.new
#puts "::::::::..ANY EVENT..::::::::"
#puts re.dtstart
t["date"] = re.dtstart
#puts re.summary
t["text"] = re.summary
if re.url != nil then
#puts re.url
t["link"] = re.url
else
#puts re.attach
t["link"] = re.attach
end
#push event hash table into array
term.push(t)
}
}
thash = Hash.new
thash["termine"] = term
thash["nrtermine"] = nrterm
return thash
end
def sort(tarr) # events are not necessarily ordered by date
# puts "Sorting dates ..."
d = Array.new # array to sort dates
ttermine = Array.new # temp array to overwrite @termine with
tarr.each{|t|
d.push(t["date"])
}
d.sort!
# puts "Sorting events by date ..."
d.each{|da|
tarr.each{|t|
if t["date"] == da then
ttermine.push(t)
end
}
}
return ttermine
end
def properdate(tarr)
p tarr
tarr.each{|t|
# pad with leading zeros and add the results as idividual keys to our hash
t["day"] = "%02d" % t["date"].day.to_s
t["month"] = "%02d" % t["date"].mon.to_s
t["year"] = t["date"].year.to_s
t["hour"] = t["date"].respond_to?(:hour) ? "%02d" % t["date"].hour.to_s : ""
t["min"] = t["date"].respond_to?(:min ) ? "%02d" % t["date"].min.to_s : ""
# translate numeric day of week to a meaningful string
t["wday"] = %w{So Mo Di Mi Do Fr Sa}[t["date"].wday]
# anmerkung Asklepios: nicht loeschen behebt das problem mit dem "NilClass" XXX Fixme!
#t.delete("date") # currently we do not need the comlete date anymore => delete it
}
return tarr
end
def debug(arr)
puts "\n \n::::::::::::::::..Debug output..::::::::::::::::::::"
arr.each{|e|
puts ":::::::..Event..::::::::"
puts e["date"]
puts e["date"].gregorian?
puts e["text"]
puts e["link"]
}
end
def run
parsedtermine = parse()
# all events
# puts "handling all events ..."
termine = sort(parsedtermine["termine"])
@termine = properdate(termine)
# non-recurring events only
# puts "handling non-recurring events ..."
nrtermine = sort(parsedtermine["nrtermine"])
@nrtermine = properdate(nrtermine)
end
end
####
# MAIN
lt = LaborTermine.new # create instance
lt.run # run the parser
puts "Alle Termine:"
File.open( XMLFILE, "w" ) { |f|
f.puts "<termine>"
lt.termine.each { |termin|
puts "#{termin["day"]}.#{termin["month"]}.#{termin["year"]} #{termin["hour"]}:#{termin["min"]} #{termin["text"]}"
f.puts "<termin>"
termin.each { |key,val|
f.puts " <#{key}>#{val.to_s.encode(xml: :text)}</#{key}>"
}
f.puts "</termin>"
}
f.puts "</termine>"
}
puts "Vorträge und Workshops:"
File.open( NRXMLFILE, "w" ) { |f|
f.puts "<termine>"
lt.nrtermine.each { |termin|
puts "#{termin["day"]}.#{termin["month"]}.#{termin["year"]} #{termin["hour"]}:#{termin["min"]} #{termin["text"]}"
f.puts "<termin>"
termin.each { |key,val|
f.puts " <#{key}>#{val.to_s.encode(xml: :text)}</#{key}>"
}
f.puts "</termin>"
}
f.puts "</termine>"
}
puts "Veranstaltungsankündigung:"
File.open( ANNXMLFILE, "w" ) { |f|
f.puts "<termine>"
lt.nrtermine.each { |termin|
puts "#{termin["day"]}.#{termin["month"]}.#{termin["year"]} #{termin["hour"]}:#{termin["min"]} #{termin["text"]}"
f.puts "<termin>"
termin.each { |key,val|
f.puts " <#{key}>#{val.to_s.encode(xml: :text)}</#{key}>"
}
f.puts "</termin>"
}
f.puts "</termine>"
}