3 # -*- perl -*- vim: ft=perl sw=4 sts=4
5 # Brief usage instructions:
6 # Create a ~/.sct6rc that has SID=yourssn and PIN=yourpin
7 # You'll need to change the url below if you go anywhere but TAMUK.
10 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);
11 use Date::Calendar::Profiles qw($Profiles);
12 use Date::Calendar::Year;
13 use HTML::TableExtract;
16 use vars qw($ua %opts %faculty);
20 $opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
21 "Martin Luther King Day" => "3/Mon/Jan",
22 "Good Friday" => "-2",
23 "Spring Break Monday" => \&Spring_Break,
24 "Spring Break Tuesday" => \&Spring_Break,
25 "Spring Break Wednesday" => \&Spring_Break,
26 "Spring Break Thursday" => \&Spring_Break,
27 "Spring Break Friday" => \&Spring_Break,
28 "Spring Break Saturday" => \&Spring_Break,
29 "Study Day" => "4/Thu/Apr", # ?
30 "Memorial Day" => "5/Mon/May",
31 "Independence Day" => \&US_Independence,
32 "Labor Day" => \&US_Labor,
33 #"Columbus Day" => "2/Mon/Oct",
34 "Thanksgiving Day" => "4/Thu/Nov",
35 "Thanksgiving Friday" => \&Thanksgiving_Friday,
39 my($year,$label) = @_;
40 $label =~ s/^Spring Break //;
41 return( Add_Delta_Days(
42 Nth_Weekday_of_Month_Year($year,1,1,1),
43 7*(11-1) # This is for the 11th Monday of the year
44 +Decode_Day_of_Week($label)-1) );
47 sub Thanksgiving_Friday {
48 my($year,$label) = @_;
49 return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
51 sub US_Independence # Fourth of July
53 my($year,$label) = @_;
54 return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
56 sub US_Labor # First Monday after the first Sunday in September
58 my($year,$label) = @_;
59 return( Add_Delta_Days(
60 Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
63 my $config = $ENV{HOME} . "/.sct6rc";
64 if (($ARGV[0] || "") eq '-F') {
70 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
76 next unless m/^([^=]*)=(.*)/;
81 my $domain = $opts{'domain'} || "as1.tamuk.edu:9003";
82 my $url = "https://$domain/pls/PROD/";
83 $url = $opts{'url'} if(defined($opts{'url'}));
85 $ua = LWP::UserAgent->new;
88 $ua->cookie_jar( {} );
89 $ua->get("${url}twbkwbis.P_WWWLogin") or die "$!";
90 $response = $ua->post("${url}twbkwbis.P_ValLogin", { sid => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
92 $response = $ua->get("${url}bwskflib.P_SelDefTerm"); # Valid terms
93 die $response->status_line unless $response->is_success;
94 my @terms = grep {s/^<option value="([^"]*)">.*/$1/i} (split( /\r?\n/, $response->content)); # "
95 die "Site down. Try again later.\n" unless (@terms);
98 my ($section, $number) = split("-", shift);
100 foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
101 $id=10000*$id+$number;
107 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
108 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
109 my $firstday = Date::Calc->new($1,$2,$3);
110 my $lastday = Date::Calc->new($4,$5,$6);
111 my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
113 foreach(split(" ",$class{'days'})) {
114 push @days, Decode_Day_of_Week($_);
116 my @off = @{$class{'off'}};
117 for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
118 next unless(grep($_ == Day_of_Week($today->date), @days));
119 next if(grep($_ == $today, @off));
122 $today = Date::Calc->new(Date::Calc->gmtime->date);
123 for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
124 next unless(grep($_ == Day_of_Week($today->date), @days));
125 next if(grep($_ == $today, @off));
131 sub off_for_holidays {
133 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
134 my $d = $class{'days'};
135 $d =~ s/([MTWRFS])/ $days{$1}/g;
137 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
138 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
139 my $firstday = Date::Calc->new($1,$2,$3);
140 my $lastday = Date::Calc->new($4,$5,$6);
141 my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
144 foreach(split(" ",$d)) {
145 $days[Decode_Day_of_Week($_)] = 1;
147 foreach ($year->search("")) {
149 #foreach my $x ($year->labels($_)) {
150 # $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
152 next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
154 if (defined($days[Day_of_Week($_->date)]) && $good > 0);
156 return wantarray ? @holidays : "@holidays";
161 my $time = $class{'begin'} . "-" . $class{'end'};
162 $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
163 my $lastday = Date::Calc->new($1,$2,$3);
164 return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
165 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)";
166 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)";
167 my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
169 if($time =~ /$beginning/) {
171 } elsif($time =~ /$ending/) {
174 } elsif($days eq "2 4") {
175 if($time =~ /$beginning/) {
177 } elsif($time =~ /$ending/) {
180 } elsif($days eq "5") {
183 return wantarray ? () : "";
187 local $_ = shift || "";
189 s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
190 s/\b(I)(i*)\b/$1\U$2/g;
192 s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
193 s/\b(Mc)([a-z])/$1\u$2/g;
194 s/\b(Tcp\/Ip|Pc|Tba)\b/\U$&/g;
195 s/\bThru\b/Through/g;
196 s/\bAcct\b/Accounting/g;
197 s/\bAmer\b/American/g;
198 s/\bChem\b/Chemistry/g;
199 s/\bComp\b/Composition/g;
202 s/\bIntro\b/Introduction/g;
203 s/\bPrgm\b/Programming/g;
204 s/\bOp Sys\b/Operating System/g;
205 #s/\bGovt\b/Government/g;
206 s/\bLit\b/Literature/g;
207 s/\bPrin\b/Principles/g;
208 s/\bBus\b/Business/g;
213 sub get_schedule_terms {
215 @a=('10','20','30','40'); # Fall, Spring, Summer I, Summer II
216 # Last 3 are currently guesses
217 my @localtime=localtime();
218 if ($localtime[4]<3) { # Through Mar 31
219 @sterms=((1900+$localtime[5]).$a[1]);
220 } elsif ($localtime[4]<5) { # Through May 31
221 @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
222 } elsif ($localtime[4]<7) { # through July 31
223 @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
224 } elsif ($localtime[4]<8) { # through Aug 31
225 @sterms=((1900+$localtime[5]).$a[3],(1901+$localtime[5]).$a[0]);
226 } elsif ($localtime[4]<10) { # through Oct 31
227 @sterms=((1901+$localtime[5]).$a[0]);
229 @sterms=((1901+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
235 my @readheaders = ("Type", "Time", "Days", "Where", "Date Range", "Schedule Type", "Instructors");
237 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
238 my (@schedule, @terms, $classid, $title, $begin, $end, $times, $days, $session);
239 @terms = get_schedule_terms();
240 foreach (@_ ? @_ : @terms) {
241 $response = $ua->get("${url}bwskfshd.P_CrseSchdDetl?term_in=$_" );
242 die $response->status_line unless $response->is_success;
243 $te->parse($response->content);
244 foreach my $l (split (/\n/, $response->content)) {
245 next unless $l =~ s/.*<CAPTION class=[^>]*>(.* - .... \d\d\d\d \d\d\d)<\/caption>.*/$1/i;
249 foreach my $ts ($te->table_states) {
250 foreach my $row ($ts->rows) {
251 #map { s/\xa0//g; $_} @$row;
252 #$row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
253 #$row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
254 $classid = (shift @class);
256 $classid =~ s/.* - //;
259 $row->[6] =~ s/\b([A-Z]r?)$/$1./;
260 $row->[6] = capitalize($row->[6]);
261 $row->[6] =~ s/ *\([A-Z]\)//;
262 $row->[1] =~ s/ ?([ap])m/\u$1M/g;
263 $row->[1] =~ s/ - /-/;
264 ($begin, $end) = split(/ ?- ?/, $row->[1]);
265 $begin =~ s/^(\d):/0$1:/;
266 $end =~ s/^(\d):/0$1:/;
267 if (($begin=~/PM$/ && $begin!~/^12/)||($begin=~/^12:..AM/)) {
268 $begin =~ s/^(\d?\d)/($1+12)%24/e;
270 if (($end=~/PM$/ && $end!~/^12/)||($end=~/^12:..AM/)) {
271 $end =~ s/^(\d?\d)/($1+12)%24/e;
273 $begin =~ s/ ?[AP]M//;
275 $row->[4] =~ s/([A-Za-z]{3,9})/(Decode_Month($1)<10?"0":"").Decode_Month($1)/eg;
276 $row->[4] =~ s/(\d\d?) (\d\d), (\d\d\d\d)/$3$1$2/g;
277 $row->[4] =~ s/ - /-/;
278 $row->[3] =~ s/(ON|MAIN) CAMPUS\n|Palo Alto Building \d* //ig;
279 $row->[3] =~ s/\n/ /g;
282 title => capitalize($title),
283 instructor => $row->[6],
285 #'time' => $row->[1],
287 end => $end || undef,
288 duration => $row->[4],
289 location => $row->[3],
291 my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
292 $class{'off'} = [ @off ] if (@off);
293 push @schedule, \%class;
299 sub get_faculty_email {
300 my ($name, $school, $email);
301 if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
302 open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
305 m/"([^"]*)",([^,]*),([^,]*)/; # "
306 ($name, $email, $school) = ($1, $2, $3);
307 #$name =~ s/^([^,]*), ([^,]*)(.*)$/$2 $1$3/;
308 $name =~ s/ [A-Z]\.//g;
311 $faculty{$name} = $email;
315 $name =~ s/ [A-Z]r?\.//g;
318 return $faculty{$name};
323 "X-SC-Subject: New Years Day\nX-SC-Category: Holiday\nX-SC-Cond: 1 Jan\nX-SC-Duration: 00010101-\nX-SC-Record-Id: <New_Years_Day\@from.sctweb>\n",
324 "X-SC-Subject: Martin Luther King, Jr. Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Jan\nX-SC-Duration: 19870119-\nX-SC-Record-Id: <Martin_Luther_King_Jr_Day\@from.sctweb>\n",
325 "X-SC-Subject: Presidents Day\nX-SC-Category: Holiday\nX-SC-Cond: 3rd Mon Feb\nX-SC-Duration: 19710515-\nX-SC-Record-Id: <Presidents_Day\@from.sctweb>\n",
326 "X-SC-Subject: Memorial Day\nX-SC-Category: Holiday\nX-SC-Cond: Last Mon May\nX-SC-Duration: 19710531-\nX-SC-Record-Id: <Memorial_Day\@from.sctweb>\n",
327 "X-SC-Subject: Independence Day\nX-SC-Category: Holiday\nX-SC-Cond: 4 Jul\nX-SC-Duration: 17760704-\nX-SC-Record-Id: <Independence_Day\@from.sctweb>\n",
328 "X-SC-Subject: Labor Day\nX-SC-Category: Holiday\nX-SC-Cond: 1st Mon Sep\nX-SC-Duration: 18840901-\nX-SC-Record-Id: <Labor_Day\@from.sctweb>\n",
329 "X-SC-Subject: Columbus Day\nX-SC-Category: Holiday\nX-SC-Cond: 2nd Mon Oct\nX-SC-Duration: 19711011-\nX-SC-Record-Id: <Columbus_Day\@from.sctweb>\n",
330 "X-SC-Subject: Veterans Day\nX-SC-Category: Holiday\nX-SC-Cond: 11 Nov\nX-SC-Duration: 19261111-\nX-SC-Record-Id: <Veterans_Day\@from.sctweb>\n",
331 "X-SC-Subject: Thanksgiving\nX-SC-Category: Holiday\nX-SC-Cond: 4th Thu Nov\nX-SC-Duration: 14921122-\nX-SC-Record-Id: <Thanksgiving\@from.sctweb>\n",
332 "X-SC-Subject: Christmas\nX-SC-Category: Holiday\nX-SC-Cond: 25 Dec\nX-SC-Duration: 00011225-\nX-SC-Record-Id: <Christmas\@from.sctweb>\n",
336 sub do_xml_schedule {
337 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
338 my $schedule = { class => [ get_schedule(@_) ] };
339 my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
349 sub do_html_schedule {
350 my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location");
352 my @schedule = get_schedule(@_);
353 print '<table id="schedule" cellpadding="3" cellspacing="0">'."\n<tr><th>";
354 print join("</th><th>",@showheaders);
355 print "</th></tr>\n";
356 foreach my $row (@schedule) {
357 map { s/\n/<br \/>/g; $_} %$row;
358 $row->{'duration'} =~ s/-/<br \/>/g;
359 $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
360 print '<tr class="'.$shade.'">';
361 $shade = ($shade eq "dark"?"light":"dark");
362 print '<td class="idtitle">';
363 print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
364 print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
365 my $instructor = $row->{'instructor'};
366 #$instructor =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
367 my $email = get_faculty_email($instructor);
369 #print '<td><a href="mailto:' . $email . '">'
370 #. $row->{'instructor'} . "</a></td>";
372 print "<td>" . $row->{'instructor'} . "</td>";
374 print "<td>" . $row->{'days'} . "</td>";
375 print "<td>" . $row->{'begin'}."-<wbr />".$row->{'end'} . "</td>";
376 print "<td>" . $row->{'duration'} . "</td>";
377 print "<td>" . $row->{'location'} . "</td>";
384 my @readheaders = ("Section ID", "Course Title", "Grade", "Earned Hours", "Quality Hours", "Quality Points", "GPA");
385 @readheaders = ("Section", "Course Title", "FinalGrade", "Earned Hours", "Quality Hours", "Quality Points", "GPA") if($opts{'school'} eq 'tamuk');
386 my @showheaders = ("Section ID", "Course Title", "Grade", "Earned<br />Hours", "Quality<br />Hours", "Quality<br />Points");
389 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
390 my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
391 die $response->status_line unless $response->is_success;
392 #my @terms = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
394 my $year = Date::Calc->localtime->year;
395 for(my $y=$year-4;$y<=$year;$y++) { push @terms, ($y.2,$y."F",$y.3,$y.4,$y.1,$y."S"); }
396 foreach(@_ ? @_ : @terms) {
397 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
398 die $response->status_line unless $response->is_success;
399 $te->parse($response->content);
401 print '<table id="grades" cellpadding="3" cellspacing="0">'."\n<tr>";
402 print '<th align="left" class="sectionid">', $showheaders[0];
403 print '</th><th align="left" class="coursetitle">', $showheaders[1];
404 print '</th><th align="center" class="grade">';
405 print join('</th><th align="center" class="right-number">',@showheaders[2 .. 5]);
406 print "</th></tr>\n";
407 foreach my $ts ($te->table_states) {
408 foreach my $row ($ts->rows) {
409 map { s/^\s+//; s/\xa0|\r//g; s/\n/<br \/>/g; $_} @$row;
410 $row->[1] = capitalize($row->[1]);
411 $row->[1] =~ s/&/&/g;
412 $row->[0] =~ s/Cumulative through/Cumulative: through/;
413 $row->[0] =~ s/Cumulative:(.*) (\d{4}) (.*)/Cumulative:$1 $3 $2/;
415 next if $row->[0] =~ /:|Current Term/;
416 print '<tr class="'.$shade.'">';
417 $shade = ($shade eq "dark"?"light":"dark");
418 print '<td align="left" class="sectionid">', $$row[0], '</td>';
419 print '<td align="left" class="coursetitle">', $$row[1], '</td>';
420 print '<td align="center" class="grade">';
421 #print join('</td><td>', @$row[0 .. 1]);
422 print join('</td><td align="right" class="right-number">', @$row[2 .. 5]);
423 print "</td></tr>\n";
426 print '<tr class="cumulative '.$shade.'"><td id="cumulative" colspan="2">', capitalize($lastrow->[0]);
427 #shift @$lastrow; shift @$lastrow;
428 print '</td><td align="center" id="gpa" class="grade">';
430 print '</td><td align="right" class="right-number">';
431 print join('</td><td align="right" class="right-number">', @$lastrow[3 .. 5]);
432 print "</td></tr>\n";
437 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN&tserve_trans_config=rtranscr.cfg&CareerReqNum=1");
438 foreach (split /\n/, $response->content) {
439 next unless s/^\ |<pre>// || s/\ / /;
440 s/<\/?([Hh]\d|[Bb])>//g;
446 if ($arg eq "-s" || $arg eq "-h") {
447 do_html_schedule(@ARGV);
448 } elsif ($arg eq "-m") {
449 do_mhc_schedule(@ARGV);
450 } elsif ($arg eq "-c") {
451 do_csv_schedule(@ARGV);
452 } elsif ($arg eq "-v") {
453 do_vcalendar_schedule(@ARGV);
454 } elsif ($arg eq "-g") {
455 die "Grade support currently broken.\n";
456 do_html_grades(@ARGV);
457 } elsif ($arg eq "-t") {
458 die "Transcript support currently broken.\n";
460 } elsif ($arg eq "-x" || 1) {
461 do_xml_schedule(@ARGV);
464 $ua->get("${url}twbkwbis.P_Logout"); # Logout