debug api requests given on command line
[sc2-widget] / getsc2clan
1 #!/usr/bin/env perl
2 use 5.024;
3 use warnings;
4 use utf8;
5
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 );
10
11 if (@ARGV and all { m[/] } @ARGV) {
12         say pp blizget($_) for @ARGV;
13         exit;
14 }
15
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};
20
21 sub blizget {
22         state $bliz = do {
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 )],
27                 );
28                 $bliz->request_tokens(grant_type => 'client_credentials');
29                 $bliz;
30         };
31
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);
37 }
38
39 # prefer deprecated interface to prevent costly ladder search
40 my @ladderdata = map {
41         blizget(legacy => profile => 2 => 1 => $_ => 'ladders')
42 } @{$profiles};
43 my %ladders = (
44         map { $_->{ladder}->[0]->{ladderId} => $_ } # unique
45         grep { $_->{ladder}->[0]->{division} }
46         map { $_->{currentSeason}->@* } @ladderdata
47 );
48 my @ladders = (
49         nsort_by {
50                 -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
51         } # activity desc
52         nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order
53         grep {
54                 !$clanmatch or
55                 all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*
56         } # members
57         values %ladders
58 ) or die "No matching groups found\n";
59
60 my (@members, %memberidx);
61 $memberidx{ $_->{id} } //= push(@members, $_) && $#members
62         for map { $_->{characters}->@* } @ladders;
63
64 say JSON->new->canonical->pretty->encode({
65         name     => $members[0]->{clanName},
66         tag      => $members[0]->{clanTag},
67         ladders  => [map {{
68                 league   => lc $_->{ladder}->[0]->{league},
69                 division => $_->{ladder}->[0]->{ladderName},
70                 rank     => $_->{ladder}->[0]->{rank},
71                 members  => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
72                 wins     => $_->{ladder}->[0]->{wins},
73                 losses   => $_->{ladder}->[0]->{losses},
74         }} @ladders],
75         members  => [map {
76                 blizget(metadata => profile => 2 => 1 => $_->{id})
77                 # lacks mmr, fav race (available in new api)
78         } @members],
79 }) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits