Added "off" days to XML support
authorTim Pope <code@tpope.net>
Tue, 16 Aug 2005 21:05:55 +0000 (21:05 +0000)
committerTim Pope <code@tpope.net>
Tue, 16 Aug 2005 21:05:55 +0000 (21:05 +0000)
perl/sct6

index ffb1cbbc7031f977070430a865652f5ac98c8603..b2f5bff3ed7cde50eb26bd2b715268024202e7b1 100755 (executable)
--- a/perl/sct6
+++ b/perl/sct6
@@ -12,8 +12,7 @@ use Date::Calendar::Profiles qw($Profiles);
 use Date::Calendar::Year;
 use HTML::TableExtract;
 use LWP::UserAgent;
-use IO::File;
-use XML::Writer;
+use XML::Simple;
 use vars qw($ua %opts %faculty);
 
 my ($response);
@@ -114,7 +113,7 @@ sub next_class {
     foreach(split(" ",$class{'days'})) {
        push @days, Decode_Day_of_Week($_);
     }
-    my @off = (off_for_holidays(%class),off_for_exams(%class));
+    my @off = @{$row->{'off'}};
     for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
        next unless(grep($_ == Day_of_Week($today->date), @days));
        next if(grep($_ == $today, @off));
@@ -131,6 +130,10 @@ sub next_class {
 
 sub off_for_holidays {
     my %class = @_;
+    my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
+    my $d = $class{'days'};
+    $d =~ s/([MTWRFS])/ $days{$1}/g;
+    $d =~ s/^ //;
     $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);
@@ -138,7 +141,7 @@ sub off_for_holidays {
     my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
     my @holidays=();
     my @days=();
-    foreach(split(" ",$class{days})) {
+    foreach(split(" ",$d)) {
        $days[Decode_Day_of_Week($_)] = 1;
     }
     foreach ($year->search("")) {
@@ -274,7 +277,7 @@ sub get_schedule {
            $row->[4] =~ s/ - /-/;
            $row->[3] =~ s/(ON|MAIN) CAMPUS\n|Palo Alto Building \d* //ig;
            $row->[3] =~ s/\n/ /g;
-           push @schedule, {
+           my %class = (
                id => $classid,
                title => capitalize($title),
                instructor => $row->[6],
@@ -284,7 +287,10 @@ sub get_schedule {
                end => $end || undef,
                duration => $row->[4],
                location => $row->[3],
-           };
+           );
+           my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
+           $class{'off'} = [ @off ] if (@off);
+           push @schedule, \%class;
        }
     }
     return @schedule;
@@ -346,12 +352,13 @@ sub do_mhc_schedule {
        @mhc = @mhc2;
     }
     foreach my $row (@schedule) {
+       #my @day = map { "!" . $_ } (off_for_holidays(%$row),off_for_exams(%$row));
+       my @day = @{$row->{'off'}};
        map {s/\n/-/g if defined; $_} %$row;
        my $id=generate_id($row->{'id'});
        $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
        $row->{'days'} =~ s/^ //;
        #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
-       my @day = map { "!" . $_ } (off_for_holidays(%$row),off_for_exams(%$row));
        $current = "";
        #print "# $id\n";
        $current .= "X-SC-Subject: " . $row->{'title'} . "\n";
@@ -443,7 +450,7 @@ sub do_vcalendar_schedule {
        $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g;
        $row->{'days'} =~ s/^ //;
        #$row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
-       my @day = (off_for_holidays(%$row),off_for_exams(%$row));
+       my @day = @{$row->{'off'}};
        my $day = "";
        if(exists($day[0])) {
            $day = join(";", @day);
@@ -481,25 +488,15 @@ sub do_vcalendar_schedule {
 
 sub do_xml_schedule {
     my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
-    my ($output, $writer);
+    my $schedule = { class => [ get_schedule(@_) ] };
+    my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
     if($file) {
-       $output = new IO::File(">$file");
-       $writer = new XML::Writer(OUTPUT => $output, DATA_MODE => 1);
+       open FH, ">$file";
+       print FH $xml;
+       close FH;
     } else {
-       $writer = new XML::Writer(DATA_MODE => 1);
-    }
-    my @schedule = get_schedule(@_);
-    $writer->startTag("schedule");
-    foreach my $class (@schedule) {
-       $writer->startTag("class");
-       foreach my $key (sort keys %$class) {
-           $writer->dataElement($key, $class->{$key});
-       }
-       $writer->endTag("class");
+       print $xml;
     }
-    $writer->endTag("schedule");
-    $writer->end();
-    close $output if $file;
 }
 
 sub do_html_schedule {