3 # -*- perl -*- vim: ft=perl sw=4 sts=4
5 # Brief usage instructions:
6 # Create a ~/.sctwebrc that has SID=yourssn and PIN=yourpin
7 # You'll need to change the url below if you go anywhere but Northwest Vista.
9 # Update: This tool now only outputs in XML. For other formats, convert
14 use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Nth_Weekday_of_Month_Year Gmtime Mktime);
15 use Date::Calendar::Profiles qw($Profiles);
16 use Date::Calendar::Year;
17 use HTML::TableExtract;
20 use vars qw($ua %opts %faculty);
24 $opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
25 "Martin Luther King Day" => "3/Mon/Jan",
26 "Good Friday" => "-2",
27 "Spring Break Monday" => \&Spring_Break,
28 "Spring Break Tuesday" => \&Spring_Break,
29 "Spring Break Wednesday" => \&Spring_Break,
30 "Spring Break Thursday" => \&Spring_Break,
31 "Spring Break Friday" => \&Spring_Break,
32 "Spring Break Saturday" => \&Spring_Break,
33 "Fiesta Holiday" => "4/Fri/Apr",
34 "Memorial Day" => "5/Mon/May",
35 "Independence Day" => \&US_Independence,
36 "Labor Day" => \&US_Labor,
37 "Columbus Day" => "2/Mon/Oct",
38 "Thanksgiving Day" => "4/Thu/Nov",
39 "Thanksgiving Friday" => \&Thanksgiving_Friday,
43 my($year,$label) = @_;
44 $label =~ s/^Spring Break //;
45 return( Add_Delta_Days(
46 Nth_Weekday_of_Month_Year($year,1,1,1),
47 7*(11-1) # This is for the 11th Monday of the year
48 +Decode_Day_of_Week($label)-1) );
51 sub Thanksgiving_Friday {
52 my($year,$label) = @_;
53 return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
55 sub US_Independence # Fourth of July
57 my($year,$label) = @_;
58 return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
60 sub US_Labor # First Monday after the first Sunday in September
62 my($year,$label) = @_;
63 return( Add_Delta_Days(
64 Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
67 my $config = $ENV{HOME} . "/.sctwebrc";
68 if ($ARGV[0] eq '-F') {
74 $arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/);
80 next unless m/^([^=]*)=(.*)/;
85 my $domain = $opts{'domain'} || "sctweb.accd.edu";
86 my $url = "https://$domain/ia-bin4/tsrvweb.exe?WID=W&tserve_tip_read_destroy&tserve_host_code=4&tserve_tiphost_code=0";
87 $url = $opts{'url'} if(defined($opts{'url'}));
89 $ua = LWP::UserAgent->new;
92 $ua->cookie_jar( {} );
93 $ua->get("$url&tserve_tip_write=||WID&tserve_transconfig=astulog.cfg") or die "$!";
94 $response = $ua->post("$url&tserve_trans_config=astulog.cfg", { tserve_tip_write => "%7C%7CWID%7CSID%7CPIN", SID => $opts{SID}, PIN => $opts{PIN} }) or die "$!";
96 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=" . ("regterm.cfg"||"rgrdterm.cfg")); # Valid terms
97 die $response->status_line unless $response->is_success;
98 my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content)); # "
100 $opts{school} = 'accd';
102 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=aterm.cfg"); # Valid terms
103 die $response->status_line unless $response->is_success;
104 @terms = grep {s/&Term=(\w*)$/$1/} (split( /\r\n/, $response->content));
105 $opts{school} = 'tamuk';
107 die "Site down. Try again later.\n" unless (@terms);
109 sub off_for_holidays {
111 $class{'duration'} =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)/;
112 my $days = Delta_Days($1,$2,$3,$4,$5,$6);
113 my $firstday = Date::Calc->new($1,$2,$3);
114 my $lastday = Date::Calc->new($4,$5,$6);
115 my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
118 foreach(split(" ",$class{days})) {
119 $days[Decode_Day_of_Week($_)] = 1;
121 foreach ($year->search("")) {
123 #foreach my $x ($year->labels($_)) {
124 # $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
126 next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
128 if (defined($days[Day_of_Week($_->date)]) && $good > 0);
130 return wantarray ? @holidays : "@holidays";
135 my $time = $class{'begin'} . "-" . $class{'end'};
136 $class{'duration'} =~ /\d{8}-(\d\d\d\d)(\d\d)(\d\d)/;
137 my $lastday = Date::Calc->new($1,$2,$3);
138 return wantarray ? () : undef unless (Day_of_Week($lastday->date) == 7);
139 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)";
140 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)";
141 my $days = join(" ",map {Decode_Day_of_Week($_)} split(/ /, $class{'days'}));
143 if($time =~ /$beginning/) {
145 } elsif($time =~ /$ending/) {
148 } elsif($days eq "2 4") {
149 if($time =~ /$beginning/) {
151 } elsif($time =~ /$ending/) {
154 } elsif($days eq "5") {
157 return wantarray ? () : "";
161 local $_ = shift || "";
163 s/\b([A-Z])([A-Z]*)\b/$1\L$2/g;
164 s/\b(I)(i*)\b/$1\U$2/g;
166 s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
167 s/\b(Mc)([a-z])/$1\u$2/g;
168 s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
169 s/\bThru\b/Through/g;
170 s/\bAcct\b/Accounting/g;
171 s/\bAmer\b/American/g;
172 s/\bChem\b/Chemistry/g;
173 s/\bComp\b/Composition/g;
176 s/\bIntro\b/Introduction/g;
177 s/\bPrgm\b/Programming/g;
178 s/\bOp Sys\b/Operating System/g;
179 #s/\bGovt\b/Government/g;
180 s/\bLit\b/Literature/g;
181 s/\bPrin\b/Principles/g;
182 s/\bBus\b/Business/g;
186 sub get_schedule_terms {
188 if($opts{school} eq 'tamuk') {
189 @a=('F','S','1','2');
191 @a=('1','2','3','4');
193 my @localtime=localtime();
194 if ($localtime[4]<3) { # Through Mar 31
195 @sterms=((1900+$localtime[5]).$a[1]);
196 } elsif ($localtime[4]<5) { # Through May 31
197 @sterms=((1900+$localtime[5]).$a[1],(1900+$localtime[5]).$a[2]);
198 } elsif ($localtime[4]<7) { # through July 31
199 @sterms=((1900+$localtime[5]).$a[2],(1900+$localtime[5]).$a[3]);
200 } elsif ($localtime[4]<8) { # through Aug 31
201 @sterms=((1900+$localtime[5]).$a[3],(1900+$localtime[5]).$a[0]);
202 } elsif ($localtime[4]<10) { # through Oct 31
203 @sterms=((1900+$localtime[5]).$a[0]);
205 @sterms=((1900+$localtime[5]).$a[0],(1901+$localtime[5]).$a[1]);
211 my @readheaders = ("Section ID/Title", "Session", "Instructor", "Credits", "CallNumber","GradeType", "Days", "Time", "Start/EndDates", "Site/Building/Room", "Message");
212 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
213 @readheaders = ("Course", "Days\xa0TimeLocation", "Session and Dates", "Instructor", "Credit", "CallNumber", "GradeType", "Message");
214 my $tf = new HTML::TableExtract( headers => [ @readheaders ] );
215 my (@schedule, @terms, $classid, $begin, $end, $times, $days, $session);
216 @terms = get_schedule_terms();
217 foreach (@_ ? @_ : @terms) {
218 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rstusch.cfg&Term=".($_) );
219 die $response->status_line unless $response->is_success;
220 $te->parse($response->content);
221 $tf->parse($response->content);
222 #print $response->content;
224 foreach my $ts ($te->table_states) {
225 foreach my $row ($ts->rows) {
226 map { s/\xa0//g; $_} @$row;
227 $row->[0] =~ s/.*launchWebCT\("([^"]*)"\).*/$1/s;
228 $row->[0] =~ s/(.*) ?<[Bb][Rr][^>]*>(.*)/capitalize($2)/eg;
230 $row->[2] = capitalize($row->[2]);
231 $row->[2] =~ s/([A-Z]r?)$/$1./;
232 $row->[2] =~ s/^([^,]*), ([^,.]*|[^,]{4,})/$2 $1/;
233 ($begin, $end) = split("-", $row->[7]);
234 if($row->[7] =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
235 if($1 < $2 && $2 != 12) {
236 $begin =~ s/^(\d\d)/$1+12/e;
239 $end =~ s/^(\d\d)/$1+12/e;
242 } else { $end =~ s/AM$//; }
243 $row->[8] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
244 $row->[9] =~ s/(ON|MAIN) CAMPUS\n//ig;
245 $row->[9] =~ s/\n/ /g;
249 #session => $row->[1],
250 instructor => $row->[2],
251 credits => $row->[3],
253 #gradetype => $row->[5],
255 #'time' => $row->[7],
257 end => $end || undef,
258 duration => $row->[8],
259 location => $row->[9],
260 #message => $row->[10],
262 my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
263 $class{'off'} = [ @off ] if (@off);
264 push @schedule, \%class;
267 foreach my $ts ($tf->table_states) {
268 foreach my $row ($ts->rows) {
269 next if ($row->[0] =~ /Total Enrolled Hours/);
270 $row->[1] =~ s/\xa0/ /g;
271 map { s/\xa0//g; $_} @$row;
272 map { s/^\r?\n? *//g; $_} @$row;
273 $row->[0] =~ s/.*launchWebCT\("([^"]*)"[^)]*\).*>\s*//s;
275 $row->[0] =~ s/(.*)/capitalize($1)/eg;
276 $row->[1] =~ s/^([MTWRFS]*) *([0-9:-]*[AP]M)\s*//;
277 ($days, $times) = ($1, $2);
278 ($begin, $end) = split("-", $times);
279 if($times =~ /^(\d\d):\d\d-(\d\d):\d\dPM$/) {
280 if($1 < $2 && $2 != 12) {
281 $begin =~ s/^(\d\d)/$1+12/e;
284 $end =~ s/^(\d\d)/$1+12/e;
287 } else { $end =~ s/AM$//; }
288 $row->[2] =~ s/(.*\S)\s*//;
290 $row->[2] =~ s/(\d\d)-(\d\d)-(\d\d)/20$3$1$2/g;
291 $row->[2] =~ s/ *to */-/g;
292 $row->[3] = capitalize($row->[3]);
293 $row->[3] =~ s/([A-Z]r?)$/$1./;
294 $row->[3] =~ s/^([^,]*), ([^,.]{3}[^,]*)/$2 $1/;
298 #session => $session,
299 instructor => $row->[3],
300 credits => $row->[4],
302 #gradetype => $row->[6],
306 end => $end || undef,
307 duration => $row->[2],
308 location => $row->[1],
309 #message => $row->[7],
311 my @off = ( map { "$_"; } (off_for_holidays(%class), off_for_exams(%class)));
312 $class{'off'} = [ @off ] if (@off);
313 push @schedule, \%class;
319 sub do_xml_schedule {
320 my $file = shift if (defined $_[0] and $_[0] !~ /^\d/);
321 my $schedule = { class => [ get_schedule(@_) ] };
322 my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule');
333 my @readheaders = ("Section ID", "Course Title", "Grade", "Earned Hours", "Quality Hours", "Quality Points", "GPA");
334 @readheaders = ("Section", "Course Title", "FinalGrade", "Earned Hours", "Quality Hours", "Quality Points", "GPA") if($opts{'school'} eq 'tamuk');
336 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
337 my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
338 die $response->status_line unless $response->is_success;
339 #my @terms = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
342 my $year = Date::Calc->localtime->year;
343 for(my $y=$year-4;$y<=$year;$y++) { push @terms, ($y.2,$y."F",$y.3,$y.4,$y.1,$y."S"); }
344 foreach(@_ ? @_ : @terms) {
345 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
346 die $response->status_line unless $response->is_success;
347 $te->parse($response->content);
349 foreach my $ts ($te->table_states) {
350 foreach my $row ($ts->rows) {
351 map { s/^\s+//; s/\xa0|\r//g; s/\n/<br \/>/g; $_} @$row;
352 $row->[1] = capitalize($row->[1]);
353 $row->[1] =~ s/&/&/g;
354 $row->[0] =~ s/Cumulative through/Cumulative: through/;
355 $row->[0] =~ s/Cumulative:(.*) (\d{4}) (.*)/Cumulative:$1 $3 $2/;
357 next if $row->[0] =~ /:|Current Term/;
368 $lastrow->[0] =~ s/Cumulative:? *through *//i;
369 my $cumulative = { term => capitalize($lastrow->[0]),
370 gpa => $lastrow->[6],
371 earned => $lastrow->[3],
372 hours => $lastrow->[4],
373 points => $lastrow->[5]
375 my $xml = XMLout({class => [@grades], cumulative => $cumulative}, NoAttr => 1, RootName => 'grades');
378 print '<tr class="cumulative"><td id="cumulative" colspan="2">', capitalize($lastrow->[0]);
379 #shift @$lastrow; shift @$lastrow;
380 print '</td><td align="center" id="gpa" class="grade">';
382 print '</td><td align="right" class="right-number">';
383 print join('</td><td align="right" class="right-number">', @$lastrow[3 .. 5]);
384 print "</td></tr>\n";
389 my @readheaders = ("Name", "College", "Email");
390 my $te = new HTML::TableExtract( headers => [ @readheaders ] );
391 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)) {
392 my $response = $ua->get("http://$domain/fs/".$_."dir.htm");
393 die $response->status_line unless $response->is_success;
394 $te->parse($response->content);
396 foreach my $ts ($te->table_states) {
397 foreach my $row ($ts->rows) {
398 map { s/[\r\n]//g; $_} @$row;
399 $row->[0] = capitalize($row->[0]);
400 $row->[0] =~ s/([A-Z]r?)$/$1./;
401 print '"'.$row->[0].'",',$row->[2].',', $row->[1], "\n";
407 $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN&tserve_trans_config=rtranscr.cfg&CareerReqNum=1");
408 foreach (split /\n/, $response->content) {
409 next unless s/^\ |<pre>// || s/\ / /;
410 s/<\/?([Hh]\d|[Bb])>//g;
417 do_xml_grades(@ARGV);
418 } elsif ($arg eq "-t") {
420 } elsif ($arg eq "-f") {
422 } elsif ($arg eq "-x" || $arg eq "-s" || 1) {
423 do_xml_schedule(@ARGV);
426 $ua->get("$url&tserve_trans_config=alogout.cfg"); # Logout