From: Tim Pope Date: Tue, 7 Sep 2004 05:39:35 +0000 (+0000) Subject: CSV support X-Git-Url: http://git.tpope.net/?p=tpope-extra.git;a=commitdiff_plain;h=db56a28ec9f782b005dadc831cf88938674581bb CSV support --- diff --git a/perl/sctweb b/perl/sctweb index 309c3e3..274252d 100755 --- a/perl/sctweb +++ b/perl/sctweb @@ -49,6 +49,32 @@ sub generate_id { return $id; } +sub next_class { + 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 $today = Date::Calc->today; + my @days=(); + foreach(split(" ",$class{'days'})) { + push @days, Decode_Day_of_Week($_); + } + my @off = (off_for_holidays(%class),off_for_exams(%class)); + for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) { + next unless(grep($_ == Day_of_Week($today->date), @days)); + next if(grep($_ == $today, @off)); + return $today; + } + $today = Date::Calc->today; + for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) { + next unless(grep($_ == Day_of_Week($today->date), @days)); + 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)/; @@ -161,6 +187,7 @@ sub get_schedule { $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 CAMPUS\n//g; $row->[9] =~ s/\n/ /g; push @schedule, { @@ -212,7 +239,7 @@ sub do_mhc_schedule { foreach (@mhc) { $_ =~ s/X-SC-Subject: ([^\n]*)/X-SC-Subject: $1\nSubject: $1/; $_ =~ s/X-SC-Category: ([^\n]*)/X-SC-Category: $1\nFrom: $1/; - $_ =~ s/X-SC-Duration: (\d\d\d\d)(\d\d)(\d\d)-/"X-SC-Duration: $1$2$3-\nDate: $3 " . $mon[$2-1] . " 2037 00:00:00 +0000"/e; + $_ =~ s/X-SC-Duration: (\d\d\d\d)(\d\d)(\d\d)-/"X-SC-Duration: $1$2$3-\nDate: $3 " . $mon[$2-1] . " 2037 12:00:00 +0000"/e; push @mhc2, $_; } @mhc = @mhc2; @@ -222,7 +249,7 @@ sub do_mhc_schedule { 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; + #$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"; @@ -237,12 +264,10 @@ sub do_mhc_schedule { $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n"; if(defined($file) && (-d $file)) { $row->{'instructor'} =~ s/(.*), (.*)/"$2 $1" <$1\@from.sctweb>/; - $row->{'days'} =~ s/ .*$//; $row->{'duration'} =~ /^(\d\d\d\d)(\d\d)(\d\d)-\d{8}$/; - my @date = Add_Delta_Days(Monday_of_Week(Week_of_Year - ($1,$2,$3)),Decode_Day_of_Week($row->{'days'})-1); $row->{'begin'} =~ /^(\d\d):(\d\d)$/; - @date = Gmtime(Mktime(@date,$1,$2,0)); + my $next = next_class(%$row); + my @date = Gmtime(Mktime($next->date,$1,$2,0)); $current .= sprintf "Date: %s, %2d %s %4d %02d:%02d:00 +0000\n", Day_of_Week_Abbreviation($date[7]), $date[2], $mon[$date[1]-1], $date[0], $date[3], $date[4], $date[5]; $current .= "Subject: " . $row->{'title'} . "\n"; $current .= "From: " . $row->{'instructor'} . "\n"; @@ -269,12 +294,38 @@ sub do_mhc_schedule { close FH; } } else { - open(STDOUT, ">>" . $file) if(defined($file)); + open(STDOUT, ">" . $file) if(defined($file)); print "# MHC school schedule\n# Autogenerated by sctweb ".localtime()."\n\n"; print join("\n", @mhc); } } +sub do_csv_schedule { + $| = 1; + my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU"); + my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.); + my (@terms, $file, $current, @mhc, @schedule); + @schedule = get_schedule(@_); + $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/); + open(STDOUT, ">" . $file) if(defined($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->{'instructor'} =~ s/(.*), (.*)/$2 $1/; + my $next = next_class(%$row); + $current = ""; + #print "# $id\n"; + $current .= $row->{'id'} . ","; + $current .= $row->{'title'} . ","; + $current .= $row->{'instructor'} . ","; + $next =~ /^(\d\d\d\d)(\d\d)(\d\d)$/; + $current .= $1 . "-". $2 ."-" . $3 . "\n"; + print $current; + } +} + sub do_vcalendar_schedule { $| = 1; my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU"); @@ -285,9 +336,10 @@ sub do_vcalendar_schedule { print "BEGIN:VCALENDAR\r\nVERSION:1.0\r\n"; foreach my $row (@schedule) { map { s/\n/-/g; $_} %$row; + $row->{'instructor'} =~ s/(.*), (.*)/$2 $1/; $row->{'days'} =~ s/([MTWRFS])/ $days{$1}/g; $row->{'days'} =~ s/^ //; - $row->{'duration'} =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g; + #$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])) { @@ -313,6 +365,7 @@ sub do_vcalendar_schedule { 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 "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, "\n"; print "END:VEVENT\r\n"; if(defined($file) && (-d $file)) { print "END:VCALENDAR\r\n"; @@ -332,6 +385,7 @@ sub do_html_schedule { foreach my $row (@schedule) { map { s/\n/
/g; $_} %$row; $row->{'time'} =~ s/-/-/; + $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g; print ''; print "" . $row->{'id'} . "
" . $row->{'title'} . ""; print "" . $row->{'instructor'} . ""; @@ -397,6 +451,8 @@ if ($arg eq "-g") { do_html_schedule(@ARGV); } elsif ($arg eq "-m") { do_mhc_schedule(@ARGV); +} elsif ($arg eq "-c") { + do_csv_schedule(@ARGV); } elsif ($arg eq "-v") { do_vcalendar_schedule(@ARGV); } elsif ($arg eq "-t") {