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.
9 # Update: This tool now only outputs in XML. For other formats, convert
13 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);
14 use Date::Calendar::Profiles qw($Profiles);
15 use Date::Calendar::Year;
16 use HTML::TableExtract;
19 use vars qw($ua %opts %faculty);
23 $opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
24 "Martin Luther King Day" => "3/Mon/Jan",
25 "Good Friday" => "-2",
26 "Spring Break Monday" => \&Spring_Break,
27 "Spring Break Tuesday" => \&Spring_Break,
28 "Spring Break Wednesday" => \&Spring_Break,
29 "Spring Break Thursday" => \&Spring_Break,
30 "Spring Break Friday" => \&Spring_Break,
31 "Spring Break Saturday" => \&Spring_Break,
32 "Study Day" => "4/Thu/Apr", # ?
33 "Memorial Day" => "5/Mon/May",
34 "Independence Day" => \&US_Independence,
35 "Labor Day" => \&US_Labor,
36 #"Columbus Day" => "2/Mon/Oct",
37 "Thanksgiving Day" => "4/Thu/Nov",
38 "Thanksgiving Friday" => \&Thanksgiving_Friday,
42 my($year,$label) = @_;
43 $label =~ s/^Spring Break //;
44 return( Add_Delta_Days(
45 Nth_Weekday_of_Month_Year($year,1,1,1),
46 7*(11-1) # This is for the 11th Monday of the year
47 +Decode_Day_of_Week($label)-1) );
50 sub Thanksgiving_Friday {
51 my($year,$label) = @_;
52 return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
54 sub US_Independence # Fourth of July
56 my($year,$label) = @_;
57 return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
59 sub US_Labor # First Monday after the first Sunday in September
61 my($year,$label) = @_;
62 return( Add_Delta_Days(
63 Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
66 my $config = $ENV{HOME} . "/.sct6rc";
67 if (($ARGV[0] || "") eq '-F') {
73 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
79 next unless m/^([^=]*)=(.*)/;
84 my $domain = $opts{'domain'} || "as1.tamuk.edu:9003";
85 my $url = "https://$domain/pls/PROD/";
86 $url = $opts{'url'} if(defined($opts{'url'}));
88 $ua = LWP::UserAgent->new;
91 $ua->cookie_jar( {} );
92 $ua->get("${url}twbkwbis.P_WWWLogin") or die "$!";
93 $response = $ua->post("${url}twbkwbis.P_ValLogin", { sid => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
95 $response = $ua->get("${url}bwskflib.P_SelDefTerm"); # Valid terms
96 die $response->status_line unless $response->is_success;
97 my @terms = grep {s/^<option value="([^"]*)">.*/$1/i} (split( /\r?\n/, $response->content)); # "
98 die "Site down. Try again later.\n" unless (@terms);
101 my ($section, $number) = split("-", shift);
103 foreach (split //, $section) {$id=26*$id+(ord($_)-1)%32;}
104 $id=10000*$id+$number;
110 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
111 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
112 my $firstday = Date::Calc->new($1,$2,$3);
113 my $lastday = Date::Calc->new($4,$5,$6);
114 my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
116 foreach(split(" ",$class{'days'})) {
117 push @days, Decode_Day_of_Week($_);
119 my @off = @{$class{'off'}};
120 for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
121 next unless(grep($_ == Day_of_Week($today->date), @days));
122 next if(grep($_ == $today, @off));
125 $today = Date::Calc->new(Date::Calc->gmtime->date);
126 for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
127 next unless(grep($_ == Day_of_Week($today->date), @days));
128 next if(grep($_ == $today, @off));
134 sub off_for_holidays {
136 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
137 my $d = $class{'days'};
138 $d =~ s/([MTWRFS])/ $days{$1}/g;
140 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
141 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
142 my $firstday = Date::Calc->new($1,$2,$3);
143 my $lastday = Date::Calc->new($4,$5,$6);
144 my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
147 foreach(split(" ",$d)) {
148 $days[Decode_Day_of_Week($_)] = 1;
150 foreach ($year->search("")) {
152 #foreach my $x ($year->labels($_)) {
153 # $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
155 next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
157 if (defined($days[Day_of_Week($_->date)]) && $good > 0);
159 return wantarray ? @holidays : "@holidays";
164 my $time = $class{'begin'} . "-" . $class{'end'};
165 $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
166 my $lastday = Date::Calc->new($1,$2,$3);
167 return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
168 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)";
169 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)";
170 my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
172 if($time =~ /$beginning/) {
174 } elsif($time =~ /$ending/) {
177 } elsif($days eq "2 4") {
178 if($time =~ /$beginning/) {
180 } elsif($time =~ /$ending/) {
183 } elsif($days eq "5") {
186 return wantarray ? () : "";
190 local $_ = shift || "";
192 s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
193 s/\b(I)(i*)\b/$1\U$2/g;
195 s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
196 s/\b(Mc)([a-z])/$1\u$2/g;
197 s/\b(Tcp\/Ip|Pc|Tba)\b/\U$&/g;
198 s/\bThru\b/Through/g;
199 s/\bAcct\b/Accounting/g;
200 s/\bAmer\b/American/g;
201 s/\bChem\b/Chemistry/g;
202 s/\bComp\b/Composition/g;
205 s/\bIntro\b/Introduction/g;
206 s/\bPrgm\b/Programming/g;
207 s/\bOp Sys\b/Operating System/g;
208 #s/\bGovt\b/Government/g;
209 s/\bLit\b/Literature/g;
210 s/\bPrin\b/Principles/g;
211 s/\bBus\b/Business/g;
216 sub get_schedule_terms {
218 @a=('10','20','30','40'); # Fall, Spring, Summer I, Summer II
219 # Last 3 are currently guesses
220 my @localtime=localtime();
221 if ($localtime[4]<3) { # Through Mar 31
222 @sterms=((1900+$localtime[5]).$a[1]);
223 } elsif ($localtime[4]<5) { # Through May 31
224 @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
225 } elsif ($localtime[4]<7) { # through July 31
226 @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
227 } elsif ($localtime[4]<8) { # through Aug 31
228 @sterms=((1900+$localtime[5]).$a[3],(1901+$localtime[5]).$a[0]);
229 } elsif ($localtime[4]<10) { # through Oct 31
230 @sterms=((1901+$localtime[5]).$a[0]);
232 @sterms=((1901+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
238 my @readheaders = ("Type", "Time", "Days", "Where", "Date Range", "Schedule Type", "Instructors");
240 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
241 my (@schedule, @terms, $classid, $title, $begin, $end, $times, $days, $session);
242 @terms = get_schedule_terms();
243 foreach (@_ ? @_ : @terms) {
244 $response = $ua->get("${url}bwskfshd.P_CrseSchdDetl?term_in=$_" );
245 die $response->status_line unless $response->is_success;
246 $te->parse($response->content);
247 foreach my $l (split (/\n/, $response->content)) {
248 next unless $l =~ s/.*<CAPTION class=[^>]*>(.* - .... \d\d\d\d \d\d\d)<\/caption>.*/$1/i;
252 foreach my $ts ($te->table_states) {
253 foreach my $row ($ts->rows) {
254 #map { s/\xa0//g; $_} @$row;
255 #$row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
256 #$row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
257 $classid = (shift @class);
259 $classid =~ s/.* - //;
262 $row->[6] =~ s/\b([A-Z]r?)$/$1./;
263 $row->[6] = capitalize($row->[6]);
264 $row->[6] =~ s/ *\([A-Z]\)//;
265 $row->[1] =~ s/ ?([ap])m/\u$1M/g;
266 $row->[1] =~ s/ - /-/;
267 ($begin, $end) = split(/ ?- ?/, $row->[1]);
268 $begin =~ s/^(\d):/0$1:/;
269 $end =~ s/^(\d):/0$1:/;
270 if (($begin=~/PM$/ && $begin!~/^12/)||($begin=~/^12:..AM/)) {
271 $begin =~ s/^(\d?\d)/($1+12)%24/e;
273 if (($end=~/PM$/ && $end!~/^12/)||($end=~/^12:..AM/)) {
274 $end =~ s/^(\d?\d)/($1+12)%24/e;
276 $begin =~ s/ ?[AP]M//;
278 $row->[4] =~ s/([A-Za-z]{3,9})/(Decode_Month($1)<10?"0":"").Decode_Month($1)/eg;
279 $row->[4] =~ s/(\d\d?) (\d\d), (\d\d\d\d)/$3$1$2/g;
280 $row->[4] =~ s/ - /-/;
281 $row->[3] =~ s/(ON|MAIN) CAMPUS\n|Palo Alto Building \d* //ig;
282 $row->[3] =~ s/\n/ /g;
285 title => capitalize($title),
286 instructor => $row->[6],
288 #'time' => $row->[1],
290 end => $end || undef,
291 duration => $row->[4],
292 location => $row->[3],
294 my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
295 $class{'off'} = [ @off ] if (@off);
296 push @schedule, \%class;
302 sub get_faculty_email {
303 my ($name, $school, $email);
304 if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) {
305 open INS, $ENV{'HOME'} . "/public_html/faculty.csv";
308 m/"([^"]*)",([^,]*),([^,]*)/; # "
309 ($name, $email, $school) = ($1, $2, $3);
310 #$name =~ s/^([^,]*), ([^,]*)(.*)$/$2 $1$3/;
311 $name =~ s/ [A-Z]\.//g;
314 $faculty{$name} = $email;
318 $name =~ s/ [A-Z]r?\.//g;
321 return $faculty{$name};
326 "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",
327 "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",
328 "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",
329 "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",
330 "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",
331 "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",
332 "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",
333 "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",
334 "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",
335 "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",
339 sub do_xml_schedule {
340 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
341 my $schedule = { class => [ get_schedule(@_) ] };
342 my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
352 sub do_html_schedule {
353 my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location");
355 my @schedule = get_schedule(@_);
356 print '<table id="schedule" cellpadding="3" cellspacing="0">'."\n<tr><th>";
357 print join("</th><th>",@showheaders);
358 print "</th></tr>\n";
359 foreach my $row (@schedule) {
360 map { s/\n/<br \/>/g; $_} %$row;
361 $row->{'duration'} =~ s/-/<br \/>/g;
362 $row->{'duration'} =~ s/\d\d(\d\d)(\d\d)(\d\d)/$2-$3-$1/g;
363 print '<tr class="'.$shade.'">';
364 $shade = ($shade eq "dark"?"light":"dark");
365 print '<td class="idtitle">';
366 print '<span class="sectionid">' .$row->{'id'}. '</span><br />';
367 print '<span class="coursetitle">' .$row->{'title'}. '</span></td>';
368 my $instructor = $row->{'instructor'};
369 #$instructor =~ s/^([^,]*), ([^,]*)(.*)/$2 $1$3/;
370 my $email = get_faculty_email($instructor);
372 #print '<td><a href="mailto:' . $email . '">'
373 #. $row->{'instructor'} . "</a></td>";
375 print "<td>" . $row->{'instructor'} . "</td>";
377 print "<td>" . $row->{'days'} . "</td>";
378 print "<td>" . $row->{'begin'}."-<wbr />".$row->{'end'} . "</td>";
379 print "<td>" . $row->{'duration'} . "</td>";
380 print "<td>" . $row->{'location'} . "</td>";
387 print "<grades>\n</grades>\n";
391 print "Not implemented.\n";
394 if ($arg eq "-s" || $arg eq "-h") {
395 do_html_schedule(@ARGV);
396 } elsif ($arg eq "-m") {
397 do_mhc_schedule(@ARGV);
398 } elsif ($arg eq "-c") {
399 do_csv_schedule(@ARGV);
400 } elsif ($arg eq "-v") {
401 do_vcalendar_schedule(@ARGV);
402 } elsif ($arg eq "-g") {
403 do_xml_grades(@ARGV);
404 } elsif ($arg eq "-t") {
406 } elsif ($arg eq "-x" || 1) {
407 do_xml_schedule(@ARGV);
410 $ua->get("${url}twbkwbis.P_Logout"); # Logout