-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.pl
executable file
·353 lines (262 loc) · 8.16 KB
/
calendar.pl
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/perl
#This code will parse and iCal file and produce what events are happening today. It will then copy them to a html file#
#Modules#
use strict;
use warnings;
use iCal::Parser;
use Data::Dumper;
#First we need to open up our file handles#
open (infile, '<example.html');
open (outfile, '>example-new.html');
#We are now going to copy the old file into the new file#
#This variable stores where the beginning of the editable section is#
my $beginaside="<!--Begin-->\n";
my $currentline="";
while ($currentline ne $beginaside) {
$currentline = <infile>;
print outfile $currentline;
}
#Now that we have found the section we wish to edit we will now construct our calendar#
#Get the Current Date#
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
#Variable for New Month and Day
my $newmon;
my $newmday;
#Adds one to the current month as localtime runs from 0-11#
$mon=$mon+1;
#This variable will store the month with a 0 prefix if needed
my $month;
$month=$mon;
#Pads the day and month with zeros if needed#
if ($mday < 10) {
$mday="0$mday";
}
if ($mon < 10) {
$month="0$mon";
}
#Localtime provides years since 1900 so we need to correct for that#
$year=$year+1900;
#Puts together the startdate string needed by the parser#
my $startdate="$year$month$mday";
#Adds a day to calculate the enddate#
$newmday = $mday+1;
#Pads the day with zeroes#
if ($newmday < 10) {
$newmday="0$newmday";
}
#Detects if day is at the end of the month and wraps around if it is#
if (($newmday == 32) && (($mon == 1) || ($mon == 3) || ($mon == 5) || ($mon == 7) || ($mon == 8) || ($mon == 10))) {
$newmon=$mon+1;
$month=$newmon;
$newmday="01";
if ($newmon < 10) {
$month="0$newmon";
}
}
elsif (($newmday == 31) && (($mon == 4) || ($mon == 6) || ($mon == 9) || ($mon == 11))) {
$newmon=$mon+1;
$month=$newmon;
$newmday="01";
if ($newmon < 10) {
$month="0$newmon";
}
}
elsif (($mon == 2) && ($newmday == 29)) {
$newmon=$mon+1;
$month=$newmon;
$newmday="01";
if ($newmon < 10) {
$month="0$newmon";
}
}
elsif (($newmday == 32) && ($mon == 12)) {
$newmon="1";
$month="01";
$newmday="01";
$year=$year+1;
}
#Caculates the enddate string needed by the parser#
my $enddate="$year$month$newmday";
#Gets rid of the padding for the day because we don't need it anymore.
$mday=$mday+0;
#print "$startdate \n";
#print "$enddate \n";
#print "$mon \n";
#print "$mday \n";
#Now that we have the date we will work on the calendars that need to be parsed.#
#However first we will write our header for the section to the outfile#
#We will start with todays events#
print outfile "<h1>Today's Events</h1>\n";
#We will start with the Regular Calendar
#Name of the calendar file to be read in and parsed#
my $calen="calendar.ics";
#Now we will parse it#
my $ical_parser=iCal::Parser->new('start' => $startdate, 'end' => $enddate);
my $ical=$ical_parser->parse($calen);
#print Dumper($ical->{events}{$year}{$mon}{$mday});
#print keys %{$ical->{'events'}{$year}{$mon}{$mday}};
#print "\n";
#Will iterate through the events for the day and print different facts about them.
my $temp;
my $title;
my $location;
my $starttime;
my $starthr;
my $startmin;
my $endtime;
my $endhr;
my $endmin;
my $ampm;
for $temp ( keys %{$ical->{'events'}{$year}{$mon}{$mday}}) {
# print keys %{$ical->{'events'}{$year}{$mon}{$mday}{$temp}};
# print "\n";
$title=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'SUMMARY'};
$location=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'LOCATION'};
$starthr=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTSTART'}{'local_c'}{'hour'};
$startmin=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTSTART'}{'local_c'}{'minute'};
#Removes back slashes if they appear.#
$title =~ s%(?<!<)\\%%g;
#Converts 24 hr time to 12 hr time#
$ampm="am";
if ($starthr == 12) {
$ampm="pm";
}
if ($starthr > 12) {
$starthr=$starthr-12;
$ampm="pm";
}
if ($starthr == 0) {
$starthr=12;
}
if ($startmin == 0) {
$startmin="00";
}
$starttime="$starthr:$startmin$ampm";
$endhr=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTEND'}{'local_c'}{'hour'};
$endmin=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTEND'}{'local_c'}{'minute'};
#Converts 24 hr time to 12 hr time#
$ampm="am";
if ($endhr == 12) {
$ampm="pm";
}
if ($endhr > 12) {
$endhr=$endhr-12;
$ampm="pm";
}
if ($endhr == 0) {
$endhr=12;
}
if ($endmin == 0) {
$endmin="00"
}
$endtime="$endhr:$endmin$ampm";
# print "$title\n";
# print "$location\n";
# print "$starttime-$endtime\n";
print outfile "<h2>$title</h2>\n";
print outfile "<h3>$starttime-$endtime<h3>\n";
print outfile "<h3>$location<h3>\n";
print outfile "<br>\n";
}
#-----------------------------------------------------------------#
#Next Calendar2#
#Yes I should have made this into a subroutine but I don't feel like learning how to do that in Perl right now. So off to good old fashioned copy and paste.#
#Name of the calendar file to be read in and parsed#
$calen="calendar2.ics";
#Now we will parse it#
$ical_parser=iCal::Parser->new('start' => $startdate, 'end' => $enddate);
$ical=$ical_parser->parse($calen);
#print Dumper($ical->{events}{$year}{$mon}{$mday});
#print keys %{$ical->{'events'}{$year}{$mon}{$mday}};
#print "\n";
#Will iterate through the events for the day and print different facts about them.
for $temp ( keys %{$ical->{'events'}{$year}{$mon}{$mday}}) {
# print keys %{$ical->{'events'}{$year}{$mon}{$mday}{$temp}};
# print "\n";
$title=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'SUMMARY'};
$location=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'LOCATION'};
$starthr=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTSTART'}{'local_c'}{'hour'};
$startmin=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTSTART'}{'local_c'}{'minute'};
#Removes back slashes if they appear.#
$title =~ s%(?<!<)\\%%g;
#Converts 24 hr time to 12 hr time#
$ampm="am";
if ($starthr == 12) {
$ampm="pm";
}
if ($starthr > 12) {
$starthr=$starthr-12;
$ampm="pm";
}
if ($starthr == 0) {
$starthr=12;
}
if ($startmin == 0) {
$startmin="00";
}
$starttime="$starthr:$startmin$ampm";
$endhr=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTEND'}{'local_c'}{'hour'};
$endmin=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'DTEND'}{'local_c'}{'minute'};
#Converts 24 hr time to 12 hr time#
$ampm="am";
if ($endhr == 12) {
$ampm="pm";
}
if ($endhr > 12) {
$endhr=$endhr-12;
$ampm="pm";
}
if ($endhr == 0) {
$endhr=12;
}
if ($endmin == 0) {
$endmin="00"
}
$endtime="$endhr:$endmin$ampm";
# print "$title\n";
# print "$location\n";
# print "$starttime-$endtime\n";
print outfile "<h2>$title</h2>\n";
print outfile "<h3>$starttime-$endtime<h3>\n";
print outfile "<h3>$location<h3>\n";
print outfile "<br>\n";
}
#----------------------------------------------------------------------------------------------#
#Next we will print out the visitors for today#
print outfile "<br><br><br>\n";
print outfile "<h1>Visitors</h1>\n";
#Name of the calendar file to be read in and parsed#
$calen="visitor.ics";
#Now we will parse it#
$ical_parser=iCal::Parser->new('start' => $startdate, 'end' => $enddate);
$ical=$ical_parser->parse($calen);
#print Dumper($ical->{events}{$year}{$mon}{$mday});
#print keys %{$ical->{'events'}{$year}{$mon}{$mday}};
#print "\n";
#Will iterate through the events for the day and print different facts about them.
for $temp ( keys %{$ical->{'events'}{$year}{$mon}{$mday}}) {
#print keys %{$ical->{'events'}{$year}{$mon}{$mday}{$temp}};
#print "\n";
$title=$ical->{'events'}{$year}{$mon}{$mday}{$temp}{'SUMMARY'};
#print "$title\n";
#Removes the "Visiting: "header from the string#
$title = substr($title, 10);
#Removes back slashes if they appear.#
$title =~ s%(?<!<)\\%%g;
print outfile "<h2>$title</h2>\n";
}
#Now that we have completed this section we need to have the infile catch up#
my $endaside="<!--End-->\n";
while ($currentline ne $endaside) {
$currentline = <infile>;
}
#Now we will print the final line of the modified section and copy the rest of the file#
print outfile $currentline;
my $endoffile="</html>\n";
while ($currentline ne $endoffile) {
$currentline = <infile>;
print outfile $currentline;
}
#Closes the file handles#
close(infile);
close (outfile);