Stripped out non-XML formats in preparation for new schedproc script
[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 # Update: This tool now only outputs in XML.  For other formats, convert
10 # with schedproc
11
12 use strict;
13 #use Time::Local;
14 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 Nth_Weekday_of_Month_Year Gmtime Mktime);
15 use Date::Calendar::Profiles qw($Profiles);
16 use Date::Calendar::Year;
17 use HTML::TableExtract;
18 use LWP::UserAgent;
19 use XML::Simple;
20 use vars qw($ua %opts %faculty);
21
22 my ($response);
23
24 $opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
25     "Martin Luther King Day"    => "3/Mon/Jan",
26     "Good Friday"               => "-2",
27     "Spring Break Monday"       => \&Spring_Break,
28     "Spring Break Tuesday"      => \&Spring_Break,
29     "Spring Break Wednesday"    => \&Spring_Break,
30     "Spring Break Thursday"     => \&Spring_Break,
31     "Spring Break Friday"       => \&Spring_Break,
32     "Spring Break Saturday"     => \&Spring_Break,
33     "Fiesta Holiday"            => "4/Fri/Apr",
34     "Memorial Day"              => "5/Mon/May",
35     "Independence Day"          => \&US_Independence,
36     "Labor Day"                 => \&US_Labor,
37     "Columbus Day"              => "2/Mon/Oct",
38     "Thanksgiving Day"          => "4/Thu/Nov",
39     "Thanksgiving Friday"       => \&Thanksgiving_Friday,
40 };
41
42 sub Spring_Break {
43     my($year,$label) = @_;
44     $label =~ s/^Spring Break //;
45     return( Add_Delta_Days(
46             Nth_Weekday_of_Month_Year($year,1,1,1),
47             7*(11-1) # This is for the 11th Monday of the year
48             +Decode_Day_of_Week($label)-1) );
49 }
50
51 sub Thanksgiving_Friday {
52     my($year,$label) = @_;
53     return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
54 }
55 sub US_Independence # Fourth of July
56 {
57     my($year,$label) = @_;
58     return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
59 }
60 sub US_Labor # First Monday after the first Sunday in September
61 {
62     my($year,$label) = @_;
63     return( Add_Delta_Days(
64         Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
65 }
66
67 my $config = $ENV{HOME} . "/.sctwebrc";
68 if ($ARGV[0] eq '-F') {
69     shift;
70     $config = shift;
71 }
72
73 my $arg = "";
74 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
75
76 if (-r $config) {
77     open CONFIG, $config;
78     while(<CONFIG>) {
79         s/\#.*//;
80         next unless m/^([^=]*)=(.*)/;
81         $opts{$1}=$2;
82     }
83     close CONFIG;
84 }
85 my $domain = $opts{'domain'} || "sctweb.accd.edu";
86 my $url = "https://$domain/ia-bin4/tsrvweb.exe?WID=W&tserve_tip_read_destroy&tserve_host_code=4&tserve_tiphost_code=0";
87 $url = $opts{'url'} if(defined($opts{'url'}));
88
89 $ua = LWP::UserAgent->new;
90 $ua->timeout(10);
91 $ua->env_proxy;
92 $ua->cookie_jar( {} );
93 $ua->get("$url&tserve_tip_write=||WID&tserve_transconfig=astulog.cfg") or die "$!";
94 $response = $ua->post("$url&tserve_trans_config=astulog.cfg", { tserve_tip_write => "%7C%7CWID%7CSID%7CPIN", SID => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
95
96 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ("regterm.cfg"||"rgrdterm.cfg")); # Valid terms
97 die $response->status_line unless $response->is_success;
98 my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content)); # "
99 if(@terms) {
100     $opts{school} = 'accd';
101 } else {
102     $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=aterm.cfg"); # Valid terms
103     die $response->status_line unless $response->is_success;
104     @terms = grep {s/&amp;Term=(\w*)$/$1/} (split( /\r\n/, $response->content));
105     $opts{school} = 'tamuk';
106 }
107 die "Site down. Try again later.\n" unless (@terms);
108
109 sub off_for_holidays {
110     my %class = @_;
111     $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
112     my $days = Delta_Days($1,$2,$3,$4,$5,$6);
113     my $firstday = Date::Calc->new($1,$2,$3);
114     my $lastday = Date::Calc->new($4,$5,$6);
115     my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
116     my @holidays=();
117     my @days=();
118     foreach(split(" ",$class{days})) {
119         $days[Decode_Day_of_Week($_)] = 1;
120     }
121     foreach ($year->search("")) {
122         my $good=1;
123         #foreach my $x ($year->labels($_)) {
124         #    $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
125         #}
126         next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
127         push @holidays, $_
128             if (defined($days[Day_of_Week($_->date)]) && $good > 0);
129     }
130     return wantarray ? @holidays : "@holidays";
131 }
132
133 sub off_for_exams {
134     my %class = @_;
135     my $time = $class{'begin'} . "-" . $class{'end'};
136     $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
137     my $lastday = Date::Calc->new($1,$2,$3);
138     return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
139     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)";
140     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)";
141     my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
142     if($days eq "1 3") {
143         if($time =~ /$beginning/) {
144             return $lastday-4;
145         } elsif($time =~ /$ending/) {
146             return $lastday-6;
147         }
148     } elsif($days eq "2 4") {
149         if($time =~ /$beginning/) {
150             return $lastday-3;
151         } elsif($time =~ /$ending/) {
152             return $lastday-5;
153         }
154     } elsif($days eq "5") {
155         return $lastday-2;
156     }
157     return wantarray ? () : "";
158 }
159
160 sub capitalize {
161     local $_ = shift || "";
162     s/ +$//;
163     s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
164     s/\b(I)(i*)\b/$1\U$2/g;
165     s/\bUs\b/US/g;
166     s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
167     s/\b(Mc)([a-z])/$1\u$2/g;
168     s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
169     s/\bThru\b/Through/g;
170     s/\bAcct\b/Accounting/g;
171     s/\bAmer\b/American/g;
172     s/\bChem\b/Chemistry/g;
173     s/\bComp\b/Composition/g;
174     s/\bFed\b/Federal/g;
175     s/\bGen\b/General/g;
176     s/\bIntro\b/Introduction/g;
177     s/\bPrgm\b/Programming/g;
178     s/\bOp Sys\b/Operating System/g;
179     #s/\bGovt\b/Government/g;
180     s/\bLit\b/Literature/g;
181     s/\bPrin\b/Principles/g;
182     s/\bBus\b/Business/g;
183     return $_;
184 }
185
186 sub get_schedule_terms {
187     my (@sterms, @a);
188     if($opts{school} eq 'tamuk') {
189         @a=('F','S','1','2');
190     } else {
191         @a=('1','2','3','4');
192     }
193     my @localtime=localtime();
194     if ($localtime[4]<3) { # Through Mar 31
195         @sterms=((1900+$localtime[5]).$a[1]);
196     } elsif ($localtime[4]<5) { # Through May 31
197         @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
198     } elsif ($localtime[4]<7) { # through July 31
199         @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
200     } elsif ($localtime[4]<8) { # through Aug 31
201         @sterms=((1900+$localtime[5]).$a[3],(1900+$localtime[5]).$a[0]);
202     } elsif ($localtime[4]<10) { # through Oct 31
203         @sterms=((1900+$localtime[5]).$a[0]);
204     } else {
205         @sterms=((1900+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
206     }
207     return @sterms;
208 }
209
210 sub get_schedule {
211     my @readheaders = ("Section ID/Title", "Session", "Instructor", "Credits", "CallNumber","GradeType", "Days", "Time", "Start/EndDates", "Site/Building/Room", "Message");
212     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
213     @readheaders = ("Course", "Days\xa0TimeLocation", "Session and Dates", "Instructor", "Credit", "CallNumber", "GradeType", "Message");
214     my $tf = new HTML::TableExtract( headers => [ @readheaders ] );
215     my (@schedule, @terms, $classid, $begin, $end, $times, $days, $session);
216     @terms = get_schedule_terms();
217     foreach (@_ ? @_ : @terms) {
218         $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rstusch.cfg&Term=".($_) );
219         die $response->status_line unless $response->is_success;
220         $te->parse($response->content);
221         $tf->parse($response->content);
222         #print $response->content;
223     }
224     foreach my $ts ($te->table_states) {
225         foreach my $row ($ts->rows) {
226             map { s/\xa0//g; $_} @$row;
227             $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
228             $row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
229             $classid=$1;
230             $row->[2] = capitalize($row->[2]);
231             $row->[2] =~ s/([A-Z]r?)$/$1./;
232             $row->[2] =~ s/^([^,]*), ([^,.]*|[^,]{4,})/$2 $1/;
233             ($begin, $end) = split("-", $row->[7]);
234             if($row->[7] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
235                 if($1 < $2 && $2 != 12) {
236                     $begin =~ s/^(\d\d)/$1+12/e;
237                 }
238                 if($2 != 12) {
239                     $end =~ s/^(\d\d)/$1+12/e;
240                 }
241                 $end =~ s/PM$//;
242             } else { $end =~ s/AM$//; }
243             $row->[8] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
244             $row->[9] =~ s/(ON|MAIN) CAMPUS\n//ig;
245             $row->[9] =~ s/\n/ /g;
246             my %class = (
247                 id => $classid,
248                 title => $row->[0],
249                 #session => $row->[1],
250                 instructor => $row->[2],
251                 credits => $row->[3],
252                 crn => $row->[4],
253                 #gradetype => $row->[5],
254                 days => $row->[6],
255                 #'time' => $row->[7],
256                 begin => $begin,
257                 end => $end || undef,
258                 duration => $row->[8],
259                 location => $row->[9],
260                 #message => $row->[10],
261             );
262             my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
263             $class{'off'} = [ @off ] if (@off);
264             push @schedule, \%class;
265         }
266     }
267     foreach my $ts ($tf->table_states) {
268         foreach my $row ($ts->rows) {
269             next if ($row->[0] =~ /Total Enrolled Hours/);
270             $row->[1] =~ s/\xa0/ /g;
271             map { s/\xa0//g; $_} @$row;
272             map { s/^\r?\n? *//g; $_} @$row;
273             $row->[0] =~ s/.*launchWebCT\("([^"]*)"[^)]*\).*>\s*//s;
274             $classid=$1;
275             $row->[0] =~ s/(.*)/capitalize($1)/eg;
276             $row->[1] =~ s/^([MTWRFS]*) *([0-9:-]*[AP]M)\s*//;
277             ($days, $times) = ($1, $2);
278             ($begin, $end) = split("-", $times);
279             if($times =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
280                 if($1 < $2 && $2 != 12) {
281                     $begin =~ s/^(\d\d)/$1+12/e;
282                 }
283                 if($2 != 12) {
284                     $end =~ s/^(\d\d)/$1+12/e;
285                 }
286                 $end =~ s/PM$//;
287             } else { $end =~ s/AM$//; }
288             $row->[2] =~ s/(.*\S)\s*//;
289             $session = $1;
290             $row->[2] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
291             $row->[2] =~ s/ *to */-/g;
292             $row->[3] = capitalize($row->[3]);
293             $row->[3] =~ s/([A-Z]r?)$/$1./;
294             $row->[3] =~ s/^([^,]*), ([^,.]{3}[^,]*)/$2 $1/;
295             my %class = (
296                 id => $classid,
297                 title => $row->[0],
298                 #session => $session,
299                 instructor => $row->[3],
300                 credits => $row->[4],
301                 crn => $row->[5],
302                 #gradetype => $row->[6],
303                 days => $days,
304                 #'time' => $times,
305                 begin => $begin,
306                 end => $end || undef,
307                 duration => $row->[2],
308                 location => $row->[1],
309                 #message => $row->[7],
310             );
311             my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
312             $class{'off'} = [ @off ] if (@off);
313             push @schedule, \%class;
314         }
315     }
316     return @schedule;
317 }
318
319 sub do_xml_schedule {
320     my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
321     my $schedule = { class => [ get_schedule(@_) ] };
322     my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
323     if($file) {
324         open FH, ">$file";
325         print FH $xml;
326         close FH;
327     } else {
328         print $xml;
329     }
330 }
331
332 sub do_xml_grades {
333     my @readheaders = ("Section ID", "Course Title", "Grade", "Earned  Hours", "Quality  Hours", "Quality  Points", "GPA");
334     @readheaders = ("Section", "Course Title", "FinalGrade", "Earned Hours", "Quality Hours", "Quality Points", "GPA") if($opts{'school'} eq 'tamuk');
335     my ($row, $lastrow);
336     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
337     my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
338     die $response->status_line unless $response->is_success;
339     #my @terms = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
340     my @grades = ();
341     my @terms = ();
342     my $year = Date::Calc->localtime->year;
343     for(my $y=$year-4;$y<=$year;$y++) { push @terms, ($y.2,$y."F",$y.3,$y.4,$y.1,$y."S"); }
344     foreach(@_ ? @_ : @terms) {
345         $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
346         die $response->status_line unless $response->is_success;
347         $te->parse($response->content);
348     }
349     foreach my $ts ($te->table_states) {
350         foreach my $row ($ts->rows) {
351             map { s/^\s+//; s/\xa0|\r//g; s/\n/<br \/>/g; $_} @$row;
352             $row->[1] = capitalize($row->[1]);
353             $row->[1] =~ s/&/&amp;/g;
354             $row->[0] =~ s/Cumulative through/Cumulative: through/;
355             $row->[0] =~ s/Cumulative:(.*) (\d{4}) (.*)/Cumulative:$1 $3 $2/;
356             $lastrow=$row;
357             next if $row->[0] =~ /:|Current Term/;
358             push @grades, {
359                 id => $row->[0],
360                 title => $row->[1],
361                 grade => $row->[2],
362                 earned => $row->[3],
363                 hours => $row->[4],
364                 points => $row->[5]
365             };
366         }
367     }
368     $lastrow->[0] =~ s/Cumulative:? *through *//i;
369     my $cumulative = { term => capitalize($lastrow->[0]),
370                        gpa => $lastrow->[6],
371                        earned => $lastrow->[3],
372                        hours => $lastrow->[4],
373                        points => $lastrow->[5]
374                    };
375     my $xml = XMLout({class => [@grades], cumulative => $cumulative}, NoAttr => 1, RootName => 'grades');
376     print $xml;
377     return;
378     print '<tr class="cumulative"><td id="cumulative" colspan="2">', capitalize($lastrow->[0]);
379     #shift @$lastrow; shift @$lastrow;
380     print '</td><td align="center" id="gpa" class="grade">';
381     print $$lastrow[6];
382     print '</td><td align="right" class="right-number">';
383     print join('</td><td align="right" class="right-number">', @$lastrow[3 .. 5]);
384     print "</td></tr>\n";
385     print "</table>\n";
386 }
387
388 sub do_faculty {
389     my @readheaders = ("Name", "College", "Email");
390     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
391     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)) {
392         my $response = $ua->get("http://$domain/fs/".$_."dir.htm");
393         die $response->status_line unless $response->is_success;
394         $te->parse($response->content);
395     }
396     foreach my $ts ($te->table_states) {
397         foreach my $row ($ts->rows) {
398             map { s/[\r\n]//g; $_} @$row;
399             $row->[0] = capitalize($row->[0]);
400             $row->[0] =~ s/([A-Z]r?)$/$1./;
401             print '"'.$row->[0].'",',$row->[2].',', $row->[1], "\n";
402         }
403     }
404 }
405
406 sub do_transcripts {
407     $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN&tserve_trans_config=rtranscr.cfg&CareerReqNum=1");
408     foreach (split /\n/, $response->content) {
409         next unless s/^\&nbsp;|<pre>// || s/\&nbsp;/ /;
410         s/<\/?([Hh]\d|[Bb])>//g;
411         next if /<\/td>/;
412         print "$_\n";
413     }
414 }
415
416 if ($arg eq "-g") {
417     do_xml_grades(@ARGV);
418 } elsif ($arg eq "-t") {
419     do_transcripts;
420 } elsif ($arg eq "-f") {
421     do_faculty;
422 } elsif ($arg eq "-x" || $arg eq "-s" || 1) {
423     do_xml_schedule(@ARGV);
424 }
425
426 $ua->get("$url&tserve_trans_config=alogout.cfg"); # Logout