Middle initial removed from CSV; AM/PM in HTML; comments
[tpope-extra.git] / perl / schedproc
index 0dd2941026d074d1ed144fbbae243bae7743b5dd..9107e8353f548a3eb975c8c6e59ed49c0fb6c74b 100755 (executable)
@@ -3,8 +3,8 @@
 # -*- perl -*- vim: ft=perl sw=4 sts=4
 
 # Brief usage instructions:
-# Create a ~/.sct6rc that has SID=yourssn and PIN=yourpin
-# You'll need to change the url below if you go anywhere but TAMUK.
+# Create a ~/.schedprocrc that has schedule=/path/to/schedule.xml and
+# grades=/path/to/grades.xml.  HTTP URLs are acceptable
 
 use strict;
 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);
@@ -16,49 +16,6 @@ use vars qw(%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,
-    "Study Day"                 => "4/Thu/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} . "/.schedprocrc";
 if (($ARGV[0] || "") eq '-F') {
     shift;
@@ -105,69 +62,18 @@ sub next_class {
     @off = @{$class{'off'}} if ($class{'off'});
     for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
        next unless(grep($_ == Day_of_Week($today->date), @days));
-       next if(grep($_ == $today, @off));
+       next if(grep($_ == "$today", @off));
        return $today;
     }
     $today = Date::Calc->new(Date::Calc->gmtime->date);
     for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
        next unless(grep($_ == Day_of_Week($today->date), @days));
-       next if(grep($_ == $today, @off));
+       next if(grep($_ == "$today", @off));
        return $today;
     }
     return undef;
 }
 
-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/ +$//;
@@ -225,7 +131,7 @@ sub get_grades {
        die $response->status_line unless $response->is_success;
        $content = $response->content;
     } else {
-       open(F,$schedurl) || die "$!";
+       open(F,$gradeurl) || die "$!";
        $content = join ("", <F>);
        close F;
     }
@@ -234,7 +140,7 @@ sub get_grades {
 }
 
 sub do_html_grades {
-    my ($file, $grades);
+    my $grades;
     my @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points");
     #$file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
     $grades = get_grades(@_);
@@ -398,7 +304,9 @@ sub do_csv_schedule {
        #print "# $id\n";
        $current .= $row->{'id'} . ",";
        $current .= $row->{'title'} . ",";
-       $current .= '"' . $row->{'instructor'} . '",';
+       my $instructor = $row->{'instructor'};
+       $instructor =~ s/ [A-Z. ]* / /;
+       $current .= '"' . $instructor . '",';
        $next =~ /^(\d\d\d\d)(\d\d)(\d\d)$/;
        $current .= $1 . "-". $2 ."-" . $3 . "\n";
        print $current;
@@ -476,7 +384,6 @@ sub do_html_schedule {
     print "</th></tr>\n";
        foreach my $row (@schedule) {
            map { s/\n/<br \/>/g; $_} %$row;
-           #$row->{'time'} =~ s/-/-<wbr \/>/;
            $row->{'duration'} =~ s/-/<br \/>/g;
            $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
            print '<tr class="'.$shade.'">';
@@ -485,15 +392,20 @@ sub do_html_schedule {
            print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
            print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
            my $instructor = $row->{'instructor'};
+           #$instructor =~ s/ [A-Z. ]* / /;
            my $email = get_faculty_email($row->{'instructor'});
            #if($email) {
                #print '<td><a href="mailto:' . $email . '">'
                #. $row->{'instructor'} . "</a></td>";
            # } else {
-               print "<td>" . $row->{'instructor'} . "</td>";
+           print "<td>" . $instructor . "</td>";
            #}
            print "<td>" . $row->{'days'} . "</td>";
-           print "<td>" . $row->{'begin'}."-<wbr />".$row->{'end'} . "</td>";
+           my ($a,$b) = ($row->{'begin'}, $row->{'end'});
+           $b .= "AM";
+           $a=~s/(1[3-9]|2\d|00):(\d\d)/sprintf "%d:%02d",abs $1-12,$2/e;
+           $b=~s/(1[3-9]|2\d|00):(\d\d)AM/sprintf "%d:%02dPM",abs $1-12,$2/e;
+           print "<td>$a-<wbr />$b</td>";
            print "<td>" . $row->{'duration'} . "</td>";
            print "<td>" . $row->{'location'} . "</td>";
            print "</tr>\n";