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 if (@ARGV and all { m[/] } @ARGV) {
12 say pp blizget($_) for @ARGV;
16 my ($profiles, $clanmatches) = part { /\D/ } @ARGV; # separate numbers
17 $profiles && @{$profiles}
18 or die "Usage: $0 <profile id>... [<clan name>...]\n";
19 my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches || []};
23 my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n";
24 my $bliz = LWP::Authen::OAuth2->new(%auth,
25 token_endpoint => 'https://eu.battle.net/oauth/token',
26 request_required_params => [qw( client_id client_secret grant_type )],
28 $bliz->request_tokens(grant_type => 'client_credentials');
32 my $args = join('/', @_);
33 my $res = $bliz->get("https://eu.api.blizzard.com/sc2/$args");
34 $res->is_success or die $res->status_line;
35 my $json = $res->decoded_content;
36 return decode_json($json);
39 # prefer deprecated interface to prevent costly ladder search
40 my @ladderdata = map {
41 blizget(legacy => profile => 2 => 1 => $_ => 'ladders')
44 map { $_->{ladder}->[0]->{ladderId} => $_ } # unique
45 grep { $_->{ladder}->[0]->{division} }
46 map { $_->{previousSeason}->@*, $_->{currentSeason}->@* }
51 -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
53 nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order
56 all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*
59 ) or die "No matching groups found\n";
61 my (@members, %memberidx);
62 $memberidx{ $_->{id} } //= push(@members, $_) && $#members
63 for map { $_->{characters}->@* } @ladders;
65 say JSON->new->canonical->pretty->encode({
66 name => $members[0]->{clanName},
67 tag => $members[0]->{clanTag},
69 league => lc $_->{ladder}->[0]->{league},
70 division => $_->{ladder}->[0]->{ladderName},
71 rank => $_->{ladder}->[0]->{rank},
72 members => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
73 wins => $_->{ladder}->[0]->{wins},
74 losses => $_->{ladder}->[0]->{losses},
77 blizget(metadata => profile => 2 => 1 => $_->{id})
78 # lacks mmr, fav race (available in new api)
80 }) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits