Assorted away-tpope updates
[tpope-extra.git] / perl / sctweb
index 7fe412b1a138a3f9bccf0baf24160f4661504192..5d82c6c786de6e11a746ae3bd9292137e049a287 100755 (executable)
@@ -6,15 +6,75 @@
 # Create a ~/.sctwebrc that has SID=yourssn and PIN=yourpin
 # You'll need to change the url below if you go anywhere but Northwest Vista.
 
+# Update: This tool now only outputs in XML.  For other formats, convert
+# with schedproc
+
 use strict;
-use LWP::UserAgent;
+#use Time::Local;
+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);
+use Date::Calendar::Profiles qw($Profiles);
+use Date::Calendar::Year;
 use HTML::TableExtract;
-use vars qw($ua %opts);
-my ($ts, $row, $response);
-my $url="https://sctweb.accd.edu/ia-bin4/tsrvweb.exe?WID=W&tserve_tip_read_destroy&tserve_host_code=4&tserve_tiphost_code=0";
+use LWP::UserAgent;
+use XML::Simple;
+use vars qw($ua %opts %faculty);
+
+my ($response);
+
+$opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
+    "Martin Luther King Day"    => "3/Mon/Jan",
+    "Good Friday"               => "-2",
+    "Spring Break Monday"      => \&Spring_Break,
+    "Spring Break Tuesday"     => \&Spring_Break,
+    "Spring Break Wednesday"   => \&Spring_Break,
+    "Spring Break Thursday"    => \&Spring_Break,
+    "Spring Break Friday"      => \&Spring_Break,
+    "Spring Break Saturday"    => \&Spring_Break,
+    "Fiesta Holiday"            => "4/Fri/Apr",
+    "Memorial Day"              => "5/Mon/May",
+    "Independence Day"          => \&US_Independence,
+    "Labor Day"                 => \&US_Labor,
+    "Columbus Day"              => "2/Mon/Oct",
+    "Thanksgiving Day"          => "4/Thu/Nov",
+    "Thanksgiving Friday"       => \&Thanksgiving_Friday,
+};
+
+sub Spring_Break {
+    my($year,$label) = @_;
+    $label =~ s/^Spring Break //;
+    return( Add_Delta_Days(
+           Nth_Weekday_of_Month_Year($year,1,1,1),
+           7*(11-1) # This is for the 11th Monday of the year
+           +Decode_Day_of_Week($label)-1) );
+}
+
+sub Thanksgiving_Friday {
+    my($year,$label) = @_;
+    return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
+}
+sub US_Independence # Fourth of July
+{
+    my($year,$label) = @_;
+    return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
+}
+sub US_Labor # First Monday after the first Sunday in September
+{
+    my($year,$label) = @_;
+    return( Add_Delta_Days(
+        Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
+}
+
+my $config = $ENV{HOME} . "/.sctwebrc";
+if ($ARGV[0] eq '-F') {
+    shift;
+    $config = shift;
+}
 
-if (-r $ENV{HOME} . "/.sctwebrc") {
-    open CONFIG, $ENV{HOME} . "/.sctwebrc";
+my $arg = "";
+$arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
+
+if (-r $config) {
+    open CONFIG, $config;
     while(<CONFIG>) {
        s/\#.*//;
        next unless m/^([^=]*)=(.*)/;
@@ -22,6 +82,9 @@ if (-r $ENV{HOME} . "/.sctwebrc") {
     }
     close CONFIG;
 }
+my $domain = $opts{'domain'} || "sctweb.accd.edu";
+my $url = "https://$domain/ia-bin4/tsrvweb.exe?WID=W&tserve_tip_read_destroy&tserve_host_code=4&tserve_tiphost_code=0";
+$url = $opts{'url'} if(defined($opts{'url'}));
 
 $ua = LWP::UserAgent->new;
 $ua->timeout(10);
@@ -29,96 +92,335 @@ $ua->env_proxy;
 $ua->cookie_jar( {} );
 $ua->get("$url&tserve_tip_write=||WID&tserve_transconfig=astulog.cfg") or die "$!";
 $response = $ua->post("$url&tserve_trans_config=astulog.cfg", { tserve_tip_write => "%7C%7CWID%7CSID%7CPIN", SID => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
-#print $response->content;
-#die "Site down.  Try again later.\n" if(length($response->content)<256);
 
 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ("regterm.cfg"||"rgrdterm.cfg")); # Valid terms
 die $response->status_line unless $response->is_success;
-my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
+my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content)); # "
+if(@terms) {
+    $opts{school} = 'accd';
+} else {
+    $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=aterm.cfg"); # Valid terms
+    die $response->status_line unless $response->is_success;
+    @terms = grep {s/&amp;Term=(\w*)$/$1/} (split( /\r\n/, $response->content));
+    $opts{school} = 'tamuk';
+}
 die "Site down. Try again later.\n" unless (@terms);
 
-sub do_html_schedule {
-    my @readheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Start/EndDates", "Site/Building/Room");
-    my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Start/End\nDates", "Location");
-    my $te = new HTML::TableExtract( headers => [ @readheaders ] );
-    my (@terms, $row);
+sub off_for_holidays {
+    my %class = @_;
+    $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
+    my $days = Delta_Days($1,$2,$3,$4,$5,$6);
+    my $firstday = Date::Calc->new($1,$2,$3);
+    my $lastday = Date::Calc->new($4,$5,$6);
+    my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
+    my @holidays=();
+    my @days=();
+    foreach(split(" ",$class{days})) {
+       $days[Decode_Day_of_Week($_)] = 1;
+    }
+    foreach ($year->search("")) {
+       my $good=1;
+       #foreach my $x ($year->labels($_)) {
+       #    $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
+       #}
+       next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
+       push @holidays, $_
+           if (defined($days[Day_of_Week($_->date)]) && $good > 0);
+    }
+    return wantarray ? @holidays : "@holidays";
+}
+
+sub off_for_exams {
+    my %class = @_;
+    my $time = $class{'begin'} . "-" . $class{'end'};
+    $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
+    my $lastday = Date::Calc->new($1,$2,$3);
+    return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
+    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)";
+    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)";
+    my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
+    if($days eq "1 3") {
+       if($time =~ /$beginning/) {
+           return $lastday-4;
+       } elsif($time =~ /$ending/) {
+           return $lastday-6;
+       }
+    } elsif($days eq "2 4") {
+       if($time =~ /$beginning/) {
+           return $lastday-3;
+       } elsif($time =~ /$ending/) {
+           return $lastday-5;
+       }
+    } elsif($days eq "5") {
+       return $lastday-2;
+    }
+    return wantarray ? () : "";
+}
+
+sub capitalize {
+    local $_ = shift || "";
+    s/ +$//;
+    s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
+    s/\b(I)(i*)\b/$1\U$2/g;
+    s/\bUs\b/US/g;
+    s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
+    s/\b(Mc)([a-z])/$1\u$2/g;
+    s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
+    s/\bThru\b/Through/g;
+    s/\bAcct\b/Accounting/g;
+    s/\bAmer\b/American/g;
+    s/\bChem\b/Chemistry/g;
+    s/\bComp\b/Composition/g;
+    s/\bFed\b/Federal/g;
+    s/\bGen\b/General/g;
+    s/\bIntro\b/Introduction/g;
+    s/\bPrgm\b/Programming/g;
+    s/\bOp Sys\b/Operating System/g;
+    #s/\bGovt\b/Government/g;
+    s/\bLit\b/Literature/g;
+    s/\bPrin\b/Principles/g;
+    s/\bBus\b/Business/g;
+    return $_;
+}
+
+sub get_schedule_terms {
+    my (@sterms, @a);
+    if($opts{school} eq 'tamuk') {
+       @a=('F','S','1','2');
+    } else {
+       @a=('1','2','3','4');
+    }
     my @localtime=localtime();
     if ($localtime[4]<3) { # Through Mar 31
-       @terms=((1900+$localtime[5])*10+2);
+       @sterms=((1900+$localtime[5]).$a[1]);
     } elsif ($localtime[4]<5) { # Through May 31
-       @terms=((1900+$localtime[5])*10+2,(1900+$localtime[5])*10+3);
+       @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
     } elsif ($localtime[4]<7) { # through July 31
-       @terms=((1900+$localtime[5])*10+3,(1900+$localtime[5])*10+4);
+       @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
     } elsif ($localtime[4]<8) { # through Aug 31
-       @terms=((1900+$localtime[5])*10+4,(1900+$localtime[5])*10+1);
+       @sterms=((1900+$localtime[5]).$a[3],(1900+$localtime[5]).$a[0]);
     } elsif ($localtime[4]<10) { # through Oct 31
-       @terms=((1900+$localtime[5])*10+1);
+       @sterms=((1900+$localtime[5]).$a[0]);
     } else {
-       @terms=((1900+$localtime[5])*10+1,(1901+$localtime[5])*10+2);
+       @sterms=((1900+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
     }
-    foreach(@ARGV || @terms) {
-       $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ($opts{cfg} || "rstusch" || "rgrades") . ".cfg&Term=".($_) );
+    return @sterms;
+}
+
+sub get_schedule {
+    my @readheaders = ("Section ID/Title", "Session", "Instructor", "Credits", "CallNumber","GradeType", "Days", "Time", "Start/EndDates", "Site/Building/Room", "Message");
+    my $te = new HTML::TableExtract( headers => [ @readheaders ] );
+    @readheaders = ("Course", "Days\xa0TimeLocation", "Session and Dates", "Instructor", "Credit", "CallNumber", "GradeType", "Message");
+    my $tf = new HTML::TableExtract( headers => [ @readheaders ] );
+    my (@schedule, @terms, $classid, $begin, $end, $times, $days, $session);
+    @terms = get_schedule_terms();
+    foreach (@_ ? @_ : @terms) {
+       $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rstusch.cfg&Term=".($_) );
        die $response->status_line unless $response->is_success;
        $te->parse($response->content);
+       $tf->parse($response->content);
+       #print $response->content;
     }
-    print '<table cellpadding="3" cellspacing="0">'."\n<tr><th>";
-    print join("</th><th>",@showheaders);
-    print "</th></tr>\n";
-    foreach $ts ($te->table_states) {
-       foreach $row ($ts->rows) {
-           map { s/\xa0//g; s/\n/<br \/>/g; $_} @$row;
+    foreach my $ts ($te->table_states) {
+       foreach my $row ($ts->rows) {
+           map { s/\xa0//g; $_} @$row;
            $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
-           $row->[0] =~ s/ <BR>/<br \/>/g;
-           $row->[5] =~ s/ON CAMPUS<br \/>//g;
-           $row->[5] =~ s/<br \/>/ /g;
-           print '<tr><td>';
-           print join('</td><td>', @$row);
-           print "</td></tr>\n";
+           $row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
+           $classid=$1;
+           $row->[2] = capitalize($row->[2]);
+           $row->[2] =~ s/([A-Z]r?)$/$1./;
+           $row->[2] =~ s/^([^,]*), ([^,.]*|[^,]{4,})/$2 $1/;
+           ($begin, $end) = split("-", $row->[7]);
+           if($row->[7] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
+               if($1 < $2 && $2 != 12) {
+                   $begin =~ s/^(\d\d)/$1+12/e;
+               }
+               if($2 != 12) {
+                   $end =~ s/^(\d\d)/$1+12/e;
+               }
+               $end =~ s/PM$//;
+           } else { $end =~ s/AM$//; }
+           $row->[8] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
+           $row->[9] =~ s/(ON|MAIN) CAMPUS\n//ig;
+           $row->[9] =~ s/\n/ /g;
+           my %class = (
+               id => $classid,
+               title => $row->[0],
+               #session => $row->[1],
+               instructor => $row->[2],
+               credits => $row->[3],
+               crn => $row->[4],
+               #gradetype => $row->[5],
+               days => $row->[6],
+               #'time' => $row->[7],
+               begin => $begin,
+               end => $end || undef,
+               duration => $row->[8],
+               location => $row->[9],
+               #message => $row->[10],
+           );
+           my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
+           $class{'off'} = [ @off ] if (@off);
+           push @schedule, \%class;
        }
     }
-    print "</table>\n";
+    foreach my $ts ($tf->table_states) {
+       foreach my $row ($ts->rows) {
+           next if ($row->[0] =~ /Total Enrolled Hours/);
+           $row->[1] =~ s/\xa0/ /g;
+           map { s/\xa0//g; $_} @$row;
+           map { s/^\r?\n? *//g; $_} @$row;
+           $row->[0] =~ s/.*launchWebCT\("([^"]*)"[^)]*\).*>\s*//s;
+           $classid=$1;
+           $row->[0] =~ s/(.*)/capitalize($1)/eg;
+           $row->[1] =~ s/^([MTWRFS]*) *([0-9:-]*[AP]M)\s*//;
+           ($days, $times) = ($1, $2);
+           ($begin, $end) = split("-", $times);
+           if($times =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
+               if($1 < $2 && $2 != 12) {
+                   $begin =~ s/^(\d\d)/$1+12/e;
+               }
+               if($2 != 12) {
+                   $end =~ s/^(\d\d)/$1+12/e;
+               }
+               $end =~ s/PM$//;
+           } else { $end =~ s/AM$//; }
+           $row->[2] =~ s/(.*\S)\s*//;
+           $session = $1;
+           $row->[2] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
+           $row->[2] =~ s/ *to */-/g;
+           $row->[3] = capitalize($row->[3]);
+           $row->[3] =~ s/([A-Z]r?)$/$1./;
+           $row->[3] =~ s/^([^,]*), ([^,.]{3}[^,]*)/$2 $1/;
+           my %class = (
+               id => $classid,
+               title => $row->[0],
+               #session => $session,
+               instructor => $row->[3],
+               credits => $row->[4],
+               crn => $row->[5],
+               #gradetype => $row->[6],
+               days => $days,
+               #'time' => $times,
+               begin => $begin,
+               end => $end || undef,
+               duration => $row->[2],
+               location => $row->[1],
+               #message => $row->[7],
+           );
+           my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
+           $class{'off'} = [ @off ] if (@off);
+           push @schedule, \%class;
+       }
+    }
+    return @schedule;
+}
+
+sub do_xml_schedule {
+    my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
+    my $schedule = { class => [ get_schedule(@_) ] };
+    my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
+    if($file) {
+       open FH, ">$file";
+       print FH $xml;
+       close FH;
+    } else {
+       print $xml;
+    }
 }
 
-sub do_html_grades {
-    my @readheaders = ("Section ID", "Course Title", "Grade", "Attempted  Hours", "Earned  Hours", "Quality  Hours", "Quality  Points", "GPA");
-    my @showheaders = ("Section ID", "Course Title", "Grade", "Attempted<br>Hours", "Earned<br>Hours", "Quality<br>Hours", "Quality<br>Points", "GPA");
+sub do_xml_grades {
+    my @readheaders = ("Section ID", "Course Title", "Grade", "Earned  Hours", "Quality  Hours", "Quality  Points", "GPA");
+    @readheaders = ("Section", "Course Title", "FinalGrade", "Earned Hours", "Quality Hours", "Quality Points", "GPA") if($opts{'school'} eq 'tamuk');
     my ($row, $lastrow);
     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
     my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
     die $response->status_line unless $response->is_success;
-    my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
-    foreach(@ARGV || reverse @terms) {
+    #my @terms = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
+    my @grades = ();
+    my @terms = ();
+    my $year = Date::Calc->localtime->year;
+    for(my $y=$year-4;$y<=$year;$y++) { push @terms, ($y.2,$y."F",$y.3,$y.4,$y.1,$y."S"); }
+    foreach(@_ ? @_ : @terms) {
        $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
        die $response->status_line unless $response->is_success;
        $te->parse($response->content);
     }
-    print '<table cellpadding="3" cellspacing="0">'."\n<tr><th>";
-    print join("</th><th>",@showheaders);
-    print "</th></tr>\n";
-    foreach $ts ($te->table_states) {
-       foreach $row ($ts->rows) {
-           map { s/\xa0//g; s/\n/<br \/>/g; $_} @$row;
-           #$row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
-           #$row->[0] =~ s/ <BR>/<br \/>/s;
+    foreach my $ts ($te->table_states) {
+       foreach my $row ($ts->rows) {
+           map { s/^\s+//; s/\xa0|\r//g; s/\n/<br \/>/g; $_} @$row;
+           $row->[1] = capitalize($row->[1]);
+           $row->[1] =~ s/&/&amp;/g;
+           $row->[0] =~ s/Cumulative through/Cumulative: through/;
+           $row->[0] =~ s/Cumulative:(.*) (\d{4}) (.*)/Cumulative:$1 $3 $2/;
            $lastrow=$row;
-           next if $row->[0] =~ /:/;
-           print '<tr><td>';
-           print join('</td><td>', @$row);
-           print "</td></tr>\n";
+           next if $row->[0] =~ /:|Current Term/;
+           push @grades, {
+               id => $row->[0],
+               title => $row->[1],
+               grade => $row->[2],
+               earned => $row->[3],
+               hours => $row->[4],
+               points => $row->[5]
+           };
        }
     }
-    print '<tr><td colspan="3">', $lastrow->[0];
-    shift @$lastrow; shift @$lastrow;
-    print join('</td><td>', @$lastrow);
+    $lastrow->[0] =~ s/Cumulative:? *through *//i;
+    my $cumulative = { term => capitalize($lastrow->[0]),
+                      gpa => $lastrow->[6],
+                      earned => $lastrow->[3],
+                      hours => $lastrow->[4],
+                      points => $lastrow->[5]
+                  };
+    my $xml = XMLout({class => [@grades], cumulative => $cumulative}, NoAttr => 1, RootName => 'grades');
+    print $xml;
+    return;
+    print '<tr class="cumulative"><td id="cumulative" colspan="2">', capitalize($lastrow->[0]);
+    #shift @$lastrow; shift @$lastrow;
+    print '</td><td align="center" id="gpa" class="grade">';
+    print $$lastrow[6];
+    print '</td><td align="right" class="right-number">';
+    print join('</td><td align="right" class="right-number">', @$lastrow[3 .. 5]);
     print "</td></tr>\n";
     print "</table>\n";
 }
 
-if(defined($ARGV[0]) && $ARGV[0] eq "-g") {
-    shift;
-    do_html_grades;
-} else {
-    do_html_schedule;
+sub do_faculty {
+    my @readheaders = ("Name", "College", "Email");
+    my $te = new HTML::TableExtract( headers => [ @readheaders ] );
+    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)) {
+       my $response = $ua->get("http://$domain/fs/".$_."dir.htm");
+       die $response->status_line unless $response->is_success;
+       $te->parse($response->content);
+    }
+    foreach my $ts ($te->table_states) {
+       foreach my $row ($ts->rows) {
+           map { s/[\r\n]//g; $_} @$row;
+           $row->[0] = capitalize($row->[0]);
+           $row->[0] =~ s/([A-Z]r?)$/$1./;
+           print '"'.$row->[0].'",',$row->[2].',', $row->[1], "\n";
+       }
+    }
+}
+
+sub do_transcripts {
+    $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN&tserve_trans_config=rtranscr.cfg&CareerReqNum=1");
+    foreach (split /\n/, $response->content) {
+       next unless s/^\&nbsp;|<pre>// || s/\&nbsp;/ /;
+       s/<\/?([Hh]\d|[Bb])>//g;
+       next if /<\/td>/;
+       print "$_\n";
+    }
+}
+
+if ($arg eq "-g") {
+    do_xml_grades(@ARGV);
+} elsif ($arg eq "-t") {
+    do_transcripts;
+} elsif ($arg eq "-f") {
+    do_faculty;
+} elsif ($arg eq "-x" || $arg eq "-s" || 1) {
+    do_xml_schedule(@ARGV);
 }
 
 $ua->get("$url&tserve_trans_config=alogout.cfg"); # Logout