3 # -*- perl -*- vim: ft=perl sw=4 sts=4
5 # Brief usage instructions:
6 # Create a ~/.sct6rc that has SID=yourssn and PIN=yourpin
7 # You'll need to change the url below if you go anywhere but TAMUK.
10 use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Decode_Month Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Nth_Weekday_of_Month_Year Gmtime Mktime);
11 use Date::Calendar::Profiles qw($Profiles);
12 use Date::Calendar::Year;
15 use vars qw(%opts %faculty);
19 $opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
20 "Martin Luther King Day" => "3/Mon/Jan",
21 "Good Friday" => "-2",
22 "Spring Break Monday" => \&Spring_Break,
23 "Spring Break Tuesday" => \&Spring_Break,
24 "Spring Break Wednesday" => \&Spring_Break,
25 "Spring Break Thursday" => \&Spring_Break,
26 "Spring Break Friday" => \&Spring_Break,
27 "Spring Break Saturday" => \&Spring_Break,
28 "Study Day" => "4/Thu/Apr", # ?
29 "Memorial Day" => "5/Mon/May",
30 "Independence Day" => \&US_Independence,
31 "Labor Day" => \&US_Labor,
32 #"Columbus Day" => "2/Mon/Oct",
33 "Thanksgiving Day" => "4/Thu/Nov",
34 "Thanksgiving Friday" => \&Thanksgiving_Friday,
38 my($year,$label) = @_;
39 $label =~ s/^Spring Break //;
40 return( Add_Delta_Days(
41 Nth_Weekday_of_Month_Year($year,1,1,1),
42 7*(11-1) # This is for the 11th Monday of the year
43 +Decode_Day_of_Week($label)-1) );
46 sub Thanksgiving_Friday {
47 my($year,$label) = @_;
48 return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
50 sub US_Independence # Fourth of July
52 my($year,$label) = @_;
53 return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
55 sub US_Labor # First Monday after the first Sunday in September
57 my($year,$label) = @_;
58 return( Add_Delta_Days(
59 Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
62 my $config = $ENV{HOME} . "/.schedprocrc";
63 if (($ARGV[0] || "") eq '-F') {
69 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
75 next unless m/^([^=]*)=(.*)/;
80 my $schedurl = $ENV{HOME} . "/schedule.xml";
81 $schedurl = $opts{'schedule'} if(defined($opts{'schedule'}));
82 my $gradeurl = $ENV{HOME} . "/grades.xml";
83 $gradeurl = $opts{'grades'} if(defined($opts{'schedule'}));
86 my ($section, $number) = split("-", shift);
88 foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
89 $id=10000*$id+$number;
95 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
96 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
97 my $firstday = Date::Calc->new($1,$2,$3);
98 my $lastday = Date::Calc->new($4,$5,$6);
99 my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
101 foreach(split(" ",$class{'days'})) {
102 push @days, Decode_Day_of_Week($_);
105 @off = @{$class{'off'}} if ($class{'off'});
106 for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
107 next unless(grep($_ == Day_of_Week($today->date), @days));
108 next if(grep($_ == "$today", @off));
111 $today = Date::Calc->new(Date::Calc->gmtime->date);
112 for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
113 next unless(grep($_ == Day_of_Week($today->date), @days));
114 next if(grep($_ == "$today", @off));
120 sub off_for_holidays {
122 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
123 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
124 my $firstday = Date::Calc->new($1,$2,$3);
125 my $lastday = Date::Calc->new($4,$5,$6);
126 my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
129 foreach(split(" ",$class{days})) {
130 $days[Decode_Day_of_Week($_)] = 1;
132 foreach ($year->search("")) {
134 #foreach my $x ($year->labels($_)) {
135 # $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
137 next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
139 if (defined($days[Day_of_Week($_->date)]) && $good > 0);
141 return wantarray ? @holidays : "@holidays";
146 my $time = $class{'begin'} . "-" . $class{'end'};
147 $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
148 my $lastday = Date::Calc->new($1,$2,$3);
149 return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
150 my $beginning="(08:00-09:15|11:00-12:15|14:00-15:15|17:00-18:15|17:30-18:45|20:00-21:15|20:30-21:45)";
151 my $ending ="(06:30-07:45|09:30-10:45|12:30-13:45|15:30-16:45|18:30-19:45|19:00-20:15)";
152 my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
154 if($time =~ /$beginning/) {
156 } elsif($time =~ /$ending/) {
159 } elsif($days eq "2 4") {
160 if($time =~ /$beginning/) {
162 } elsif($time =~ /$ending/) {
165 } elsif($days eq "5") {
168 return wantarray ? () : "";
172 local $_ = shift || "";
174 s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
175 s/\b(I)(i*)\b/$1\U$2/g;
177 s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
178 s/\b(Mc)([a-z])/$1\u$2/g;
179 s/\b(Tcp\/Ip|Pc|Tba)\b/\U$&/g;
180 s/\bThru\b/Through/g;
181 s/\bAcct\b/Accounting/g;
182 s/\bAmer\b/American/g;
183 s/\bChem\b/Chemistry/g;
184 s/\bComp\b/Composition/g;
187 s/\bIntro\b/Introduction/g;
188 s/\bPrgm\b/Programming/g;
189 s/\bOp Sys\b/Operating System/g;
190 #s/\bGovt\b/Government/g;
191 s/\bLit\b/Literature/g;
192 s/\bPrin\b/Principles/g;
193 s/\bBus\b/Business/g;
200 if($schedurl =~ /:\/\//) {
201 my $ua = LWP::UserAgent->new;
204 # $ua->cookie_jar( {} );
205 $response = $ua->get("$schedurl") or die "$!";
206 die $response->status_line unless $response->is_success;
207 $content = $response->content;
209 open(F,$schedurl) || die "$!";
210 $content = join ("", <F>);
213 my $ref = XMLin($content, ForceArray => [ 'class', 'off' ], KeyAttr => "");
214 return @{$ref->{'class'}};
219 if($gradeurl =~ /:\/\//) {
220 my $ua = LWP::UserAgent->new;
223 # $ua->cookie_jar( {} );
224 $response = $ua->get("$gradeurl") or die "$!";
225 die $response->status_line unless $response->is_success;
226 $content = $response->content;
228 open(F,$schedurl) || die "$!";
229 $content = join ("", <F>);
232 my $ref = XMLin($content, ForceArray => [ 'class', 'cumulative' ], KeyAttr => "");
238 my @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points");
239 #$file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
240 $grades = get_grades(@_);
243 print '<table id="grades" cellpadding="3" cellspacing="0">'."\n<tr>";
244 print '<th align="left" class="sectionid">', $showheaders[0];
245 print '</th><th align="left" class="coursetitle">', $showheaders[1];
246 print '</th><th align="center" class="grade">';
247 print join('</th><th align="center" class="right-number">',@showheaders[2 .. 5]);
248 print "</th></tr>\n";
249 foreach my $ts ($grades->{'class'}) {
250 foreach my $row (@$ts) {
251 print '<tr class="'.$shade.'">';
252 $shade = ($shade eq "dark"?"light":"dark");
253 print '<td align="left" class="sectionid">', $row->{'id'}, '</td>';
254 print '<td align="left" class="coursetitle">', $row->{'title'}, '</td>';
255 print '<td align="center" class="grade">';
256 #print join('</td><td>', @$row[0 .. 1]);
257 print join('</td><td align="right" class="right-number">', ($row->{'grade'},$row->{'earned'},$row->{'hours'},$row->{'points'}));
258 print "</td></tr>\n";
261 my $lastrow = $grades->{'cumulative'}->[scalar @{$grades->{'cumulative'}}-1];
262 print '<tr class="cumulative '.$shade.'"><td id="cumulative" colspan="2">Cumulative: through ', capitalize($lastrow->{'term'});
263 print '</td><td align="center" id="gpa" class="grade">';
264 print $lastrow->{'gpa'};
265 print '</td><td align="right" class="right-number">';
266 print join('</td><td align="right" class="right-number">', ($lastrow->{'earned'}, $lastrow->{'hours'}, $lastrow->{'points'}));
267 print "</td></tr>\n";
271 sub get_faculty_email {
272 my ($name, $school, $email);
273 if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
274 open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
277 m/"([^"]*)",([^,]*),([^,]*)/; # "
278 ($name, $email, $school) = ($1, $2, $3);
279 $name =~ s/ [A-Z]\.//g;
282 $faculty{$name} = $email;
286 $name =~ s/ [A-Z]r?\.//g;
289 return $faculty{$name};
294 "X-SC-Subject: New Years Day\nX-SC-Category: Holiday\nX-SC-Cond: 1 Jan\nX-SC-Duration: 00010101-\nX-SC-Record-Id: <New_Years_Day\@from.sctweb>\n",
295 "X-SC-Subject: Martin Luther King, Jr. Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Jan\nX-SC-Duration: 19870119-\nX-SC-Record-Id: <Martin_Luther_King_Jr_Day\@from.sctweb>\n",
296 "X-SC-Subject: Presidents Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Feb\nX-SC-Duration: 19710515-\nX-SC-Record-Id: <Presidents_Day\@from.sctweb>\n",
297 "X-SC-Subject: Memorial Day\nX-SC-Category: Holiday\nX-SC-Cond: Last Mon May\nX-SC-Duration: 19710531-\nX-SC-Record-Id: <Memorial_Day\@from.sctweb>\n",
298 "X-SC-Subject: Independence Day\nX-SC-Category: Holiday\nX-SC-Cond: 4 Jul\nX-SC-Duration: 17760704-\nX-SC-Record-Id: <Independence_Day\@from.sctweb>\n",
299 "X-SC-Subject: Labor Day\nX-SC-Category: Holiday\nX-SC-Cond: 1st Mon Sep\nX-SC-Duration: 18840901-\nX-SC-Record-Id: <Labor_Day\@from.sctweb>\n",
300 "X-SC-Subject: Columbus Day\nX-SC-Category: Holiday\nX-SC-Cond: 2nd Mon Oct\nX-SC-Duration: 19711011-\nX-SC-Record-Id: <Columbus_Day\@from.sctweb>\n",
301 "X-SC-Subject: Veterans Day\nX-SC-Category: Holiday\nX-SC-Cond: 11 Nov\nX-SC-Duration: 19261111-\nX-SC-Record-Id: <Veterans_Day\@from.sctweb>\n",
302 "X-SC-Subject: Thanksgiving\nX-SC-Category: Holiday\nX-SC-Cond: 4th Thu Nov\nX-SC-Duration: 14921122-\nX-SC-Record-Id: <Thanksgiving\@from.sctweb>\n",
303 "X-SC-Subject: Christmas\nX-SC-Category: Holiday\nX-SC-Cond: 25 Dec\nX-SC-Duration: 00011225-\nX-SC-Record-Id: <Christmas\@from.sctweb>\n",
307 sub do_mhc_schedule {
309 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
310 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
311 my ($file, $current, @mhc, @schedule);
312 $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
313 @schedule = get_schedule(@_);
314 @mhc = get_mhc_header;
315 if(defined($file) && (-d $file)) {
318 $_ =~ s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/;
319 $_ =~ s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/;
320 $_ =~ s/X-SC-Duration: (\d\d\d\d)(\d\d)(\d\d)-/"X-SC-Duration: $1$2$3-\nDate: $3 " . $mon[$2-1] . " 1970 12:00:00 +0000"/e;
325 foreach my $row (@schedule) {
326 map {s/\n/-/g if defined; $_} %$row;
327 my $id=generate_id($row->{'id'});
328 $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
329 $row->{'days'} =~ s/^ //;
330 #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
332 @day = map { "!" . $_ } (@{$row->{'off'}}) if ($row->{'off'});
335 $current .= "X-SC-Subject: " . $row->{'title'} . "\n";
336 $current .= "X-SC-Location: " . $row->{'location'} . "\n";
337 $current .= "X-SC-Category: School\n";
338 $current .= "X-SC-Cond: " . $row->{'days'} . "\n";
339 $current .= "X-SC-Time: ".$row->{'begin'}."-".$row->{'end'}."\n";
340 $current .= "X-SC-Duration: " . $row->{'duration'} . "\n";
341 $current .= "X-SC-Day: @day\n" if(exists $day[0]);
342 $current .= "X-SC-Alarm: 15 minutes\n";
343 $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n";
344 if(defined($file) && (-d $file)) {
345 my $email = get_faculty_email $row->{'instructor'};
346 $row->{'instructor'} = '"' . $row->{'instructor'} . '" <'. ($email || ($1 || "unknown") . "\@from.sctweb") . ">";
347 $row->{'duration'} =~ /^(\d\d\d\d)(\d\d)(\d\d)-\d{8}$/;
348 $row->{'begin'} =~ /^(\d\d):(\d\d)$/;
349 my $next = next_class(%$row);
350 my @date = Gmtime(Mktime($next->date,$1,$2,0));
351 $current .= sprintf "Date: %s, %2d %s %4d %02d:%02d:00 +0000\n", Day_of_Week_Abbreviation($date[7]), $date[2], $mon[$date[1]-1], $date[0], $date[3], $date[4], $date[5];
352 $current .= "Subject: " . $row->{'title'} . "\n";
353 $current .= "From: " . $row->{'instructor'} . "\n";
358 if(defined($file) && (-d $file)) {
360 foreach my $name (<$file/[1-9]*>) {
361 next unless $name =~ /^$file\/[1-9][0-9]*$/;
366 unlink $name if /^X-SC-Record-Id: <.*\@from.sctweb>/;
372 open FH, ">$file/$i" or die $!;
377 open(STDOUT, ">" . $file) if(defined($file));
378 print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n";
379 print join("\n", @mhc);
383 sub do_csv_schedule {
385 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
386 my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.);
387 my (@terms, $file, $current, @mhc, @schedule);
388 $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
389 @schedule = get_schedule(@_);
390 open(STDOUT, ">" . $file) if(defined($file));
391 foreach my $row (@schedule) {
392 map {s/\n/-/g if defined; $_} %$row;
393 my $id=generate_id($row->{'id'});
394 $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
395 $row->{'days'} =~ s/^ //;
396 my $next = next_class(%$row);
399 $current .= $row->{'id'} . ",";
400 $current .= $row->{'title'} . ",";
401 $current .= '"' . $row->{'instructor'} . '",';
402 $next =~ /^(\d\d\d\d)(\d\d)(\d\d)$/;
403 $current .= $1 . "-". $2 ."-" . $3 . "\n";
408 sub do_vcalendar_schedule {
410 my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU");
411 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
412 my @schedule = get_schedule(@_);
413 open(STDOUT, ">>" . $file) if(defined($file) && (! -d $file));
414 open(STDOUT, ">/dev/null") if(defined($file) && (-d $file));
415 print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
416 foreach my $row (@schedule) {
417 map { s/\n/-/g; $_} %$row;
418 $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
419 $row->{'days'} =~ s/^ //;
421 @day = @{$row->{'off'}} if ($row->{'off'});
423 if(exists($day[0])) {
424 $day = join(";", @day);
425 $day =~ s/\b(\d{8})\b/$1T000000/g;
427 my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
428 my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
431 if(defined($file) && (-d $file)) {
432 open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
434 print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
436 print "BEGIN:VEVENT\r\n";
437 print "SUMMARY:", $row->{'title'}, "\r\n";
438 print "DESCRIPTION:", $row->{'id'}, "\r\n";
439 print "LOCATION:", $row->{'location'}, "\r\n";
440 print "CATEGORIES:Education\r\n";
441 print "DTSTART:", $startdate."T".$starttime, "00\r\n";
442 print "DTEND:", $startdate."T".$stoptime, "00\r\n";
443 print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
444 print("EXDATE:$day\r\n") if($day);
445 print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{'name'}, "\r\n" if(defined($opts{'name'}));
446 print "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\r\n";
447 print "END:VEVENT\r\n";
448 if(defined($file) && (-d $file)) {
449 print "END:VCALENDAR\r\n";
454 print "END:VCALENDAR\r\n";
457 sub do_xml_schedule {
458 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
459 my $schedule = { class => [ get_schedule(@_) ] };
460 my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
470 sub do_html_schedule {
471 my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location");
473 my @schedule = get_schedule(@_);
474 print '<table id="schedule" cellpadding="3" cellspacing="0">'."\n<tr><th>";
475 print join("</th><th>",@showheaders);
476 print "</th></tr>\n";
477 foreach my $row (@schedule) {
478 map { s/\n/<br \/>/g; $_} %$row;
479 #$row->{'time'} =~ s/-/-<wbr \/>/;
480 $row->{'duration'} =~ s/-/<br \/>/g;
481 $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
482 print '<tr class="'.$shade.'">';
483 $shade = ($shade eq "dark"?"light":"dark");
484 print '<td class="idtitle">';
485 print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
486 print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
487 my $instructor = $row->{'instructor'};
488 my $email = get_faculty_email($row->{'instructor'});
490 #print '<td><a href="mailto:' . $email . '">'
491 #. $row->{'instructor'} . "</a></td>";
493 print "<td>" . $row->{'instructor'} . "</td>";
495 print "<td>" . $row->{'days'} . "</td>";
496 print "<td>" . $row->{'begin'}."-<wbr />".$row->{'end'} . "</td>";
497 print "<td>" . $row->{'duration'} . "</td>";
498 print "<td>" . $row->{'location'} . "</td>";
504 if ($arg eq "-s" || $arg eq "-h") {
505 do_html_schedule(@ARGV);
506 } elsif ($arg eq "-m") {
507 do_mhc_schedule(@ARGV);
508 } elsif ($arg eq "-c") {
509 do_csv_schedule(@ARGV);
510 } elsif ($arg eq "-v") {
511 do_vcalendar_schedule(@ARGV);
512 } elsif ($arg eq "-x") {
513 do_xml_schedule(@ARGV);
514 } elsif ($arg eq "-g") {
515 do_html_grades(@ARGV);