prefer european blizzard api endpoint
[sc2-widget] / getsc2clan
index 23190e48f990e8516ad1c359f67d72f7e1adf636..60cbc1a50d7521153bc662bec71ed1cad89544dd 100755 (executable)
@@ -12,17 +12,16 @@ my ($profileid, $clanmatch) = @ARGV;  # clan host and name
 $profileid and $profileid =~ /\A\d+\z/
        or die "Usage: $0 <profile id> [<clan name>]\n";
 
-my $bliz = LWP::Authen::OAuth2->new(
-       client_id               => '7f0f95ac9529474f854ee8d68a12c3e0',
-       client_secret           => 'Kfa8n98UAaDo4brOeqxe9C2kJE9pqpSd',
-       token_endpoint          => 'https://us.battle.net/oauth/token',
+my %auth = do './.blizzard.passwd.pl' or die "no auth setup: $!\n";
+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');
 
 sub blizget {
        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);
@@ -32,7 +31,10 @@ sub blizget {
 # prefer deprecated interface to prevent costly ladder search
 my $ladderdata = blizget(legacy => profile => 2 => 1 => $profileid => 'ladders');
 my @ladders = (
-       sort { $b->{characters}->@* <=> $a->{characters}->@* } # population desc
+       sort {
+               $b->{ladder}->[0]->{wins}+$b->{ladder}->[0]->{losses} <=>
+               $a->{ladder}->[0]->{wins}+$a->{ladder}->[0]->{losses}
+       } # activity desc
        grep {
                !$clanmatch or
                all { fc $_->{clanName} eq fc $clanmatch } $_->{characters}->@*
@@ -40,8 +42,9 @@ my @ladders = (
        grep { $_->{ladder}->[0]->{division} }
        $ladderdata->{currentSeason}->@*
 ) or die "No matching groups found\n";
-my @members = $ladders[0]->{characters}->@*;
-my %memberidx = map { $members[$_]->{id} => $_ } 0 .. $#members;
+my (@members, %memberidx);
+$memberidx{ $_->{id} } //= push(@members, $_) && $#members
+       for map { $_->{characters}->@* } @ladders;
 
 say JSON->new->canonical->pretty->encode({
        name     => $members[0]->{clanName},
@@ -51,9 +54,11 @@ say JSON->new->canonical->pretty->encode({
                division => $_->{ladder}->[0]->{ladderName},
                rank     => $_->{ladder}->[0]->{rank},
                members  => [map { $memberidx{$_->{id}} } $_->{characters}->@*],
+               wins     => $_->{ladder}->[0]->{wins},
+               losses   => $_->{ladder}->[0]->{losses},
        }} @ladders],
        members  => [map {
                blizget(metadata => profile => 2 => 1 => $_->{id})
                # lacks mmr, fav race (available in new api)
        } @members],
-});
+}) =~ s/(?: \G \d,? | \[ ) \K \s+ (?=\d|\])/ /grx; # concat arrays of single digits