6 use Data::Dump qw( pp );
7 use LWP::Authen::OAuth2;
8 use JSON qw( decode_json );
9 use List::MoreUtils qw( all part nsort_by );
11 my ($profiles, $clanmatches) = part { /\D/ } @ARGV; # separate numbers
13 or die "Usage: $0 <profile id>... [<clan name>...]\n";
14 my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches};
18 my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n";
19 my $bliz = LWP::Authen::OAuth2->new(%auth,
20 token_endpoint => 'https://eu.battle.net/oauth/token',
21 request_required_params => [qw( client_id client_secret grant_type )],
23 $bliz->request_tokens(grant_type => 'client_credentials');
27 my $args = join('/', @_);
28 my $res = $bliz->get("https://eu.api.blizzard.com/sc2/$args");
29 $res->is_success or die $res->status_line;
30 my $json = $res->decoded_content;
31 return decode_json($json);
34 # prefer deprecated interface to prevent costly ladder search
35 my @ladderdata = map {
36 blizget(legacy => profile => 2 => 1 => $_ => 'ladders')
39 map { $_->{ladder}->[0]->{ladderId} => $_ } # unique
40 grep { $_->{ladder}->[0]->{division} }
41 map { $_->{currentSeason}->@* } @ladderdata
45 -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
47 nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order
50 all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*
53 ) or die "No matching groups found\n";
55 my (@members, %memberidx);
56 $memberidx{ $_->{id} } //= push(@members, $_) && $#members
57 for map { $_->{characters}->@* } @ladders;
59 say JSON->new->canonical->pretty->encode({
60 name => $members[0]->{clanName},
61 tag => $members[0]->{clanTag},
63 league => lc $_->{ladder}->[0]->{league},
64 division => $_->{ladder}->[0]->{ladderName},
65 rank => $_->{ladder}->[0]->{rank},
66 members => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
67 wins => $_->{ladder}->[0]->{wins},
68 losses => $_->{ladder}->[0]->{losses},
71 blizget(metadata => profile => 2 => 1 => $_->{id})
72 # lacks mmr, fav race (available in new api)
74 }) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits