X-Git-Url: http://git.shiar.nl/sc2-widget/blobdiff_plain/8c443d69c70317d0a47cd980da764dedd3b98380..7f3d75fcd1ed43c99397a99bda9c14f18b7e068c:/getsc2clan diff --git a/getsc2clan b/getsc2clan index 60cbc1a..1fbdd02 100755 --- a/getsc2clan +++ b/getsc2clan @@ -6,20 +6,31 @@ use utf8; use Data::Dump qw( pp ); use LWP::Authen::OAuth2; use JSON qw( decode_json ); -use List::Util qw( all ); +use List::MoreUtils qw( all part sort_by nsort_by ); -my ($profileid, $clanmatch) = @ARGV; # clan host and name -$profileid and $profileid =~ /\A\d+\z/ - or die "Usage: $0 []\n"; +if (@ARGV and all { m[/] } @ARGV) { + say pp blizget($_) for @ARGV; + exit; +} -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 ($profiles, $clanmatches) = part { /\D/ } @ARGV; # separate numbers +$profiles && @{$profiles} + or die "Usage: $0 ... [...]\n"; +my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches || []}; sub blizget { + state $bliz = do { + my @authdata = do './.blizzard.passwd.pl' and not $@ || $! + or die "No auth setup: ", $@ || $!, "\n"; + my %auth = @authdata; + 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; @@ -27,21 +38,34 @@ sub blizget { return decode_json($json); } -# find largest group consisting entirely of clan members # prefer deprecated interface to prevent costly ladder search -my $ladderdata = blizget(legacy => profile => 2 => 1 => $profileid => 'ladders'); +my @ladderdata = map { + blizget(legacy => profile => 2 => 1 => $_ => 'ladders') +} @{$profiles}; + +# merge relevant ladder data of all users +my %ladders; +for my $season (qw[ currentSeason previousSeason ]) { + for my $row (map { $_->{$season}->@* } @ladderdata) { + $row->{ladder}->[0]->{division} or next; + $row->{season} = $season; + $ladders{ $row->{ladder}->[0]->{ladderId} } //= $row; + } +} + my @ladders = ( - sort { - $b->{ladder}->[0]->{wins}+$b->{ladder}->[0]->{losses} <=> - $a->{ladder}->[0]->{wins}+$a->{ladder}->[0]->{losses} + sort_by { $_->{season} } # season + nsort_by { + -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses}) } # activity desc + nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order grep { !$clanmatch or - all { fc $_->{clanName} eq fc $clanmatch } $_->{characters}->@* + all { $_->{clanName} =~ $clanmatch } $_->{characters}->@* } # members - grep { $_->{ladder}->[0]->{division} } - $ladderdata->{currentSeason}->@* + values %ladders ) or die "No matching groups found\n"; + my (@members, %memberidx); $memberidx{ $_->{id} } //= push(@members, $_) && $#members for map { $_->{characters}->@* } @ladders; @@ -56,6 +80,7 @@ say JSON->new->canonical->pretty->encode({ members => [map { $memberidx{$_->{id}} } $_->{characters}->@*], wins => $_->{ladder}->[0]->{wins}, losses => $_->{ladder}->[0]->{losses}, + (season => -1) x ($_->{season} eq 'previousSeason'), }} @ladders], members => [map { blizget(metadata => profile => 2 => 1 => $_->{id})