previous season ladders indicated after active results
[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 sort_by 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
44 # merge relevant ladder data of all users
45 my %ladders;
46 for my $season (qw[ currentSeason previousSeason ]) {
47         for my $row (map { $_->{$season}->@* } @ladderdata) {
48                 $row->{ladder}->[0]->{division} or next;
49                 $row->{season} = $season;
50                 $ladders{ $row->{ladder}->[0]->{ladderId} } //= $row;
51         }
52 }
53
54 my @ladders = (
55         sort_by { $_->{season} } # season
56         nsort_by {
57                 -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
58         } # activity desc
59         nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order
60         grep {
61                 !$clanmatch or
62                 all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*
63         } # members
64         values %ladders
65 ) or die "No matching groups found\n";
66
67 my (@members, %memberidx);
68 $memberidx{ $_->{id} } //= push(@members, $_) && $#members
69         for map { $_->{characters}->@* } @ladders;
70
71 say JSON->new->canonical->pretty->encode({
72         name     => $members[0]->{clanName},
73         tag      => $members[0]->{clanTag},
74         ladders  => [map {{
75                 league   => lc $_->{ladder}->[0]->{league},
76                 division => $_->{ladder}->[0]->{ladderName},
77                 rank     => $_->{ladder}->[0]->{rank},
78                 members  => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
79                 wins     => $_->{ladder}->[0]->{wins},
80                 losses   => $_->{ladder}->[0]->{losses},
81                 (season  => -1) x ($_->{season} eq 'previousSeason'),
82         }} @ladders],
83         members  => [map {
84                 blizget(metadata => profile => 2 => 1 => $_->{id})
85                 # lacks mmr, fav race (available in new api)
86         } @members],
87 }) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits