Syntax highlighting correction
[tpope-extra.git] / perl / sct6
1 #!/usr/bin/perl -w
2 # $Id$
3 # -*- perl -*- vim: ft=perl sw=4 sts=4
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.
8
9 use strict;
10 use LWP::UserAgent;
11 use HTML::TableExtract;
12 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);
13 use Date::Calendar::Profiles qw($Profiles);
14 use Date::Calendar::Year;
15 use vars qw($ua %opts %faculty);
16
17 my ($response);
18
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,
35 };
36
37 sub Spring_Break {
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) );
44 }
45
46 sub Thanksgiving_Friday {
47     my($year,$label) = @_;
48     return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
49 }
50 sub US_Independence # Fourth of July
51 {
52     my($year,$label) = @_;
53     return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
54 }
55 sub US_Labor # First Monday after the first Sunday in September
56 {
57     my($year,$label) = @_;
58     return( Add_Delta_Days(
59         Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
60 }
61
62 my $config = $ENV{HOME} . "/.sct6rc";
63 if (($ARGV[0] || "") eq '-F') {
64     shift;
65     $config = shift;
66 }
67
68 my $arg = "";
69 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
70
71 if (-r $config) {
72     open CONFIG, $config;
73     while(<CONFIG>) {
74         s/\#.*//;
75         next unless m/^([^=]*)=(.*)/;
76         $opts{$1}=$2;
77     }
78     close CONFIG;
79 }
80 my $domain = $opts{'domain'} || "as1.tamuk.edu:9003";
81 my $url = "https://$domain/pls/PROD/";
82 $url = $opts{'url'} if(defined($opts{'url'}));
83
84 $ua = LWP::UserAgent->new;
85 $ua->timeout(10);
86 $ua->env_proxy;
87 $ua->cookie_jar( {} );
88 $ua->get("${url}twbkwbis.P_WWWLogin") or die "$!";
89 $response = $ua->post("${url}twbkwbis.P_ValLogin", { sid => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
90
91 $response = $ua->get("${url}bwskflib.P_SelDefTerm"); # Valid terms
92 die $response->status_line unless $response->is_success;
93 my @terms = grep {s/^<option value="([^"]*)">.*/$1/i} (split( /\r?\n/, $response->content)); # "
94 die "Site down. Try again later.\n" unless (@terms);
95
96 sub generate_id {
97     my ($section, $number) = split("-", shift);
98     my $id = 0;
99     foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
100     $id=10000*$id+$number;
101     return $id;
102 }
103
104 sub next_class {
105     my %class = @_;
106     $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
107     my $days = Delta_Days($1,$2,$3,$4,$5,$6);
108     my $firstday = Date::Calc->new($1,$2,$3);
109     my $lastday = Date::Calc->new($4,$5,$6);
110     my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
111     my @days=();
112     foreach(split(" ",$class{'days'})) {
113         push @days, Decode_Day_of_Week($_);
114     }
115     my @off = (off_for_holidays(%class),off_for_exams(%class));
116     for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
117         next unless(grep($_ == Day_of_Week($today->date), @days));
118         next if(grep($_ == $today, @off));
119         return $today;
120     }
121     $today = Date::Calc->new(Date::Calc->gmtime->date);
122     for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
123         next unless(grep($_ == Day_of_Week($today->date), @days));
124         next if(grep($_ == $today, @off));
125         return $today;
126     }
127     return undef;
128 }
129
130 sub off_for_holidays {
131     my %class = @_;
132     $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
133     my $days = Delta_Days($1,$2,$3,$4,$5,$6);
134     my $firstday = Date::Calc->new($1,$2,$3);
135     my $lastday = Date::Calc->new($4,$5,$6);
136     my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
137     my @holidays=();
138     my @days=();
139     foreach(split(" ",$class{days})) {
140         $days[Decode_Day_of_Week($_)] = 1;
141     }
142     foreach ($year->search("")) {
143         my $good=1;
144         #foreach my $x ($year->labels($_)) {
145         #    $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
146         #}
147         next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
148         push @holidays, $_
149             if (defined($days[Day_of_Week($_->date)]) && $good > 0);
150     }
151     return wantarray ? @holidays : "@holidays";
152 }
153
154 sub off_for_exams {
155     my %class = @_;
156     my $time = $class{'begin'} . "-" . $class{'end'};
157     $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
158     my $lastday = Date::Calc->new($1,$2,$3);
159     return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
160     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)";
161     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)";
162     my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
163     if($days eq "1 3") {
164         if($time =~ /$beginning/) {
165             return $lastday-4;
166         } elsif($time =~ /$ending/) {
167             return $lastday-6;
168         }
169     } elsif($days eq "2 4") {
170         if($time =~ /$beginning/) {
171             return $lastday-3;
172         } elsif($time =~ /$ending/) {
173             return $lastday-5;
174         }
175     } elsif($days eq "5") {
176         return $lastday-2;
177     }
178     return wantarray ? () : "";
179 }
180
181 sub capitalize {
182     local $_ = shift || "";
183     s/ +$//;
184     s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
185     s/\b(I)(i*)\b/$1\U$2/g;
186     s/\bUs\b/US/g;
187     s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
188     s/\b(Mc)([a-z])/$1\u$2/g;
189     s/\bTba\b/TBA/g;
190     s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
191     s/\bThru\b/Through/g;
192     s/\bAcct\b/Accounting/g;
193     s/\bAmer\b/American/g;
194     s/\bChem\b/Chemistry/g;
195     s/\bComp\b/Composition/g;
196     s/\bFed\b/Federal/g;
197     s/\bGen\b/General/g;
198     s/\bIntro\b/Introduction/g;
199     s/\bPrgm\b/Programming/g;
200     s/\bOp Sys\b/Operating System/g;
201     #s/\bGovt\b/Government/g;
202     s/\bLit\b/Literature/g;
203     s/\bPrin\b/Principles/g;
204     s/\bBus\b/Business/g;
205     return $_;
206 }
207
208 sub get_schedule_terms {
209     my (@sterms, @a);
210     @a=('10','20','30','40');   # Fall, Spring, Summer I, Summer II
211                                 # Last 3 are currently guesses
212     my @localtime=localtime();
213     if ($localtime[4]<3) { # Through Mar 31
214         @sterms=((1900+$localtime[5]).$a[1]);
215     } elsif ($localtime[4]<5) { # Through May 31
216         @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
217     } elsif ($localtime[4]<7) { # through July 31
218         @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
219     } elsif ($localtime[4]<8) { # through Aug 31
220         @sterms=((1900+$localtime[5]).$a[3],(1901+$localtime[5]).$a[0]);
221     } elsif ($localtime[4]<10) { # through Oct 31
222         @sterms=((1901+$localtime[5]).$a[0]);
223     } else {
224         @sterms=((1901+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
225     }
226     return @sterms;
227 }
228
229 sub get_schedule {
230     my @readheaders = ("Type", "Time", "Days", "Where", "Date Range", "Schedule Type", "Instructors");
231     my @class;
232     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
233     my (@schedule, @terms, $classid, $title, $begin, $end, $times, $days, $session);
234     @terms = get_schedule_terms();
235     foreach (@_ ? @_ : @terms) {
236         $response = $ua->get("${url}bwskfshd.P_CrseSchdDetl?term_in=$_" );
237         die $response->status_line unless $response->is_success;
238         $te->parse($response->content);
239         foreach my $l (split (/\n/, $response->content)) {
240             next unless $l =~ s/.*<CAPTION class=[^>]*>(.* - .... \d\d\d\d \d\d\d)<\/caption>.*/$1/i;
241             push @class, $l;
242         }
243     }
244     foreach my $ts ($te->table_states) {
245         foreach my $row ($ts->rows) {
246             #map { s/\xa0//g; $_} @$row;
247             #$row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
248             #$row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
249             $classid = (shift @class);
250             $title = $classid;
251             $classid =~ s/.* - //;
252             $classid =~ s/ /-/g;
253             $title =~ s/ - .*//;
254             $row->[6] = capitalize($row->[6]);
255             $row->[6] =~ s/([A-Z]r?)$/$1./;
256             $row->[6] =~ s/ *\([A-Z]\)//;
257             $row->[1] =~ s/ ?([ap])m/\u$1M/g;
258             $row->[1] =~ s/ - /-/;
259             ($begin, $end) = split(/ ?- ?/, $row->[1]);
260             $begin =~ s/^(\d):/0$1:/;
261             $end =~ s/^(\d):/0$1:/;
262             if ($begin =~ /pm$/i && $begin !~ /^12/) {
263                 $begin =~ s/^(\d?\d)/$1+12/e;
264             }
265             if ($end =~ /pm$/i && $end !~ /^12/) {
266                 $end =~ s/^(\d?\d)/$1+12/e;
267             }
268             $begin =~ s/ ?[AP]M//;
269             $end =~ s/ ?[AP]M//;
270             $row->[4] =~ s/([A-Za-z]{3,9})/(Decode_Month($1)<10?"0":"").Decode_Month($1)/eg;
271             $row->[4] =~ s/(\d\d?) (\d\d), (\d\d\d\d)/$3$1$2/g;
272             $row->[4] =~ s/ - /\n/;
273             $row->[3] =~ s/(ON|MAIN) CAMPUS\n|Palo Alto Building \d* //ig;
274             $row->[3] =~ s/\n/ /g;
275             push @schedule, {
276                 id => $classid,
277                 title => $title,
278                 #session => $row->[1],
279                 instructor => $row->[6],
280                 #credits => $row->[3],
281                 #callnumber => $row->[4],
282                 #gradetype => $row->[5],
283                 days => $row->[2],
284                 'time' => $row->[1],
285                 begin => $begin,
286                 end => $end || undef,
287                 duration => $row->[4],
288                 location => $row->[3],
289                 #message => $row->[10],
290             };
291         }
292     }
293     return @schedule;
294 }
295
296 sub get_faculty_email {
297     my ($name, $school, $email);
298     if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
299         open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
300         while($_ = <INS>) {
301             chomp;
302             m/"([^"]*)",([^,]*),([^,]*)/; # "
303             ($name, $email, $school) = ($1, $2, $3);
304             $name =~ s/^([^,]*), ([^,]*)(.*)$/$2 $1$3/;
305             $name =~ s/ [A-Z]\.//g;
306             $name = lc $name;
307             $name =~ s/\W//g;
308             $faculty{$name} = $email;
309         }
310     }
311     $name = shift;
312     $name =~ s/ [A-Z]r?\.//g;
313     $name = lc $name;
314     $name =~ s/\W//g;
315     return $faculty{$name};
316 }
317
318 sub get_mhc_header {
319 return (
320 "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",
321 "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",
322 "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",
323 "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",
324 "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",
325 "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",
326 "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",
327 "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",
328 "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",
329 "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",
330 );
331 }
332
333 sub do_mhc_schedule {
334     $| = 1;
335     my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
336     my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
337     my ($file, $current, @mhc, @schedule);
338     $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
339     @schedule = get_schedule(@_);
340     @mhc = get_mhc_header;
341     if(defined($file) && (-d $file)) {
342         my @mhc2;
343         foreach (@mhc) {
344             $_ =~ s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/;
345             $_ =~ s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/;
346             $_ =~ 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;
347             push @mhc2, $_;
348         }
349         @mhc = @mhc2;
350     }
351     foreach my $row (@schedule) {
352         map {s/\n/-/g if defined; $_} %$row;
353         my $id=generate_id($row->{'id'});
354         $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
355         $row->{'days'} =~ s/^ //;
356         #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
357         my @day = map { "!" . $_ } (off_for_holidays(%$row),off_for_exams(%$row));
358         $current = "";
359         #print "# $id\n";
360         $current .= "X-SC-Subject: " . $row->{'title'} . "\n";
361         $current .= "X-SC-Location: " . $row->{'location'} . "\n";
362         $current .= "X-SC-Category: School\n";
363         $current .= "X-SC-Cond: " . $row->{'days'} . "\n";
364         $current .= "X-SC-Time: ".$row->{'begin'}."-".$row->{'end'}."\n";
365         $current .= "X-SC-Duration: " . $row->{'duration'} . "\n";
366         $current .= "X-SC-Day: @day\n" if(exists $day[0]);
367         $current .= "X-SC-Alarm: 15 minutes\n";
368         $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n";
369         if(defined($file) && (-d $file)) {
370             $row->{'instructor'} =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
371             my $email = get_faculty_email $row->{'instructor'};
372             $row->{'instructor'} = '"' . $row->{'instructor'} . '" <'. ($email || ($1 || "unknown") . "\@from.sctweb") . ">";
373             $row->{'duration'} =~ /^(\d\d\d\d)(\d\d)(\d\d)-\d{8}$/;
374             $row->{'begin'} =~ /^(\d\d):(\d\d)$/;
375             my $next = next_class(%$row);
376             my @date = Gmtime(Mktime($next->date,$1,$2,0));
377             $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];
378             $current .= "Subject: " . $row->{'title'} . "\n";
379             $current .= "From: " . $row->{'instructor'} . "\n";
380         }
381         #print "\n$current";
382         push @mhc, $current;
383     }
384     if(defined($file) && (-d $file)) {
385         my @lines;
386         foreach my $name (<$file/[1-9]*>) {
387             next unless $name =~ /^$file\/[1-9][0-9]*$/;
388             open FH, $name;
389             @lines = <FH>;
390             close FH;
391             foreach (@lines) {
392                 unlink $name if /^X-SC-Record-Id: <.*\@from.sctweb>/;
393             }
394         }
395         my $i=0;
396         foreach (@mhc) {
397             while(-f ++$i) {}
398             open FH, ">$file/$i" or die $!;
399             print FH $_;
400             close FH;
401         }
402     } else {
403         open(STDOUT, ">" . $file) if(defined($file));
404         print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n";
405         print join("\n", @mhc);
406     }
407 }
408
409 sub do_csv_schedule {
410     $| = 1;
411     my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
412     my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.);
413     my (@terms, $file, $current, @mhc, @schedule);
414     $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
415     @schedule = get_schedule(@_);
416     open(STDOUT, ">" . $file) if(defined($file));
417     foreach my $row (@schedule) {
418         map {s/\n/-/g if defined; $_} %$row;
419         my $id=generate_id($row->{'id'});
420         $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
421         $row->{'days'} =~ s/^ //;
422         $row->{'instructor'} =~ s/^([^,]*), ([^,]*)/$2 $1/;
423         my $next = next_class(%$row);
424         $current = "";
425         #print "# $id\n";
426         $current .= $row->{'id'} . ",";
427         $current .= $row->{'title'} . ",";
428         $current .= '"' . $row->{'instructor'} . '",';
429         $next =~ /^(\d\d\d\d)(\d\d)(\d\d)$/;
430         $current .= $1 . "-". $2 ."-" . $3 . "\n";
431         print $current;
432     }
433 }
434
435 sub do_vcalendar_schedule {
436     $| = 1;
437     my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU");
438     my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
439     my @schedule = get_schedule(@_);
440     open(STDOUT, ">>" . $file) if(defined($file) && (! -d $file));
441     open(STDOUT, ">/dev/null") if(defined($file) && (-d $file));
442     print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
443     foreach my $row (@schedule) {
444         map { s/\n/-/g; $_} %$row;
445         $row->{'instructor'} =~ s/^([^,]*), ([^,]*)/$2 $1/;
446         $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
447         $row->{'days'} =~ s/^ //;
448         #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
449         my @day = (off_for_holidays(%$row),off_for_exams(%$row));
450         my $day = "";
451         if(exists($day[0])) {
452             $day = join(";", @day);
453             $day =~ s/\b(\d{8})\b/$1T000000/g;
454         }
455         my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
456         my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
457         $starttime =~ s/://;
458         $stoptime =~ s/://;
459         if(defined($file) && (-d $file)) {
460             open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
461             select FH;
462             print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
463         }
464         print "BEGIN:VEVENT\r\n";
465         print "SUMMARY:", $row->{'title'}, "\r\n";
466         print "DESCRIPTION:", $row->{'id'}, "\r\n";
467         print "LOCATION:", $row->{'location'}, "\r\n";
468         print "CATEGORIES:Education\r\n";
469         print "DTSTART:", $startdate."T".$starttime, "00\r\n";
470         print "DTEND:", $startdate."T".$stoptime, "00\r\n";
471         print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
472         print("EXDATE:$day\r\n") if($day);
473         print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{'name'}, "\r\n" if(defined($opts{'name'}));
474         print "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\r\n";
475         print "END:VEVENT\r\n";
476         if(defined($file) && (-d $file)) {
477             print "END:VCALENDAR\r\n";
478             close FH;
479             select STDOUT;
480         }
481     }
482     print "END:VCALENDAR\r\n";
483 }
484
485 sub do_html_schedule {
486     my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location");
487     my $shade = "dark";
488     my @schedule = get_schedule(@_);
489     print '<table id="schedule" cellpadding="3" cellspacing="0">'."\n<tr><th>";
490     print join("</th><th>",@showheaders);
491     print "</th></tr>\n";
492         foreach my $row (@schedule) {
493             map { s/\n/<br \/>/g; $_} %$row;
494             $row->{'time'} =~ s/-/-<wbr \/>/;
495             $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
496             print '<tr class="'.$shade.'">';
497             $shade = ($shade eq "dark"?"light":"dark");
498             print '<td class="idtitle">';
499             print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
500             print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
501             my $instructor = $row->{'instructor'};
502             $instructor =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
503             my $email = get_faculty_email($instructor);
504             #if($email) {
505                 #print '<td><a href="mailto:' . $email . '">'
506                 #. $row->{'instructor'} . "</a></td>";
507             # } else {
508                 print "<td>" . $row->{'instructor'} . "</td>";
509             #}
510             print "<td>" . $row->{'days'} . "</td>";
511             print "<td>" . $row->{'time'} . "</td>";
512             print "<td>" . $row->{'duration'} . "</td>";
513             print "<td>" . $row->{'location'} . "</td>";
514             print "</tr>\n";
515         }
516     print "</table>\n";
517 }
518
519 sub do_html_grades {
520     my @readheaders = ("Section ID", "Course Title", "Grade", "Earned  Hours", "Quality  Hours", "Quality  Points", "GPA");
521     @readheaders = ("Section", "Course Title", "FinalGrade", "Earned Hours", "Quality Hours", "Quality Points", "GPA") if($opts{'school'} eq 'tamuk');
522     my @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points");
523     my ($row, $lastrow);
524     my $shade = "dark";
525     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
526     my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
527     die $response->status_line unless $response->is_success;
528     #my @terms = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
529     my @terms = ();
530     my $year = Date::Calc->localtime->year;
531     for(my $y=$year-4;$y<=$year;$y++) { push @terms, ($y.2,$y."F",$y.3,$y.4,$y.1,$y."S"); }
532     foreach(@_ ? @_ : @terms) {
533         $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
534         die $response->status_line unless $response->is_success;
535         $te->parse($response->content);
536     }
537     print '<table id="grades" cellpadding="3" cellspacing="0">'."\n<tr>";
538     print '<th align="left" class="sectionid">', $showheaders[0];
539     print '</th><th align="left" class="coursetitle">', $showheaders[1];
540     print '</th><th align="center" class="grade">';
541     print join('</th><th align="center" class="right-number">',@showheaders[2 .. 5]);
542     print "</th></tr>\n";
543     foreach my $ts ($te->table_states) {
544         foreach my $row ($ts->rows) {
545             map { s/^\s+//; s/\xa0|\r//g; s/\n/<br \/>/g; $_} @$row;
546             $row->[1] = capitalize($row->[1]);
547             $row->[1] =~ s/&/&amp;/g;
548             $row->[0] =~ s/Cumulative through/Cumulative: through/;
549             $row->[0] =~ s/Cumulative:(.*) (\d{4}) (.*)/Cumulative:$1 $3 $2/;
550             $lastrow=$row;
551             next if $row->[0] =~ /:|Current Term/;
552             print '<tr class="'.$shade.'">';
553             $shade = ($shade eq "dark"?"light":"dark");
554             print '<td align="left" class="sectionid">', $$row[0], '</td>';
555             print '<td align="left" class="coursetitle">', $$row[1], '</td>';
556             print '<td align="center" class="grade">';
557             #print join('</td><td>', @$row[0 .. 1]);
558             print join('</td><td align="right" class="right-number">', @$row[2 .. 5]);
559             print "</td></tr>\n";
560         }
561     }
562     print '<tr class="cumulative '.$shade.'"><td id="cumulative" colspan="2">', capitalize($lastrow->[0]);
563     #shift @$lastrow; shift @$lastrow;
564     print '</td><td align="center" id="gpa" class="grade">';
565     print $$lastrow[6];
566     print '</td><td align="right" class="right-number">';
567     print join('</td><td align="right" class="right-number">', @$lastrow[3 .. 5]);
568     print "</td></tr>\n";
569     print "</table>\n";
570 }
571
572 sub do_transcripts {
573     $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN&tserve_trans_config=rtranscr.cfg&CareerReqNum=1");
574     foreach (split /\n/, $response->content) {
575         next unless s/^\&nbsp;|<pre>// || s/\&nbsp;/ /;
576         s/<\/?([Hh]\d|[Bb])>//g;
577         next if /<\/td>/;
578         print "$_\n";
579     }
580 }
581
582 if ($arg eq "-g") {
583     die "Grade support currently broken.\n";
584     do_html_grades(@ARGV);
585 } elsif ($arg eq "-s") {
586     do_html_schedule(@ARGV);
587 } elsif ($arg eq "-m") {
588     do_mhc_schedule(@ARGV);
589 } elsif ($arg eq "-c") {
590     do_csv_schedule(@ARGV);
591 } elsif ($arg eq "-v") {
592     do_vcalendar_schedule(@ARGV);
593 } elsif ($arg eq "-t") {
594     die "Transcript support currently broken.\n";
595     do_transcripts;
596 }
597
598 $ua->get("${url}twbkwbis.P_Logout"); # Logout