#!/opt/bin/perl use POSIX qw(mktime strftime); local($urlpre) = "http://nhl.com/nhlonline/nhl?service=direct&context=Schedule/monthScheduleLink&month="; local($urlpost) = "&location=/onthefly/schedules/index.html&print=true"; local($cmd) = "/opt/bin/lynx -source "; local(@findteams) = ( ); local($findhome) = 1; local($findaway) = 1; local($findtoday) = 0; local($findtomor) = 0; local($findpast) = 0; local($findfuture) = 1; local($findall) = 0; local($displayshort) = 0; local(%matchteams) = ( ); local($usecache) = 1; local(%cachedfile) = ( "MAR" => "/home/nehpets/dev/nhl/cache/MAR.html", "APR" => "/home/nehpets/dev/nhl/cache/APR.html", ); local($todate) = strftime("%a %b %e", localtime(time)); local($tomdate) = strftime("%a %b %e", localtime((time + (24 * 60 * 60)))); local($row) = 0; local($datum) = 0; local(@data) = ( "date", "away", "awaygoals", "home", "homegoals", "time", "network", "recap", "BADBADBAD", ); local(%months) = ( "JAN" => "0", "FEB" => "1", "MAR" => "2", "APR" => "3", "MAY" => "4", "JUN" => "5", "JUL" => "6", "AUG" => "7", "SEP" => "8", "OCT" => "9", "NOV" => "10", "DEC" => "11", ); local(%teams) = ( "avs" => "avalanche", ); local(@games) = ( ); &Main; sub Main { &process_args(@ARGV); &process_month("OCT"); &process_month("NOV"); &process_month("DEC"); &process_month("JAN"); &process_month("FEB"); &process_month("MAR"); &process_month("APR"); &process_month("MAY"); &list_all(); } sub teamis { my($guess) = @_; $guess = lc($guess); if($teams{$guess}) { return $teams{$guess}; } else { return $guess; } } sub process_args { my(@bits) = @_; my($bit) = ""; while(($bit = shift(@bits))) { if($bit eq "--live") { $usecache = ! $usecache; } elsif ($bit eq "--team" || $bit eq "--teams") { my(@finds) = split(/,/,lc(shift(@bits))); for $find (@finds) { push(@findteams, &teamis($find)); } } elsif ($bit eq "--home") { $findaway = 0; $findhome = 1; } elsif ($bit eq "--sh") { $displayshort++; } elsif ($bit eq "--away") { $findaway = 1; $findhome = 0; } elsif ($bit eq "--today") { $findtoday = 1; $findpast = $findfuture = $findall = 0; } elsif ($bit eq "--tom") { $findtomor = 1; $findpast = $findfuture = $findall = 0; } elsif ($bit eq "--past") { $findpast = 1; $findtomor = $findtoday = $findfuture = $findall = 0; } elsif ($bit eq "--future") { $findfuture= 1; $findtomor = $findtoday = $findpast = $findall = 0; } elsif ($bit eq "--all") { $findall= 1; $findtomor = $findtoday = $findtpast = $findfuture = 0; } elsif ($bit eq "--match") { my(@finds) = split(/,/,lc(shift(@bits))); for $find (@finds) { my(@doublet) = split(/-/, $find); if(@doublet == 2) { my($teamone) = &teamis($doublet[0]); my($teamtwo) = &teamis($doublet[1]); } } } elsif (0) { } } } sub process_month { my($toprocess) = @_; my(%game) = { }; if(! $usecache || !open(CMD, "/home/nehpets/dev/nhl/cache/$toprocess.html")) { my($run_cmd) = $cmd."'".$urlpre.$toprocess.$urlpost."'"; if(!open(CMD, "$run_cmd|")) { return; } } while() { my($line) = $_; if($line =~ // || $line =~ //) { $row = 1; @game = ( ); $datum = 0; } if($row) { if($line =~ /]*)>(.*)<\/td>/) { my($bit) = $2; if($data[$datum] =~ /goals$/) { $bit = sprintf("%d", $bit); } $game{$data[$datum]} = $bit; if(0 && length($bit)) { printf("%10s: %15s\n", $data[$datum], $bit); } $datum++; } if($line =~ /<\/tr>/) { $row = 0; $game{"ts"} = &date_to_timestamp($game{"date"}, $game{"time"}); push(@games, {%game}); } } } close(CMD); } sub list_all { my(@display) = ( ); for $single (sort bynum @games) { my($saway) = lc($single->{"away"}); my($shome) = lc($single->{"home"}); my($sdate) = strftime("%a %b %e", localtime($single->{"ts"})); if(! scalar(@findteams) || ($findaway && grep(/$saway/, @findteams)) || ($findhome && grep(/$shome/, @findteams))) { if($findall || ($findtoday && $sdate eq $todate) || ($findtomor && $sdate eq $tomdate) || ($findpast && time > $single->{"ts"}) || ($findfuture && time < $single->{"ts"})) { push(@display, $single); } } } printf("%s game%s\n", scalar(@display), (scalar(@display) == 1 ? " " : "s")); for $entry (sort bynum @display) { my($saway) = lc($entry->{"away"}); my($shome) = lc($entry->{"home"}); my($stime) = ($entry->{"ts"} - 3*60*60); my($first) = "away"; my($firstg)= "awaygoals"; my($second) = "home"; my($secondg) = "homegoals"; my($locind) = "@"; my($differ) = abs($entry->{$firstg} - $entry->{$secondg}); if(scalar(@findteams) == 1) { if(grep(/$saway/, @findteams)) { $first = "home"; $firstg= "homegoals"; $second = "away"; $secondg = "awaygoals"; $locind = "-"; } $differ = $entry->{$secondg} - $entry->{$firstg}; } if($displayshort) { printf("%-2.2s %-s %12s %s%s%s %-12s %s\n", strftime("%a", localtime($stime)), strftime("%m/%d %H:%M", localtime($stime)), $entry->{$first}, ($stime > time ? " " : ($entry->{$firstg} > $entry->{$secondg} ? "<" : ($entry->{$firstg} == $entry->{$secondg} ? "=" : " "))), $locind, ($stime > time ? " " : ($entry->{$firstg} < $entry->{$secondg} ? ">" : ($entry->{$firstg} == $entry->{$secondg} ? "=" : " "))), $entry->{$second}, ($displayshort > 1 ? $entry->{"network"} : "")); } else { printf("%-s %2s %12s %s %s%s%s %s %-12s %s\n", strftime("%a %b %e %H:%M", localtime($stime)), ($stime > time ? "__" : $differ), $entry->{$first}, ($stime > time ? " " : "[".$entry->{$firstg}."]"), ($stime > time ? " " : ($entry->{$firstg} > $entry->{$secondg} ? "<" : ($entry->{$firstg} == $entry->{$secondg} ? "=" : " "))), $locind, ($stime > time ? " " : ($entry->{$firstg} < $entry->{$secondg} ? ">" : ($entry->{$firstg} == $entry->{$secondg} ? "=" : " "))), ($stime > time ? " " : "[".$entry->{$secondg}."]"), $entry->{$second}, $entry->{"network"}); } } } sub bynum { $a->{"ts"} <=> $b->{"ts"}; } # Mar 1, 2004 # Devils # 1   # Canadiens # 2   # 7:30 PM ET # RDS # RECAP  # sub date_to_timestamp { my($date_text, $time_text) = @_; $date_text =~ /(...) (.[0-9]*), (....)/; my($month) = uc($1); $month = $months{$month}; my($day) = $2; my($year) = $3 - 1900; $time_text =~ /(.[0-9]*):(.[0-9]*) (.)M (..)/; my($hour) = $1; if($3 eq "P") { $hour += 12; } my($min) = $2; my($sec) = 0; my($tzn) = $4; if($tzn eq "ET") { $hour -= 3; } elsif($tzn eq "CT") { $hour -= 2; } elsif($tzn eq "MT") { $hour -= 1; } return mktime($sec, $min, $hour, $day, $month, $year, 0, 0, -1); }