Fixed typo
[tpope-extra.git] / perl / sctweb
1 #!/usr/bin/perl -w
2 # $Id$
3 # -*- perl -*- vim: ft=perl sw=4 sts=4
4
5 # Brief usage instructions:
6 # Create a ~/.sctwebrc that has SID=yourssn and PIN=yourpin
7 # You'll need to change the url below if you go anywhere but Northwest Vista.
8
9 use strict;
10 use LWP::UserAgent;
11 use HTML::TableExtract;
12 #use Time::Local;
13 use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Gmtime Mktime);
14 use Date::Calendar::Profiles qw($Profiles);
15 use Date::Calendar::Year;
16 use vars qw($ua %opts %faculty);
17
18 my ($response);
19
20 if (-r $ENV{HOME} . "/.sctwebrc") {
21     open CONFIG, $ENV{HOME} . "/.sctwebrc";
22     while(<CONFIG>) {
23         s/\#.*//;
24         next unless m/^([^=]*)=(.*)/;
25         $opts{$1}=$2;
26     }
27     close CONFIG;
28 }
29 my $domain = $opts{'domain'} || "sctweb.accd.edu";
30 my $url = "https://$domain/ia-bin4/tsrvweb.exe?WID=W&tserve_tip_read_destroy&tserve_host_code=4&tserve_tiphost_code=0";
31 $url = $opts{'url'} if(defined($opts{'url'}));
32
33 $ua = LWP::UserAgent->new;
34 $ua->timeout(10);
35 $ua->env_proxy;
36 $ua->cookie_jar( {} );
37 $ua->get("$url&tserve_tip_write=||WID&tserve_transconfig=astulog.cfg") or die "$!";
38 $response = $ua->post("$url&tserve_trans_config=astulog.cfg", { tserve_tip_write => "%7C%7CWID%7CSID%7CPIN", SID => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
39
40 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ("regterm.cfg"||"rgrdterm.cfg")); # Valid terms
41 die $response->status_line unless $response->is_success;
42 my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
43 die "Site down. Try again later.\n" unless (@terms);
44
45 sub generate_id {
46     my ($section, $number) = split("-", shift);
47     my $id = 0;
48     foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
49     $id=10000*$id+$number;
50     return $id;
51 }
52
53 sub next_class {
54     my %class = @_;
55     $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
56     my $days = Delta_Days($1,$2,$3,$4,$5,$6);
57     my $firstday = Date::Calc->new($1,$2,$3);
58     my $lastday = Date::Calc->new($4,$5,$6);
59     my $today = Date::Calc->today;
60     my @days=();
61     foreach(split(" ",$class{'days'})) {
62         push @days, Decode_Day_of_Week($_);
63     }
64     my @off = (off_for_holidays(%class),off_for_exams(%class));
65     for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
66         next unless(grep($_ == Day_of_Week($today->date), @days));
67         next if(grep($_ == $today, @off));
68         return $today;
69     }
70     $today = Date::Calc->today;
71     for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
72         next unless(grep($_ == Day_of_Week($today->date), @days));
73         next if(grep($_ == $today, @off));
74         return $today;
75     }
76     return undef;
77 }
78
79 sub off_for_holidays {
80     my %class = @_;
81     $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
82     my $days = Delta_Days($1,$2,$3,$4,$5,$6);
83     my $firstday = Date::Calc->new($1,$2,$3);
84     my $lastday = Date::Calc->new($4,$5,$6);
85     my $year = Date::Calendar::Year->new($1, $Profiles->{'US-TX'});
86     my @holidays=();
87     my @days=();
88     foreach(split(" ",$class{days})) {
89         $days[Decode_Day_of_Week($_)] = 1;
90     }
91     foreach ($year->search("")) {
92         next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
93         if (defined($days[Day_of_Week($_->date)])) {
94             push @holidays, $_;
95         }
96     }
97     return wantarray ? @holidays : "@holidays";
98 }
99
100 sub off_for_exams {
101     my %class = @_;
102     my $time = $class{'begin'} . "-" . $class{'end'};
103     $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
104     my $lastday = Date::Calc->new($1,$2,$3);
105     return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
106     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)";
107     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)";
108     my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
109     if($days eq "1 3") {
110         if($time =~ /$beginning/) {
111             return $lastday-4;
112         } elsif($time =~ /$ending/) {
113             return $lastday-6;
114         }
115     } elsif($days eq "2 4") {
116         if($time =~ /$beginning/) {
117             return $lastday-3;
118         } elsif($time =~ /$ending/) {
119             return $lastday-5;
120         }
121     } else {
122         return wantarray ? () : "";
123     }
124 }
125
126 sub capitalize {
127     local $_ = shift;
128     s/ +$//;
129     s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
130     s/\b(I)(i*)\b/$1\U$2/g;
131     s/\bUs\b/US/g;
132     s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
133     s/\b(Ma?c)([a-z])/$1\u$2/g;
134     s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
135     s/\bThru\b/Through/g;
136     s/\bAcct\b/Accounting/g;
137     s/\bAmer\b/American/g;
138     s/\bChem\b/Chemistry/g;
139     s/\bComp\b/Composition/g;
140     s/\bFed\b/Federal/g;
141     s/\bGen\b/General/g;
142     #s/\bGovt\b/Government/g;
143     s/\bLit\b/Literature/g;
144     s/\bPrin\b/Principles/g;
145     return $_;
146 }
147
148 sub get_schedule_terms {
149     my @terms;
150     my @localtime=localtime();
151     if ($localtime[4]<3) { # Through Mar 31
152         @terms=((1900+$localtime[5])*10+2);
153     } elsif ($localtime[4]<5) { # Through May 31
154         @terms=((1900+$localtime[5])*10+2,(1900+$localtime[5])*10+3);
155     } elsif ($localtime[4]<7) { # through July 31
156         @terms=((1900+$localtime[5])*10+3,(1900+$localtime[5])*10+4);
157     } elsif ($localtime[4]<8) { # through Aug 31
158         @terms=((1900+$localtime[5])*10+4,(1900+$localtime[5])*10+1);
159     } elsif ($localtime[4]<10) { # through Oct 31
160         @terms=((1900+$localtime[5])*10+1);
161     } else {
162         @terms=((1900+$localtime[5])*10+1,(1901+$localtime[5])*10+2);
163     }
164     return @terms;
165 }
166
167 sub get_schedule {
168     my @readheaders = ("Section ID/Title", "Session", "Instructor", "Credits", "CallNumber","GradeType", "Days", "Time", "Start/EndDates", "Site/Building/Room", "Message");
169     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
170     my (@schedule, @terms, $classid, $begin, $end);
171     @terms = get_schedule_terms();
172     foreach(@_ || @terms) {
173         $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rstusch.cfg&Term=".($_) );
174         die $response->status_line unless $response->is_success;
175         $te->parse($response->content);
176     }
177     foreach my $ts ($te->table_states) {
178         foreach my $row ($ts->rows) {
179             map { s/\xa0//g; $_} @$row;
180             $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
181             $row->[0] =~ s/(.*) <BR>(.*)/capitalize($2)/eg;
182             $classid=$1;
183             $row->[2] = capitalize($row->[2]);
184             $row->[2] =~ s/([A-Z]r?)$/$1./;
185             ($begin, $end) = split("-", $row->[7]);
186             if($row->[7] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
187                 $begin =~ s/^(\d\d)/$1+12/e
188                 if($1 < $2);
189                 $end =~ s/^(\d\d)/$1+12/e;
190                 $end =~ s/PM$//;
191             } else { $end =~ s/AM$//; }
192             $row->[8] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
193             $row->[9] =~ s/ON CAMPUS\n//g;
194             $row->[9] =~ s/\n/ /g;
195             push @schedule, {
196                 id => $classid,
197                 title => $row->[0],
198                 session => $row->[1],
199                 instructor => $row->[2],
200                 credits => $row->[3],
201                 callnumber => $row->[4],
202                 gradetype => $row->[5],
203                 days => $row->[6],
204                 'time' => $row->[7],
205                 begin => $begin,
206                 end => $end || undef,
207                 duration => $row->[8],
208                 location => $row->[9],
209                 message => $row->[10],
210             };
211         }
212     }
213     return @schedule;
214 }
215
216 sub get_faculty_email {
217     my ($name, $school, $email);
218     if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
219         open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
220         while($_ = <INS>) {
221             chomp;
222             m/"([^"]*)",([^,]*),([^,]*)/;
223             ($name, $school, $email) = ($1, $2, $3);
224             $name =~ s/^([^,]*), ([^,]*)(.*)$/$2 $1$3/;
225             $name =~ s/ [A-Z]\.//g;
226             $name = lc $name;
227             $name =~ s/\W//g;
228             $faculty{$name} = $email;
229         }
230     }
231     $name = shift;
232     $name =~ s/ [A-Z]r?\.//g;
233     $name = lc $name;
234     $name =~ s/\W//g;
235     return $faculty{$name};
236 }
237
238 sub get_mhc_header {
239 return (
240 "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",
241 "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",
242 "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",
243 "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",
244 "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",
245 "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",
246 "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",
247 "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",
248 "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",
249 "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",
250 );
251 }
252
253 sub do_mhc_schedule {
254     $| = 1;
255     my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
256     my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
257     my ($file, $current, @mhc, @schedule);
258     $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
259     @schedule = get_schedule(@_);
260     @mhc = get_mhc_header;
261     if(defined($file) && (-d $file)) {
262         my @mhc2;
263         foreach (@mhc) {
264             $_ =~ s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/;
265             $_ =~ s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/;
266             $_ =~ s/X-SC-Duration: (\d\d\d\d)(\d\d)(\d\d)-/"X-SC-Duration: $1$2$3-\nDate: $3 " . $mon[$2-1] . " 2037 12:00:00 +0000"/e;
267             push @mhc2, $_;
268         }
269         @mhc = @mhc2;
270     }
271     foreach my $row (@schedule) {
272         map {s/\n/-/g if defined; $_} %$row;
273         my $id=generate_id($row->{'id'});
274         $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
275         $row->{'days'} =~ s/^ //;
276         #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
277         my @day = map { "!" . $_ } (off_for_holidays(%$row),off_for_exams(%$row));
278         $current = "";
279         #print "# $id\n";
280         $current .= "X-SC-Subject: " . $row->{'title'} . "\n";
281         $current .= "X-SC-Location: " . $row->{'location'} . "\n";
282         $current .= "X-SC-Category: School\n";
283         $current .= "X-SC-Cond: " . $row->{'days'} . "\n";
284         $current .= "X-SC-Time: ".$row->{'begin'}."-".$row->{'end'}."\n";
285         $current .= "X-SC-Duration: " . $row->{'duration'} . "\n";
286         $current .= "X-SC-Day: @day\n" if(exists $day[0]);
287         $current .= "X-SC-Alarm: 15 minutes\n";
288         $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n";
289         if(defined($file) && (-d $file)) {
290             $row->{'instructor'} =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
291             my $email = get_faculty_email $row->{'instructor'};
292             $row->{'instructor'} = '"' . $row->{'instructor'} . '" <'. ($email || "$1\@from.sctweb") . ">";
293             $row->{'duration'} =~ /^(\d\d\d\d)(\d\d)(\d\d)-\d{8}$/;
294             $row->{'begin'} =~ /^(\d\d):(\d\d)$/;
295             my $next = next_class(%$row);
296             my @date = Gmtime(Mktime($next->date,$1,$2,0));
297             $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];
298             $current .= "Subject: " . $row->{'title'} . "\n";
299             $current .= "From: " . $row->{'instructor'} . "\n";
300         }
301         #print "\n$current";
302         push @mhc, $current;
303     }
304     if(defined($file) && (-d $file)) {
305         my @lines;
306         foreach my $name (<$file/[1-9]*>) {
307             next unless $name =~ /^$file\/[1-9][0-9]*$/;
308             open FH, $name;
309             @lines = <FH>;
310             close FH;
311             foreach (@lines) {
312                 unlink $name if /^X-SC-Record-Id: <.*\@from.sctweb>/;
313             }
314         }
315         my $i=0;
316         foreach (@mhc) {
317             while(-f ++$i) {}
318             open FH, ">$file/$i" or die $!;
319             print FH $_;
320             close FH;
321         }
322     } else {
323         open(STDOUT, ">" . $file) if(defined($file));
324         print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n";
325         print join("\n", @mhc);
326     }
327 }
328
329 sub do_csv_schedule {
330     $| = 1;
331     my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
332     my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.);
333     my (@terms, $file, $current, @mhc, @schedule);
334     $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
335     @schedule = get_schedule(@_);
336     open(STDOUT, ">" . $file) if(defined($file));
337     foreach my $row (@schedule) {
338         map {s/\n/-/g if defined; $_} %$row;
339         my $id=generate_id($row->{'id'});
340         $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
341         $row->{'days'} =~ s/^ //;
342         $row->{'instructor'} =~ s/^([^,]*), ([^,]*)/$2 $1/;
343         my $next = next_class(%$row);
344         $current = "";
345         #print "# $id\n";
346         $current .= $row->{'id'} . ",";
347         $current .= $row->{'title'} . ",";
348         $current .= '"' . $row->{'instructor'} . '",';
349         $next =~ /^(\d\d\d\d)(\d\d)(\d\d)$/;
350         $current .= $1 . "-". $2 ."-" . $3 . "\n";
351         print $current;
352     }
353 }
354
355 sub do_vcalendar_schedule {
356     $| = 1;
357     my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU");
358     my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
359     my @schedule = get_schedule(@_);
360     open(STDOUT, ">>" . $file) if(defined($file) && (! -d $file));
361     open(STDOUT, ">/dev/null") if(defined($file) && (-d $file));
362     print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
363     foreach my $row (@schedule) {
364         map { s/\n/-/g; $_} %$row;
365         $row->{'instructor'} =~ s/^([^,]*), ([^,]*)/$2 $1/;
366         $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
367         $row->{'days'} =~ s/^ //;
368         #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
369         my @day = (off_for_holidays(%$row),off_for_exams(%$row));
370         my $day = "";
371         if(exists($day[0])) {
372             $day = join(";", @day);
373             $day =~ s/\b(\d{8})\b/$1T000000/g;
374         }
375         my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'});
376         my ($startdate, $stopdate)=split(/-/, $row->{'duration'});
377         $starttime =~ s/://;
378         $stoptime =~ s/://;
379         if(defined($file) && (-d $file)) {
380             open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!";
381             select FH;
382             print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n";
383         }
384         print "BEGIN:VEVENT\r\n";
385         print "SUMMARY:", $row->{'title'}, "\r\n";
386         print "DESCRIPTION:", $row->{'id'}, "\r\n";
387         print "LOCATION:", $row->{'location'}, "\r\n";
388         print "CATEGORIES:Education\r\n";
389         print "DTSTART:", $startdate."T".$starttime, "00\r\n";
390         print "DTEND:", $startdate."T".$stoptime, "00\r\n";
391         print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
392         print("EXDATE:$day\r\n") if($day);
393         print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{NAME}, "\r\n" if(defined($opts{NAME}));
394         print "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\n";
395         print "END:VEVENT\r\n";
396         if(defined($file) && (-d $file)) {
397             print "END:VCALENDAR\r\n";
398             close FH;
399             select STDOUT;
400         }
401     }
402     print "END:VCALENDAR\r\n";
403 }
404
405 sub do_html_schedule {
406     my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location");
407     my @schedule = get_schedule(@_);
408     print '<table cellpadding="3" cellspacing="0">'."\n<tr><th>";
409     print join("</th><th>",@showheaders);
410     print "</th></tr>\n";
411         foreach my $row (@schedule) {
412             map { s/\n/<br \/>/g; $_} %$row;
413             $row->{'time'} =~ s/-/-<wbr \/>/;
414             $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
415             print '<tr>';
416             print "<td>" . $row->{'id'} . "<br />" . $row->{'title'} . "</td>";
417             my $instructor = $row->{'instructor'};
418             $instructor =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
419             my $email = get_faculty_email($instructor);
420             #if($email) {
421                 #print '<td><a href="mailto:' . $email . '">'
422                 #. $row->{'instructor'} . "</a></td>";
423             # } else {
424                 print "<td>" . $row->{'instructor'} . "</td>";
425             #}
426             print "<td>" . $row->{'days'} . "</td>";
427             print "<td>" . $row->{'time'} . "</td>";
428             print "<td>" . $row->{'duration'} . "</td>";
429             print "<td>" . $row->{'location'} . "</td>";
430             print "</tr>\n";
431         }
432     print "</table>\n";
433 }
434
435 sub do_html_grades {
436     my @readheaders = ("Section ID", "Course Title", "Grade", "Earned  Hours", "Quality  Hours", "Quality  Points", "GPA");
437     my @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points", "GPA");
438     my ($row, $lastrow);
439     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
440     my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
441     die $response->status_line unless $response->is_success;
442     my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
443     foreach(@_ || reverse @terms) {
444         $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
445         die $response->status_line unless $response->is_success;
446         $te->parse($response->content);
447     }
448     print '<table cellpadding="3" cellspacing="0">'."\n<tr><th>";
449     print join("</th><th>",@showheaders);
450     print "</th></tr>\n";
451     foreach my $ts ($te->table_states) {
452         foreach my $row ($ts->rows) {
453             map { s/\xa0//g; s/\n/<br \/>/g; $_} @$row;
454             $row->[1] = capitalize($row->[1]);
455             $lastrow=$row;
456             next if $row->[0] =~ /:/;
457             print '<tr><td>';
458             print join('</td><td>', @$row);
459             print "</td></tr>\n";
460         }
461     }
462     print '<tr><td colspan="2">', capitalize($lastrow->[0]);
463     shift @$lastrow; shift @$lastrow;
464     print "</td><td>";
465     print join('</td><td>', @$lastrow);
466     print "</td></tr>\n";
467     print "</table>\n";
468 }
469
470 sub do_faculty {
471     my @readheaders = ("Name", "College", "Email");
472     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
473     foreach(qw(a b c d e f g h i j k l m n o p q r s t u v w x y z)) {
474         my $response = $ua->get("http://$domain/fs/".$_."dir.htm");
475         die $response->status_line unless $response->is_success;
476         $te->parse($response->content);
477     }
478     foreach my $ts ($te->table_states) {
479         foreach my $row ($ts->rows) {
480             map { s/[\r\n]//g; $_} @$row;
481             $row->[0] = capitalize($row->[0]);
482             $row->[0] =~ s/([A-Z]r?)$/$1./;
483             print '"'.$row->[0].'",',$row->[1].',', $row->[2], "\n";
484         }
485     }
486 }
487
488 sub do_transcripts {
489     $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN&tserve_trans_config=rtranscr.cfg&CareerReqNum=1");
490     foreach (split /\n/, $response->content) {
491         next unless s/^\&nbsp;|<pre>// || s/\&nbsp;/ /;
492         s/<\/?[Hh]\d>//g;
493         print "$_\n";
494     }
495 }
496
497 my $arg = "";
498 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
499
500 if ($arg eq "-g") {
501     do_html_grades(@ARGV);
502 } elsif ($arg eq "-s") {
503     do_html_schedule(@ARGV);
504 } elsif ($arg eq "-m") {
505     do_mhc_schedule(@ARGV);
506 } elsif ($arg eq "-c") {
507     do_csv_schedule(@ARGV);
508 } elsif ($arg eq "-v") {
509     do_vcalendar_schedule(@ARGV);
510 } elsif ($arg eq "-t") {
511     do_transcripts;
512 } elsif ($arg eq "-f") {
513     do_faculty;
514 }
515
516 $ua->get("$url&tserve_trans_config=alogout.cfg"); # Logout