cc38e4d9c1da9f84bd0c8cda0122421b61018fb9
[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 @authdata = do './.blizzard.passwd.pl' and not $@ || $!
24                         or die "No auth setup: ", $@ || $!, "\n";
25                 my %auth = @authdata;
26                 my $bliz = LWP::Authen::OAuth2->new(%auth,
27                         token_endpoint          => 'https://eu.battle.net/oauth/token',
28                         request_required_params => [qw( client_id client_secret grant_type )],
29                 );
30                 $bliz->request_tokens(grant_type => 'client_credentials');
31                 $bliz;
32         };
33
34         my $args = join('/', @_);
35         my $res = $bliz->get("https://eu.api.blizzard.com/sc2/$args");
36         $res->is_success or die $res->status_line;
37         my $json = $res->decoded_content;
38         return decode_json($json);
39 }
40
41 # prefer deprecated interface to prevent costly ladder search
42 my @ladderdata = map {
43         blizget(legacy => profile => 2 => 1 => $_ => 'ladders')
44 } @{$profiles};
45
46 # merge relevant ladder data of all users
47 my %ladders;
48 for my $season (qw[ currentSeason previousSeason ]) {
49         for my $row (map { $_->{$season}->@* } @ladderdata) {
50                 $row->{ladder}->[0]->{division} or next;
51                 $row->{season} = $season;
52                 $ladders{ $row->{ladder}->[0]->{ladderId} } //= $row;
53         }
54 }
55
56 my @ladders = (
57         nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order
58         grep {
59                 !$clanmatch or
60                 all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*
61         } # members
62         values %ladders
63 ) or die "No matching groups found\n";
64
65 my (@members, %memberidx);
66 $memberidx{ $_->{id} } //= push(@members, $_) && $#members
67         for map { $_->{characters}->@* } @ladders;
68
69 say JSON->new->canonical->pretty->encode({
70         name     => $members[0]->{clanName},
71         tag      => $members[0]->{clanTag},
72         ladders  => [
73                 map {{
74                         id       => $_->{ladder}->[0]->{ladderId},
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                 }}
83                 sort_by { $_->{season} } # season
84                 nsort_by {
85                         -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
86                 } # activity desc
87                 @ladders
88         ],
89         members  => [map {
90                 blizget(metadata => profile => 2 => 1 => $_->{id})
91                 # lacks mmr, fav race (available in new api)
92         } @members],
93 }) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits