From: Tim Pope Date: Fri, 3 Sep 2004 23:29:22 +0000 (+0000) Subject: Output individual MHC files if a directory is specified X-Git-Url: http://git.tpope.net/?p=tpope-extra.git;a=commitdiff_plain;h=b98d390505f263f1c52d33a11b25c4e19186676c Output individual MHC files if a directory is specified Centralized schedule_get --- diff --git a/perl/sctweb b/perl/sctweb index 487dd9c..43e8236 100755 --- a/perl/sctweb +++ b/perl/sctweb @@ -49,14 +49,15 @@ sub generate_id { } sub off_for_holidays { - $_[3] =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/; + 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, $Profiles->{'US-TX'}); my @holidays=(); my @days=(); - foreach(split(" ",$_[1])) { + foreach(split(" ",$class{days})) { $days[Decode_Day_of_Week($_)] = 1; } foreach ($year->search("")) { @@ -69,22 +70,24 @@ sub off_for_holidays { } sub off_for_exams { - $_[3] =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/; + 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(/ /, $_[1])); + my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'})); if($days eq "1 3") { - if($_[2] =~ /$beginning/) { + if($time =~ /$beginning/) { return $lastday-4; - } elsif($_[2] =~ /$ending/) { + } elsif($time =~ /$ending/) { return $lastday-6; } } elsif($days eq "2 4") { - if($_[2] =~ /$beginning/) { + if($time =~ /$beginning/) { return $lastday-3; - } elsif($_[2] =~ /$ending/) { + } elsif($time =~ /$ending/) { return $lastday-5; } } else { @@ -132,59 +135,70 @@ sub get_schedule_terms { return @terms; } -sub print_mhc_header { -my $header = "# Autogenerated by sctweb ".localtime()."\n"; -$header .= < [ @readheaders ] ); + my (@schedule, @terms, $classid, $begin, $end); + @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); + } + 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/(.*)
(.*)/capitalize($2)/eg; + $classid=$1; + $row->[2] =~ s/([A-Z]r?)$/$1./; + ($begin, $end) = split("-", $row->[7]); + if($row->[7] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) { + $begin =~ s/^(\d\d)/$1+12/e + if($1 < $2); + $end =~ s/^(\d\d)/$1+12/e; + $end =~ s/PM$//; + } else { $end =~ s/AM$//; } + $row->[9] =~ s/ON CAMPUS\n//g; + $row->[9] =~ s/\n/ /g; + push @schedule, { + id => $classid, + title => $row->[0], + session => $row->[1], + instructor => capitalize($row->[2]), + credits => $row->[3], + callnumber => $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], + }; + } + } + return @schedule; +} -X-SC-Subject: Christmas -X-SC-Category: Holiday -X-SC-Cond: 25 Dec +sub get_mhc_header { +return ( +"X-SC-Subject: New Years Day\nX-SC-Category: Holiday\nX-SC-Cond: 1 Jan\nX-SC-Record-Id: \n", +"X-SC-Subject: Martin Luther King, Jr. Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Jan\nX-SC-Duration: 19861102-\nX-SC-Record-Id: \n", +"X-SC-Subject: Presidents Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Feb\nX-SC-Duration: 19710515-\nX-SC-Record-Id: \n", +"X-SC-Subject: Memorial Day\nX-SC-Category: Holiday\nX-SC-Cond: Last Mon May\nX-SC-Duration: 19710531-\nX-SC-Record-Id: \n", +"X-SC-Subject: Independence Day\nX-SC-Category: Holiday\nX-SC-Cond: 4 Jul\nX-SC-Duration: 17760704-\nX-SC-Record-Id: \n", +"X-SC-Subject: Labor Day\nX-SC-Category: Holiday\nX-SC-Cond: 1st Mon Sep\nX-SC-Duration: 18840901-\nX-SC-Record-Id: \n", +"X-SC-Subject: Columbus Day\nX-SC-Category: Holiday\nX-SC-Cond: 2nd Mon Oct\nX-SC-Duration: 19711011-\nX-SC-Record-Id: \n", +"X-SC-Subject: Veterans Day\nX-SC-Category: Holiday\nX-SC-Cond: 11 Nov\nX-SC-Duration: 19261111-\nX-SC-Record-Id: \n", +"X-SC-Subject: Thanksgiving\nX-SC-Category: Holiday\nX-SC-Cond: 4th Thu Nov\nX-SC-Record-Id: \n", +"X-SC-Subject: Christmas\nX-SC-Category: Holiday\nX-SC-Cond: 25 Dec\nX-SC-Record-Id: \n", +); +} -EOF -; +sub print_mhc_header { +my $header .= join("\n", get_mhc_header) . "\n"; if(defined($_[0])) { open SCHEDULE, (">" . $_[0]); print SCHEDULE $header; @@ -196,159 +210,124 @@ EOF sub do_mhc_schedule { $| = 1; - my @readheaders = ("Section ID/Title", "Days", "Time", "Start/EndDates", "Site/Building/Room"); my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU"); - my $te = new HTML::TableExtract( headers => [ @readheaders ] ); - my (@terms, $row, $file); + my (@terms, $file, $current, @mhc, @schedule); $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/); - @terms = get_schedule_terms(); - print_mhc_header($file); - foreach(@_ || @terms) { - $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ($opts{cfg} || "rstusch" || "rgrades") . ".cfg&Term=".($_) ); - die $response->status_line unless $response->is_success; - $te->parse($response->content); + @schedule = get_schedule(@_); + @mhc = get_mhc_header; + #print_mhc_header($file); + foreach my $row (@schedule) { + 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"; + $current .= "X-SC-Location: " . $row->{'location'} . "\n"; + $current .= "X-SC-Category: School\n"; + $current .= "X-SC-Cond: " . $row->{'days'} . "\n"; + $current .= "X-SC-Time: ".$row->{'begin'}."-".$row->{'end'}."\n"; + $current .= "X-SC-Duration: " . $row->{'duration'} . "\n"; + $current .= "X-SC-Day: @day\n" if(exists $day[0]); + $current .= "X-SC-Alarm: 20 minutes\n"; + $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n"; + #print "\n$current"; + push @mhc, $current; } - #$response = $ua->get("http://localhost/~tpope/schedule.html" ); - #$te->parse($response->content); - open(STDOUT, ">>" . $file) if(defined($file)); - print "# MHC school schedule\n"; - foreach $ts ($te->table_states) { - foreach $row ($ts->rows) { - map { s/\xa0//g; s/\n/-/g; $_} @$row; - $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s; - $row->[0] =~ s/(.*)
//g; - my $classid=$1; - my $id=generate_id($classid); - $row->[0] = capitalize($row->[0]); - $row->[1] =~ s/([MTWRFS])/ $days{$1}/g; - $row->[1] =~ s/^ //; - if($row->[2] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) { - $row->[2] =~ s/^(\d\d)/$1+12/e - if($1 < $2); - $row->[2] =~ s/-(\d\d)/"-".($1+12)/e; - $row->[2] =~ s/PM$//; - } else { $row->[2] =~ s/AM$//; } - $row->[3] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g; - $row->[4] =~ s/ON CAMPUS-//g; - $row->[4] =~ s/-/ /g; - my @day = map { "!" . $_ } (off_for_holidays(@$row),off_for_exams(@$row)); - print "\n"; - print "# $id\n"; - print "X-SC-Subject: ", $row->[0], "\n"; - print "X-SC-Location: ", $row->[4], "\n"; - print "X-SC-Category: School\n"; - print "X-SC-Cond: ", $row->[1], "\n"; - print "X-SC-Time: ", $row->[2], "\n"; - print "X-SC-Duration: ", $row->[3], "\n"; - print("X-SC-Day: @day\n") if(exists $day[0]); - print "X-SC-Alarm: 20 minutes\n"; - print "X-SC-Record-Id: <$classid\@from.sctweb>\n" + if(defined($file) && (-d $file)) { + my @lines; + foreach my $name (<$file/[1-9]*>) { + next unless $name =~ /^$file\/[1-9][0-9]*$/; + open FH, $name; + @lines = ; + close FH; + foreach (@lines) { + unlink $name if /^X-SC-Record-Id: <.*\@from.sctweb>/; + } } + my $i=0; + foreach (@mhc) { + while(-f ++$i) {} + open FH, ">$file/$i" or die $!; + print FH $_; + close FH; + } + } else { + open(STDOUT, ">>" . $file) if(defined($file)); + print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n"; + print join("\n", @mhc); } } sub do_vcalendar_schedule { $| = 1; - my @readheaders = ("Section ID/Title", "Days", "Time", "Start/EndDates", "Site/Building/Room"); - #my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat"); my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU"); - my $te = new HTML::TableExtract( headers => [ @readheaders ] ); - my (@terms, $row, $file); - $file = shift if (defined $_[0] and $_[0] !~ /^\d/); - @terms = get_schedule_terms(); - foreach(@_ || @terms) { - $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ($opts{cfg} || "rstusch" || "rgrades") . ".cfg&Term=".($_) ); - die $response->status_line unless $response->is_success; - $te->parse($response->content); - } -# $response = $ua->get("http://localhost/~tpope/schedule.html"); -# $te->parse($response->content); - open(STDOUT, ">>" . $file) if(defined($file) && (-f $file)); + my $file = shift if (defined $_[0] and $_[0] !~ /^\d/); + my @schedule = get_schedule(@_); + open(STDOUT, ">>" . $file) if(defined($file) && (! -d $file)); open(STDOUT, ">/dev/null") if(defined($file) && (-d $file)); print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n"; - foreach $ts ($te->table_states) { - foreach $row ($ts->rows) { - map { s/\xa0//g; s/\n/-/g; $_} @$row; - $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s; - $row->[0] =~ s/(.*)
//g; - my $classid=$1; - $row->[0] = capitalize($row->[0]); - $row->[1] =~ s/([MTWRFS])/ $days{$1}/g; - $row->[1] =~ s/^ //; - if($row->[2] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) { - $row->[2] =~ s/^(\d\d)/$1+12/e - if($1 < $2); - $row->[2] =~ s/-(\d\d)/"-".($1+12)/e; - $row->[2] =~ s/PM$//; - } else { $row->[2] =~ s/AM$//; } - $row->[3] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g; - $row->[4] =~ s/ON CAMPUS-//g; - $row->[4] =~ s/-/ /g; - my @day = (off_for_holidays(@$row),off_for_exams(@$row)); - my $day = ""; - if(exists($day[0])) { - $day = join(";", @day); - $day =~ s/\b(\d{8})\b/$1T000000/g; - } - $row->[2] =~ s/://g; - my ($starttime, $stoptime)=split(/-/, $row->[2]); - my ($startdate, $stopdate)=split(/-/, $row->[3]); - if(defined($file) && (-d $file)) { - open FH, ">$file/$classid.vcs"; - select FH; - print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n"; - } - print "BEGIN:VEVENT\r\n"; - print "SUMMARY:", $row->[0], "\r\n"; - print "DESCRIPTION:", $classid, "\r\n"; - print "LOCATION:", $row->[4], "\r\n"; - print "CATEGORIES:Education\r\n"; - print "DTSTART:", $startdate."T".$starttime, "00\r\n"; - print "DTEND:", $startdate."T".$stoptime, "00\r\n"; - print "RRULE:W1 ", $row->[1] . " $stopdate", "T000000\r\n"; - - print("EXDATE:$day\r\n") if($day); - print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{NAME}, "\r\n" if(defined($opts{NAME})); - print "END:VEVENT\r\n"; - if(defined($file) && (-d $file)) { - print "END:VCALENDAR\r\n"; - close FH; - select STDOUT; - } + foreach my $row (@schedule) { + map { s/\n/-/g; $_} %$row; + $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 = ""; + if(exists($day[0])) { + $day = join(";", @day); + $day =~ s/\b(\d{8})\b/$1T000000/g; + } + my ($starttime, $stoptime)=($row->{'begin'}, $row->{'end'}); + my ($startdate, $stopdate)=split(/-/, $row->{'duration'}); + $starttime =~ s/://; + $stoptime =~ s/://; + if(defined($file) && (-d $file)) { + open FH, ">$file/" . $row->{'id'} . ".vcs" or die "$!"; + select FH; + print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n"; + } + print "BEGIN:VEVENT\r\n"; + print "SUMMARY:", $row->{'title'}, "\r\n"; + print "DESCRIPTION:", $row->{'id'}, "\r\n"; + print "LOCATION:", $row->{'location'}, "\r\n"; + print "CATEGORIES:Education\r\n"; + print "DTSTART:", $startdate."T".$starttime, "00\r\n"; + print "DTEND:", $startdate."T".$stoptime, "00\r\n"; + print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n"; + print("EXDATE:$day\r\n") if($day); + print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{NAME}, "\r\n" if(defined($opts{NAME})); + print "END:VEVENT\r\n"; + if(defined($file) && (-d $file)) { + print "END:VCALENDAR\r\n"; + close FH; + select STDOUT; } } print "END:VCALENDAR\r\n"; } 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", "Duration", "Location"); - my $te = new HTML::TableExtract( headers => [ @readheaders ] ); - my (@terms, $row); - @terms = get_schedule_terms(); - foreach(@ARGV || @terms) { - $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ($opts{cfg} || "rstusch" || "rgrades") . ".cfg&Term=".($_) ); - die $response->status_line unless $response->is_success; - $te->parse($response->content); - } + my @schedule = get_schedule(@_); print ''."\n\n"; - foreach $ts ($te->table_states) { - foreach $row ($ts->rows) { - map { s/\xa0//g; s/\n/
/g; $_} @$row; - $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s; - $row->[0] =~ s/
(.*)/"
" . capitalize($1)/eg; - $row->[1] = capitalize($row->[1]); - $row->[1] =~ s/([A-Z]r?)$/$1./; - $row->[3] =~ s/-/-/g; - $row->[5] =~ s/ON CAMPUS
//g; - $row->[5] =~ s/
/ /g; - print '\n"; + foreach my $row (@schedule) { + map { s/\n/
/g; $_} %$row; + $row->{'time'} =~ s/-/-/; + print ''; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + print "\n"; } - } print "
"; print join("",@showheaders); print "
'; - print join('', @$row); - print "
" . $row->{'id'} . "
" . $row->{'title'} . "
" . $row->{'instructor'} . "" . $row->{'days'} . "" . $row->{'time'} . "" . $row->{'duration'} . "" . $row->{'location'} . "
\n"; } @@ -400,9 +379,9 @@ my $arg = ""; $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/); if ($arg eq "-g") { - do_html_grades; + do_html_grades(@ARGV); } elsif ($arg eq "-s") { - do_html_schedule; + do_html_schedule(@ARGV); } elsif ($arg eq "-m") { do_mhc_schedule(@ARGV); } elsif ($arg eq "-v") {