-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
243 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use warnings; | ||
use strict; | ||
|
||
use Data::Dumper; | ||
use DateTime::Format::ISO8601; | ||
use IPC::Shareable; | ||
use NHL::API; | ||
use POSIX qw(strftime); | ||
|
||
tie my $sent_cache, 'IPC::Shareable', { | ||
key => 'NHL GAME ALERTS SENT', | ||
create => 1 | ||
}; | ||
|
||
#_cache_flush(); | ||
|
||
my $nhl = NHL::API->new; | ||
|
||
my @alert_times = qw(15 60); | ||
|
||
@alert_times = sort { $b <=> $a } @alert_times; | ||
|
||
my $date = strftime("%Y-%m-%d", localtime); | ||
|
||
my @teams = ( | ||
"Toronto Maple Leafs", | ||
"Edmonton Oilers", | ||
"Buffalo Sabres", | ||
); | ||
|
||
#print Dumper $sent_cache; | ||
|
||
for my $team (@teams) { | ||
|
||
my $game_time = $nhl->game_time($team); | ||
next if ! $game_time; | ||
|
||
my $dt_now = DateTime->now; | ||
my $dt_game = DateTime::Format::ISO8601->parse_datetime($game_time); | ||
|
||
if ($dt_game > $dt_now) { | ||
my $until_game_dt = $dt_game - $dt_now; | ||
my $minutes_to_game = $until_game_dt->in_units('minutes'); | ||
|
||
for my $alert_time (@alert_times) { | ||
if ($minutes_to_game < $alert_time) { | ||
if (! alert_sent($team, $date, $alert_time)) { | ||
print "$team is playing in $alert_time minutes\n"; | ||
_cache_set($team, $date, $alert_time); | ||
last; | ||
#print Dumper $sent_cache; | ||
#_cache_reset($team, $date, $alert_time); | ||
#_cache_flush(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
sub alert_sent { | ||
my ($team, $date, $alert_time) = @_; | ||
|
||
my $team_cache = _cache_get($team); | ||
|
||
my $sent = 0; | ||
|
||
if (exists $team_cache->{$alert_time}) { | ||
my $alert_cache = $team_cache->{$alert_time}; | ||
|
||
if (keys %$alert_cache && $alert_cache->{date} eq $date) { | ||
$sent = $alert_cache->{sent}; | ||
} | ||
} | ||
|
||
return $sent; | ||
} | ||
|
||
sub _cache_get { | ||
my ($team) = @_; | ||
return $sent_cache->{$team}; | ||
} | ||
sub _cache_set { | ||
my ($team, $date, $alert_time) = @_; | ||
|
||
$sent_cache->{$team}{$alert_time} = { | ||
date => $date, | ||
sent => 1 | ||
}; | ||
} | ||
sub _cache_reset { | ||
my ($team, $date, $alert_time) = @_; | ||
$sent_cache->{$team}{$alert_time} = { | ||
date => $date, | ||
sent => 0 | ||
}; | ||
} | ||
sub _cache_flush { | ||
$sent_cache = {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use warnings; | ||
use strict; | ||
|
||
use Data::Dumper; | ||
use NHL::API; | ||
use Test::More; | ||
|
||
my $api = NHL::API->new; | ||
|
||
my $teams = $api->teams; | ||
|
||
print Dumper $teams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use warnings; | ||
use strict; | ||
|
||
use Data::Dumper; | ||
use NHL::API; | ||
use Test::More; | ||
|
||
my $api = NHL::API->new; | ||
|
||
my $name = 'Toronto Maple Leafs'; | ||
is $api->team_id($name), 10, "$name has ID 10 ok"; | ||
|
||
$name = 'Edmonton Oilers'; | ||
is $api->team_id($name), 22, "$name has ID 22 ok"; | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use warnings; | ||
use strict; | ||
|
||
use Data::Dumper; | ||
use NHL::API; | ||
use Test::More; | ||
|
||
my $api = NHL::API->new; | ||
|
||
my $name = 'Buffalo Sabres'; | ||
|
||
my $next_game = $api->game_time($name); | ||
|
||
print Dumper $next_game; |