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
#!/usr/bin/env perl | |
use v5.28; | |
use warnings; | |
use experimental qw(signatures); | |
use Data::Printer; | |
use Encode qw(encode); | |
use Getopt::Long::Descriptive; | |
use Interpolation E => 'eval'; | |
use Mojo::JSON qw(decode_json encode_json); | |
use Mojo::UserAgent; | |
use Path::Tiny; | |
my ($opt, $usage) = describe_options( | |
'%c %o', | |
['debug|d', "print extra stuff"], | |
['domain=s', 'the api to connect to', {default => 'https://statsapi.web.nhl.com'}], | |
['help|h', "print usage message and exit", {shortcircuit => 1}], | |
['refresh', "read from api and write to files"], | |
['verbose|v', "print extra stuff"], | |
); | |
die $usage->text if $opt->help; | |
my $ua = Mojo::UserAgent->new; | |
my $people = get_people($ua, $opt); | |
p $people if $opt->verbose; | |
sub get_people($ua, $opt) { | |
my $path = path 'people.json'; | |
if ($path->is_file and not $opt->refresh) { | |
warn "Getting people from the file '$path'\n" if $opt->debug; | |
my $json = $path->slurp; | |
my $people = decode_json $json; | |
return $people; | |
} | |
warn "Getting people from the API\n" if $opt->debug; | |
my $rosters = get_rosters($ua, $opt); | |
my %people; | |
for my $team (keys $rosters->%*) { | |
for my $player (keys $rosters->{$team}->%*) { | |
my $url = Mojo::URL->new("$E{$opt->domain}/api/v1/people/$rosters->{$team}{$player}"); | |
my $json = $ua->get($url)->result->json; | |
my %person; | |
for my $person ($json->{people}->@*) { | |
$person{lastName} = encode('UTF-8', $person->{lastName}); | |
$person{nationality} = $person->{nationality}; | |
} | |
$people{$team}{$player} = \%person; | |
} | |
} | |
$path->spew(encode_json \%people); | |
return \%people; | |
} | |
sub get_rosters($ua, $opt) { | |
my $path = path 'rosters.json'; | |
if ($path->is_file and not $opt->refresh) { | |
warn "Getting rosters from the file '$path'\n" if $opt->debug; | |
my $json = $path->slurp; | |
my $rosters = decode_json $json; | |
return $rosters; | |
} | |
warn "Getting rosters from the API\n" if $opt->debug; | |
my $teams = get_teams($ua, $opt); | |
my %rosters; | |
for my $team (keys $teams->%*) { | |
my $url = Mojo::URL->new("$E{$opt->domain}/api/v1/teams/$teams->{$team}/roster"); | |
my $json = $ua->get($url)->result->json; | |
my %roster = map { | |
encode('UTF-8', $_->{person}{fullName}) => $_->{person}{id} | |
} $json->{roster}->@*; | |
$rosters{$team} = \%roster; | |
} | |
$path->spew(encode_json \%rosters); | |
return \%rosters; | |
} | |
sub get_teams($ua, $opt) { | |
my $path = path 'teams.json'; | |
if ($path->is_file and not $opt->refresh) { | |
warn "Getting teams from the file '$path'\n" if $opt->debug; | |
my $json = $path->slurp; | |
my $teams = decode_json $json; | |
return $teams; | |
} | |
warn "Getting teams from the API\n" if $opt->debug; | |
my $url = Mojo::URL->new("$E{$opt->domain}/api/v1/teams"); | |
my $json = $ua->get($url)->result->json; | |
my %teams = map {$_->{abbreviation} => $_->{id}} $json->{teams}->@*; | |
$path->spew(encode_json \%teams); | |
return \%teams; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment