X-Git-Url: http://git.shiar.nl/sc2-widget/blobdiff_plain/6f8ef036acc8859b467c34e77a0a9050bf69e79b..60d357554684c38bfe547a601ce20013b1b5b693:/getsc2clan diff --git a/getsc2clan b/getsc2clan index 3a6e237..94ca435 100755 --- a/getsc2clan +++ b/getsc2clan @@ -8,19 +8,27 @@ use LWP::Authen::OAuth2; use JSON qw( decode_json ); use List::MoreUtils qw( all part nsort_by ); +if (@ARGV and all { m[/] } @ARGV) { + say pp blizget($_) for @ARGV; + exit; +} + my ($profiles, $clanmatches) = part { /\D/ } @ARGV; # separate numbers -@{$profiles} +$profiles && @{$profiles} or die "Usage: $0 ... [...]\n"; -my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches}; - -my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n"; -my $bliz = LWP::Authen::OAuth2->new(%auth, - token_endpoint => 'https://eu.battle.net/oauth/token', - request_required_params => [qw( client_id client_secret grant_type )], -); -$bliz->request_tokens(grant_type => 'client_credentials'); +my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches || []}; sub blizget { + state $bliz = do { + my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n"; + my $bliz = LWP::Authen::OAuth2->new(%auth, + token_endpoint => 'https://eu.battle.net/oauth/token', + request_required_params => [qw( client_id client_secret grant_type )], + ); + $bliz->request_tokens(grant_type => 'client_credentials'); + $bliz; + }; + my $args = join('/', @_); my $res = $bliz->get("https://eu.api.blizzard.com/sc2/$args"); $res->is_success or die $res->status_line; @@ -35,7 +43,8 @@ my @ladderdata = map { my %ladders = ( map { $_->{ladder}->[0]->{ladderId} => $_ } # unique grep { $_->{ladder}->[0]->{division} } - map { $_->{currentSeason}->@* } @ladderdata + map { $_->{previousSeason}->@*, $_->{currentSeason}->@* } + @ladderdata ); my @ladders = ( nsort_by {