define common region/realm request path
[sc2-widget] / getsc2clan
index 6c02c0bd2cec780ece29040d4625665337db277e..02990826636280b35663c791a9d8872c883334c5 100755 (executable)
@@ -6,45 +6,89 @@ use utf8;
 use Data::Dump qw( pp );
 use LWP::Authen::OAuth2;
 use JSON qw( decode_json );
-use List::Util qw( all );
+use List::MoreUtils qw( all part sort_by nsort_by );
 
-my ($profileid, $clanmatch) = @ARGV;  # clan host and name
-$profileid and $profileid =~ /\A\d+\z/
-       or die "Usage: $0 <profile id> [<clan name>]\n";
+if (@ARGV and all { m[/] } @ARGV) {
+       say pp blizget($_) for @ARGV;
+       exit;
+}
 
-my $bliz = LWP::Authen::OAuth2->new(
-       client_id               => '7f0f95ac9529474f854ee8d68a12c3e0',
-       client_secret           => 'Kfa8n98UAaDo4brOeqxe9C2kJE9pqpSd',
-       token_endpoint          => 'https://us.battle.net/oauth/token',
-       request_required_params => [qw( client_id client_secret grant_type )],
-);
-$bliz->request_tokens(grant_type => 'client_credentials');
+my ($profiles, $clanmatches) = part { /\D/ } @ARGV;  # separate numbers
+$profiles && @{$profiles}
+       or die "Usage: $0 <profile id>... [<clan name>...]\n";
+my ($clanmatch) = map { $_ && qr/\A(?:$_)\z/i } join '|', @{$clanmatches || []};
 
+my @realmget = (profile => 2 => 1); # common request path for european data
 sub blizget {
+       state $bliz = do {
+               my @authdata = do './.blizzard.passwd.pl' and not $@ || $!
+                       or die "No auth setup: ", $@ || $!, "\n";
+               my %auth = @authdata;
+               my $bliz = LWP::Authen::OAuth2->new(%auth,
+                       token_endpoint          => 'https://eu.battle.net/oauth/token',
+                       request_required_params => [qw( client_id client_secret grant_type )],
+               );
+               $bliz->request_tokens(grant_type => 'client_credentials');
+               $bliz;
+       };
+
        my $args = join('/', @_);
-       my $res = $bliz->get("https://us.api.blizzard.com/sc2/$args");
+       my $res = $bliz->get("https://eu.api.blizzard.com/sc2/$args");
        $res->is_success or die $res->status_line;
        my $json = $res->decoded_content;
        return decode_json($json);
 }
 
-# find largest group consisting entirely of clan members
 # prefer deprecated interface to prevent costly ladder search
-my $ladders = blizget(legacy => profile => 2 => 1 => $profileid => 'ladders');
-my ($ladder) = (
-       sort { $b->{characters}->@* <=> $a->{characters}->@* } # population desc
+my @ladderdata = map {
+       blizget(legacy => @realmget => $_ => 'ladders')
+} @{$profiles};
+
+# merge relevant ladder data of all users
+my %ladders;
+for my $season (qw[ currentSeason previousSeason ]) {
+       for my $row (map { $_->{$season}->@* } @ladderdata) {
+               $row->{ladder}->[0]->{division} or next;
+               $row->{season} = $season;
+               $ladders{ $row->{ladder}->[0]->{ladderId} } //= $row;
+       }
+}
+
+my @ladders = (
+       nsort_by { $_->{ladder}->[0]->{ladderId} } # stable order
        grep {
                !$clanmatch or
-               all { fc $_->{clanName} eq fc $clanmatch } $_->{characters}->@*
+               all { $_->{clanName} =~ $clanmatch } $_->{characters}->@*
        } # members
-       $ladders->{currentSeason}->@*
+       values %ladders
 ) or die "No matching groups found\n";
 
+my (@members, %memberidx);
+$memberidx{ $_->{id} } //= push(@members, $_) && $#members
+       for map { $_->{characters}->@* } @ladders;
+
 say JSON->new->canonical->pretty->encode({
-       league   => ucfirst lc $ladder->{ladder}->[0]->{league},
-       division => $ladder->{ladder}->[0]->{ladderName},
-       rank     => $ladder->{ladder}->[0]->{rank},
-       tag      => $ladder->{characters}->[0]->{clanTag},
-       members  => [map { blizget(metadata => profile => 2 => 1 => $_->{id}) } $ladder->{characters}->@*],
-       # lacks mmr, fav race (available in new api)
-});
+       name     => $members[0]->{clanName},
+       tag      => $members[0]->{clanTag},
+       ladders  => [
+               map {{
+                       id       => $_->{ladder}->[0]->{ladderId},
+                       league   => lc $_->{ladder}->[0]->{league},
+                       division => $_->{ladder}->[0]->{ladderName},
+                       rank     => $_->{ladder}->[0]->{rank},
+                       members  => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
+                       wins     => $_->{ladder}->[0]->{wins},
+                       losses   => $_->{ladder}->[0]->{losses},
+                       (season  => -1) x ($_->{season} eq 'previousSeason'),
+               }}
+               sort_by { $_->{season} } # season
+               nsort_by {
+                       -($_->{ladder}->[0]->{wins} + $_->{ladder}->[0]->{losses})
+               } # activity desc
+               @ladders
+       ],
+       members  => [map {
+               blizget(metadata => @realmget => $_->{id})
+               # lacks mmr, fav race (available in new api)
+       } @members],
+}) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits