clan match from command line arguments
authorMischa POSLAWSKY <perl@shiar.org>
Tue, 30 Apr 2019 21:05:32 +0000 (23:05 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 30 Apr 2019 21:06:45 +0000 (23:06 +0200)
widget.pl

index 8f118d51ff325dc377b5dabcba39b9a278a9eea0..6c02c0bd2cec780ece29040d4625665337db277e 100644 (file)
--- a/widget.pl
+++ b/widget.pl
@@ -8,8 +8,9 @@ use LWP::Authen::OAuth2;
 use JSON qw( decode_json );
 use List::Util qw( all );
 
-my $profileid = 2138280;
-my $clanmatch = 'Inno';
+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',
@@ -32,9 +33,12 @@ sub blizget {
 my $ladders = blizget(legacy => profile => 2 => 1 => $profileid => 'ladders');
 my ($ladder) = (
        sort { $b->{characters}->@* <=> $a->{characters}->@* } # population desc
-       grep { all { fc $_->{clanName} eq fc $clanmatch } $_->{characters}->@* } # members
+       grep {
+               !$clanmatch or
+               all { fc $_->{clanName} eq fc $clanmatch } $_->{characters}->@*
+       } # members
        $ladders->{currentSeason}->@*
-);
+) or die "No matching groups found\n";
 
 say JSON->new->canonical->pretty->encode({
        league   => ucfirst lc $ladder->{ladder}->[0]->{league},