# Create a ~/.sct6rc that has SID=yourssn and PIN=yourpin
# You'll need to change the url below if you go anywhere but TAMUK.
+# Update: This tool now only outputs in XML. For other formats, convert
+# with schedproc
+
use strict;
-use LWP::UserAgent;
-use HTML::TableExtract;
-#use Time::Local;
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);
use Date::Calendar::Profiles qw($Profiles);
use Date::Calendar::Year;
+use HTML::TableExtract;
+use LWP::UserAgent;
+use XML::Simple;
use vars qw($ua %opts %faculty);
my ($response);
$response = $ua->get("${url}bwskflib.P_SelDefTerm"); # Valid terms
die $response->status_line unless $response->is_success;
-my @terms = grep {s/^<option value="([^"]*)">.*/$1/i} (split( /\r?\n/, $response->content));
+my @terms = grep {s/^<option value="([^"]*)">.*/$1/i} (split( /\r?\n/, $response->content)); # "
die "Site down. Try again later.\n" unless (@terms);
sub generate_id {
foreach(split(" ",$class{'days'})) {
push @days, Decode_Day_of_Week($_);
}
- my @off = (off_for_holidays(%class),off_for_exams(%class));
+ my @off = @{$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));
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);
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("")) {
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/\bTba\b/TBA/g;
- s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
+ s/\b(Tcp\/Ip|Pc|Tba)\b/\U$&/g;
s/\bThru\b/Through/g;
s/\bAcct\b/Accounting/g;
s/\bAmer\b/American/g;
s/\bLit\b/Literature/g;
s/\bPrin\b/Principles/g;
s/\bBus\b/Business/g;
+ s/\bSyst\b/Sys/g;
return $_;
}
sub get_schedule_terms {
my (@sterms, @a);
- @a=('10','20','30','40'); # XXX 10 is fall, others are speculation
+ @a=('10','20','30','40'); # Fall, Spring, Summer I, Summer II
+ # Last 3 are currently guesses
my @localtime=localtime();
if ($localtime[4]<3) { # Through Mar 31
@sterms=((1900+$localtime[5]).$a[1]);
my @readheaders = ("Type", "Time", "Days", "Where", "Date Range", "Schedule Type", "Instructors");
my @class;
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, $title, $begin, $end, $times, $days, $session);
@terms = get_schedule_terms();
foreach (@_ ? @_ : @terms) {
next unless $l =~ s/.*<CAPTION class=[^>]*>(.* - .... \d\d\d\d \d\d\d)<\/caption>.*/$1/i;
push @class, $l;
}
- #$tf->parse($response->content);
- #print $response->content;
}
foreach my $ts ($te->table_states) {
foreach my $row ($ts->rows) {
$classid =~ s/.* - //;
$classid =~ s/ /-/g;
$title =~ s/ - .*//;
+ $row->[6] =~ s/\b([A-Z]r?)$/$1./;
$row->[6] = capitalize($row->[6]);
- $row->[6] =~ s/([A-Z]r?)$/$1./;
$row->[6] =~ s/ *\([A-Z]\)//;
$row->[1] =~ s/ ?([ap])m/\u$1M/g;
$row->[1] =~ s/ - /-/;
($begin, $end) = split(/ ?- ?/, $row->[1]);
$begin =~ s/^(\d):/0$1:/;
$end =~ s/^(\d):/0$1:/;
- if ($begin =~ /pm$/i && $begin !~ /^12/) {
- $begin =~ s/^(\d?\d)/$1+12/e;
+ if (($begin=~/PM$/ && $begin!~/^12/)||($begin=~/^12:..AM/)) {
+ $begin =~ s/^(\d?\d)/($1+12)%24/e;
}
- if ($end =~ /pm$/i && $end !~ /^12/) {
- $end =~ s/^(\d?\d)/$1+12/e;
+ if (($end=~/PM$/ && $end!~/^12/)||($end=~/^12:..AM/)) {
+ $end =~ s/^(\d?\d)/($1+12)%24/e;
}
$begin =~ s/ ?[AP]M//;
$end =~ s/ ?[AP]M//;
$row->[4] =~ s/([A-Za-z]{3,9})/(Decode_Month($1)<10?"0":"").Decode_Month($1)/eg;
$row->[4] =~ s/(\d\d?) (\d\d), (\d\d\d\d)/$3$1$2/g;
- $row->[4] =~ s/ - /\n/;
+ $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 => $title,
- #session => $row->[1],
+ title => capitalize($title),
instructor => $row->[6],
- #credits => $row->[3],
- #callnumber => $row->[4],
- #gradetype => $row->[5],
days => $row->[2],
- 'time' => $row->[1],
+ #'time' => $row->[1],
begin => $begin,
end => $end || undef,
duration => $row->[4],
location => $row->[3],
- #message => $row->[10],
- };
- #print join (" ", values %{$schedule[$#schedule]}), "\n";
+ );
+ my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
+ $class{'off'} = [ @off ] if (@off);
+ push @schedule, \%class;
}
}
return @schedule;
open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
while($_ = <INS>) {
chomp;
- m/"([^"]*)",([^,]*),([^,]*)/;
+ m/"([^"]*)",([^,]*),([^,]*)/; # "
($name, $email, $school) = ($1, $2, $3);
- $name =~ s/^([^,]*), ([^,]*)(.*)$/$2 $1$3/;
+ #$name =~ s/^([^,]*), ([^,]*)(.*)$/$2 $1$3/;
$name =~ s/ [A-Z]\.//g;
$name = lc $name;
$name =~ s/\W//g;
);
}
-sub do_mhc_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 Jun Jul Aug Sep Oct Nov Dec);
- my ($file, $current, @mhc, @schedule);
- $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
- @schedule = get_schedule(@_);
- @mhc = get_mhc_header;
- if(defined($file) && (-d $file)) {
- my @mhc2;
- 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] . " 1970 12:00:00 +0000"/e;
- push @mhc2, $_;
- }
- @mhc = @mhc2;
- }
- 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: 15 minutes\n";
- $current .= "X-SC-Record-Id: <".$row->{'id'}."\@from.sctweb>\n";
- if(defined($file) && (-d $file)) {
- $row->{'instructor'} =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
- my $email = get_faculty_email $row->{'instructor'};
- $row->{'instructor'} = '"' . $row->{'instructor'} . '" <'. ($email || ($1 || "unknown") . "\@from.sctweb") . ">";
- $row->{'duration'} =~ /^(\d\d\d\d)(\d\d)(\d\d)-\d{8}$/;
- $row->{'begin'} =~ /^(\d\d):(\d\d)$/;
- 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";
- }
- #print "\n$current";
- push @mhc, $current;
- }
- 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 = <FH>;
- 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_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);
- $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/);
- @schedule = get_schedule(@_);
- 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");
+sub do_xml_schedule {
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 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;
- 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 "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\r\n";
- print "END:VEVENT\r\n";
- if(defined($file) && (-d $file)) {
- print "END:VCALENDAR\r\n";
- close FH;
- select STDOUT;
- }
+ 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;
}
- print "END:VCALENDAR\r\n";
}
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.'">';
$shade = ($shade eq "dark"?"light":"dark");
print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
my $instructor = $row->{'instructor'};
- $instructor =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
+ #$instructor =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
my $email = get_faculty_email($instructor);
#if($email) {
#print '<td><a href="mailto:' . $email . '">'
print "<td>" . $row->{'instructor'} . "</td>";
#}
print "<td>" . $row->{'days'} . "</td>";
- print "<td>" . $row->{'time'} . "</td>";
+ print "<td>" . $row->{'begin'}."-<wbr />".$row->{'end'} . "</td>";
print "<td>" . $row->{'duration'} . "</td>";
print "<td>" . $row->{'location'} . "</td>";
print "</tr>\n";
print "</table>\n";
}
-sub do_html_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 @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points");
- my ($row, $lastrow);
- my $shade = "dark";
- 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 = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
- 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 id="grades" cellpadding="3" cellspacing="0">'."\n<tr>";
- print '<th align="left" class="sectionid">', $showheaders[0];
- print '</th><th align="left" class="coursetitle">', $showheaders[1];
- print '</th><th align="center" class="grade">';
- print join('</th><th align="center" class="right-number">',@showheaders[2 .. 5]);
- print "</th></tr>\n";
- 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/&/&/g;
- $row->[0] =~ s/Cumulative through/Cumulative: through/;
- $row->[0] =~ s/Cumulative:(.*) (\d{4}) (.*)/Cumulative:$1 $3 $2/;
- $lastrow=$row;
- next if $row->[0] =~ /:|Current Term/;
- print '<tr class="'.$shade.'">';
- $shade = ($shade eq "dark"?"light":"dark");
- print '<td align="left" class="sectionid">', $$row[0], '</td>';
- print '<td align="left" class="coursetitle">', $$row[1], '</td>';
- print '<td align="center" class="grade">';
- #print join('</td><td>', @$row[0 .. 1]);
- print join('</td><td align="right" class="right-number">', @$row[2 .. 5]);
- print "</td></tr>\n";
- }
- }
- print '<tr class="cumulative '.$shade.'"><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";
-}
-
-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_xml_grades {
+ print "<grades>\n</grades>\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/^\ |<pre>// || s/\ / /;
- s/<\/?([Hh]\d|[Bb])>//g;
- next if /<\/td>/;
- print "$_\n";
- }
+ print "Not implemented.\n";
}
-if ($arg eq "-g") {
- do_html_grades(@ARGV);
-} elsif ($arg eq "-s") {
+if ($arg eq "-s" || $arg eq "-h") {
do_html_schedule(@ARGV);
} elsif ($arg eq "-m") {
do_mhc_schedule(@ARGV);
do_csv_schedule(@ARGV);
} elsif ($arg eq "-v") {
do_vcalendar_schedule(@ARGV);
+} elsif ($arg eq "-g") {
+ do_xml_grades(@ARGV);
} elsif ($arg eq "-t") {
do_transcripts;
-} elsif ($arg eq "-f") {
- do_faculty;
+} elsif ($arg eq "-x" || 1) {
+ do_xml_schedule(@ARGV);
}
$ua->get("${url}twbkwbis.P_Logout"); # Logout