Skip to content

Commit

Permalink
Initial game alert script code
Browse files Browse the repository at this point in the history
  • Loading branch information
stevieb9 committed Oct 23, 2023
1 parent e0237b6 commit b2101fc
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 25 deletions.
99 changes: 99 additions & 0 deletions bin/game_alert.pl
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 = {};
}
127 changes: 102 additions & 25 deletions lib/NHL/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,127 @@ package NHL::API;
use strict;
use warnings;

use Carp qw(croak);
use Data::Dumper;
use DateTime;
use DateTime::Format::ISO8601;
use HTTP::Request;
use JSON;
use LWP::UserAgent;

our $VERSION = '0.01';

sub __placeholder {}
my $ua = LWP::UserAgent->new;
my $url = 'https://statsapi.web.nhl.com/api/v1/';

sub new {
my ($class, %args) = @_;

my $self = bless {}, $class;

$self->_args(\%args);

return $self;
}
sub fetch {
my ($self, $want_uri, $params) = @_;

if (! $want_uri) {
croak "fetch() requires a valid category...";
}

my ($req, $response);

1;
__END__
$req = HTTP::Request->new('GET', $self->_uri($want_uri, $params));
$response = $ua->request($req);

=head1 NAME
if ($response->is_success) {
my $json;

NHL::API - One line description
$json = $response->decoded_content;
my $result = decode_json $json;

=for html
<a href="https://github.com/stevieb9/nhl-api/actions"><img src="https://github.com/stevieb9/nhl-api/workflows/CI/badge.svg"/></a>
<a href='https://coveralls.io/github/stevieb9/nhl-api?branch=main'><img src='https://coveralls.io/repos/stevieb9/nhl-api/badge.svg?branch=main&service=github' alt='Coverage Status' /></a>
return $result;
}
else {
print "Invalid response\n\n";
return undef;
}
}
sub teams {
my ($self) = @_;

my $data = $self->fetch('teams');

=head1 SYNOPSIS
return $data->{teams};
}
sub team_id {
my ($self, $team_name) = @_;

=head1 DESCRIPTION
if (! $team_name) {
croak "team_id() requires a Team Name sent in...";
}

=head1 METHODS
my $teams = $self->teams;

=head2 name
my $team_id;

Description.
for my $team (@$teams) {
if ($team->{name} eq $team_name) {
$team_id = $team->{id};
last;
}
}

I<Parameters>:
return $team_id;
}
sub game_time {
my ($self, $team_name) = @_;

$bar
my $team_id = $self->team_id($team_name);

I<Mandatory, String>: The name of the thing with the guy and the place.
my $games = $self->fetch('schedule')->{dates}[0]{games};

I<Returns>: C<0> upon success.
my $game_time;

=head1 AUTHOR
for my $game (@$games) {
my $home_team_id = $game->{teams}{home}{team}{id};
my $away_team_id = $game->{teams}{away}{team}{id};

Steve Bertrand, C<< <steveb at cpan.org> >>
if ($home_team_id == $team_id || $away_team_id == $team_id) {
$game_time = $game->{gameDate};
}
}

=head1 LICENSE AND COPYRIGHT
if ($game_time) {
my $dt = DateTime::Format::ISO8601->parse_datetime($game_time);
$dt->set_time_zone('America/Vancouver');
}

Copyright 2023 Steve Bertrand.
return $game_time;
}

This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
sub _args {

}
sub _uri {
my ($self, $want_uri) = @_;

my %uris = (
schedule => 'schedule',
teams => 'teams',
team_id => 'team_id',
);

if (! exists $uris{$want_uri}) {
croak "'$want_uri' isn't a valid URI category...";
}

my $uri = $url . $uris{$want_uri};

return $uri;
}

sub __placeholder {}

L<http://www.perlfoundation.org/artistic_license_2_0>
1;
12 changes: 12 additions & 0 deletions t/05-teams.t
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;
16 changes: 16 additions & 0 deletions t/10-team_id.t
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();
14 changes: 14 additions & 0 deletions t/15-next_game.t
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;

0 comments on commit b2101fc

Please sign in to comment.