X-Git-Url: http://git.shiar.nl/sc2-widget/blobdiff_plain/255c21f32654ecc9a3028d59b2e5473f54bf594b..f514022942c074d55501f5a181ad809c3ab349d1:/getsc2clan diff --git a/getsc2clan b/getsc2clan index 6e98d60..3285535 100755 --- a/getsc2clan +++ b/getsc2clan @@ -6,21 +6,29 @@ use utf8; use Data::Dump qw( pp ); use LWP::Authen::OAuth2; use JSON qw( decode_json ); -use List::MoreUtils qw( all part ); +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'); - 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; @@ -38,10 +46,10 @@ my %ladders = ( map { $_->{currentSeason}->@* } @ladderdata ); my @ladders = ( - sort { - $b->{ladder}->[0]->{wins}+$b->{ladder}->[0]->{losses} <=> - $a->{ladder}->[0]->{wins}+$a->{ladder}->[0]->{losses} + nsort_by { + -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses}) } # activity desc + nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order grep { !$clanmatch or all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*