clan match using legacy ladder interface
[sc2-widget] / widget.pl
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::Util qw( all );
10
11 my $profileid = 2138280;
12 my $clanmatch = 'Inno';
13
14 my $bliz = LWP::Authen::OAuth2->new(
15         client_id               => '7f0f95ac9529474f854ee8d68a12c3e0',
16         client_secret           => 'Kfa8n98UAaDo4brOeqxe9C2kJE9pqpSd',
17         token_endpoint          => 'https://us.battle.net/oauth/token',
18         request_required_params => [qw( client_id client_secret grant_type scope )],
19 );
20 $bliz->request_tokens(
21         scope => 'account.public',
22         grant_type => 'client_credentials',
23 );
24
25 sub blizget {
26         my $args = join('/', @_);
27         my $res = $bliz->get("https://us.api.blizzard.com/sc2/$args");
28         $res->is_success or die $res->status_line;
29         my $json = $res->decoded_content;
30         return decode_json($json);
31 }
32
33 # find largest group consisting entirely of clan members
34 # prefer deprecated interface to prevent costly ladder search
35 my $ladders = blizget(legacy => profile => 2 => 1 => $profileid => 'ladders');
36 my ($ladder) = (
37         sort { $b->{characters}->@* <=> $a->{characters}->@* } # population desc
38         grep { all { fc $_->{clanName} eq fc $clanmatch } $_->{characters}->@* } # members
39         $ladders->{currentSeason}->@*
40 );
41
42 say JSON->new->canonical->pretty->encode({
43         league   => ucfirst lc $ladder->{ladder}->[0]->{league},
44         division => $ladder->{ladder}->[0]->{ladderName},
45         rank     => $ladder->{ladder}->[0]->{rank},
46         tag      => $ladder->{characters}->[0]->{clanTag},
47         members  => [map { blizget(metadata => profile => 2 => 1 => $_->{id}) } $ladder->{characters}->@*],
48         # lacks mmr, fav race (available in new api)
49 });