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);
102 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
103 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
104 my $firstday = Date::Calc->new($1,$2,$3);
105 my $lastday = Date::Calc->new($4,$5,$6);
106 my $today = Date::Calc->new(Date::Calc->localtime(time+3600*6)->date);
108 foreach(split(" ",$class{'days'})) {
109 push @days, Decode_Day_of_Week($_);
111 my @off = @{$class{'off'}};
112 for($today = ($today > $firstday ? $today : $firstday); $today < $lastday; $today++) {
113 next unless(grep($_ == Day_of_Week($today->date), @days));
114 next if(grep($_ == $today, @off));
117 $today = Date::Calc->new(Date::Calc->gmtime->date);
118 for($today = ($today < $lastday ? $today : $lastday); $today > $firstday; $today--) {
119 next unless(grep($_ == Day_of_Week($today->date), @days));
120 next if(grep($_ == $today, @off));
126 sub off_for_holidays {
128 my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU");
129 my $d = $class{'days'};
130 $d =~ s/([MTWRFS])/ $days{$1}/g;
132 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
133 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
134 my $firstday = Date::Calc->new($1,$2,$3);
135 my $lastday = Date::Calc->new($4,$5,$6);
136 my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
139 foreach(split(" ",$d)) {
140 $days[Decode_Day_of_Week($_)] = 1;
142 foreach ($year->search("")) {
144 #foreach my $x ($year->labels($_)) {
145 # $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
147 next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
149 if (defined($days[Day_of_Week($_->date)]) && $good > 0);
151 return wantarray ? @holidays : "@holidays";
156 my $time = $class{'begin'} . "-" . $class{'end'};
157 $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
158 my $lastday = Date::Calc->new($1,$2,$3);
159 return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
160 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)";
161 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)";
162 my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
164 if($time =~ /$beginning/) {
166 } elsif($time =~ /$ending/) {
169 } elsif($days eq "2 4") {
170 if($time =~ /$beginning/) {
172 } elsif($time =~ /$ending/) {
175 } elsif($days eq "5") {
178 return wantarray ? () : "";
182 local $_ = shift || "";
184 s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
185 s/\b(I)(i*)\b/$1\U$2/g;
187 s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
188 s/\b(Mc)([a-z])/$1\u$2/g;
189 s/\b(Tcp\/Ip|Pc|Tba)\b/\U$&/g;
190 s/\bThru\b/Through/g;
191 s/\bAcct\b/Accounting/g;
192 s/\bAmer\b/American/g;
193 s/\bChem\b/Chemistry/g;
194 s/\bComp\b/Composition/g;
197 s/\bIntro\b/Introduction/g;
198 s/\bPrgm\b/Programming/g;
199 s/\bOp Sys\b/Operating System/g;
200 #s/\bGovt\b/Government/g;
201 s/\bLit\b/Literature/g;
202 s/\bPrin\b/Principles/g;
203 s/\bBus\b/Business/g;
208 sub get_schedule_terms {
210 @a=('10','20','30','40'); # Fall, Spring, Summer I, Summer II
211 # Last 3 are currently guesses
212 my @localtime=localtime();
213 if ($localtime[4]<3) { # Through Mar 31
214 @sterms=((1900+$localtime[5]).$a[1]);
215 } elsif ($localtime[4]<5) { # Through May 31
216 @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
217 } elsif ($localtime[4]<7) { # through July 31
218 @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
219 } elsif ($localtime[4]<8) { # through Aug 31
220 @sterms=((1900+$localtime[5]).$a[3],(1901+$localtime[5]).$a[0]);
221 } elsif ($localtime[4]<10) { # through Oct 31
222 @sterms=((1901+$localtime[5]).$a[0]);
224 @sterms=((1901+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
230 my @readheaders = ("Type", "Time", "Days", "Where", "Date Range", "Schedule Type", "Instructors");
232 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
233 my (@schedule, @terms, $classid, $title, $begin, $end, $times, $days, $session);
234 @terms = get_schedule_terms();
235 foreach (@_ ? @_ : @terms) {
236 $response = $ua->get("${url}bwskfshd.P_CrseSchdDetl?term_in=$_" );
237 die $response->status_line unless $response->is_success;
238 $te->parse($response->content);
239 foreach my $l (split (/\n/, $response->content)) {
240 next unless $l =~ s/.*<CAPTION class=[^>]*>(.* - .... \d\d\d\d \d\d\d)<\/caption>.*/$1/i;
244 foreach my $ts ($te->table_states) {
245 foreach my $row ($ts->rows) {
246 #map { s/\xa0//g; $_} @$row;
247 #$row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
248 #$row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
249 $classid = (shift @class);
251 $classid =~ s/.* - //;
254 $row->[6] =~ s/\b([A-Z]r?)$/$1./;
255 $row->[6] = capitalize($row->[6]);
256 $row->[6] =~ s/ *\([A-Z]\)//;
257 $row->[1] =~ s/ ?([ap])m/\u$1M/g;
258 $row->[1] =~ s/ - /-/;
259 ($begin, $end) = split(/ ?- ?/, $row->[1]);
260 $begin =~ s/^(\d):/0$1:/;
261 $end =~ s/^(\d):/0$1:/;
262 if (($begin=~/PM$/ && $begin!~/^12/)||($begin=~/^12:..AM/)) {
263 $begin =~ s/^(\d?\d)/($1+12)%24/e;
265 if (($end=~/PM$/ && $end!~/^12/)||($end=~/^12:..AM/)) {
266 $end =~ s/^(\d?\d)/($1+12)%24/e;
268 $begin =~ s/ ?[AP]M//;
270 $row->[4] =~ s/([A-Za-z]{3,9})/(Decode_Month($1)<10?"0":"").Decode_Month($1)/eg;
271 $row->[4] =~ s/(\d\d?) (\d\d), (\d\d\d\d)/$3$1$2/g;
272 $row->[4] =~ s/ - /-/;
273 $row->[3] =~ s/(ON|MAIN) CAMPUS\n|Palo Alto Building \d* //ig;
274 $row->[3] =~ s/\n/ /g;
277 title => capitalize($title),
278 instructor => $row->[6],
280 #'time' => $row->[1],
282 end => $end || undef,
283 duration => $row->[4],
284 location => $row->[3],
286 my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
287 $class{'off'} = [ @off ] if (@off);
288 push @schedule, \%class;
294 sub do_xml_schedule {
295 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
296 my $schedule = { class => [ get_schedule(@_) ] };
297 my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
308 print "<grades>\n</grades>\n";
312 print "Not implemented.\n";
316 do_xml_grades(@ARGV);
317 } elsif ($arg eq "-t") {
319 } elsif ($arg eq "-x" || $arg eq "-s" || 1) {
320 do_xml_schedule(@ARGV);
323 $ua->get("${url}twbkwbis.P_Logout"); # Logout