4 use Mojo::UserAgent -signatures;
5 use Mojo::JSON qw(from_json);
6 use Mojo::File qw(curfile);
7 use Mojo::Util qw(getopt dumper);
10 my $board_id = 652041;
11 my $my_name = 'Yenya';
12 my $day = (localtime(time))[3];
14 my $cached = curfile->sibling('cache.json');
16 'n|name=s' => \$my_name,
18 '1|first-task' => sub { $mode = '1' },
19 '2|second-task' => sub { $mode = '2' },
20 'r|relative' => sub { $mode = 'r' },
21 'b|board=i' => \$board_id
22 or die "Usage: $0 [-n username] [-1|-2|-r] [-b board_id]\n";
24 if (!$cached->stat || time - $cached->stat->mtime > 900) {
25 warn "Downloading cache.json\n";
26 chomp(my $cookie = curfile->sibling('cookie')->slurp);
27 my $res = Mojo::UserAgent->new->get(
28 "https://adventofcode.com/2021/leaderboard/private/view/$board_id.json",
32 $cached->spurt($res->body);
35 my $json = from_json $cached->slurp;
40 for my $name (keys %{ $json->{members} }) {
41 my $member = $json->{members}->{$name};
42 my $level = $member->{completion_day_level}->{$day}
47 $time = $level->{2}->{get_star_ts} - $level->{1}->{get_star_ts};
48 } elsif ($mode eq '1') {
50 $time = $level->{1}->{get_star_ts};
51 } elsif ($mode eq '2') {
53 $time = $level->{2}->{get_star_ts};
55 my $id_or_name = $member->{name} // 'Anonymous user #' . $member->{id};
56 $times{ $id_or_name } = $time;
58 if $id_or_name eq $my_name;
61 for my $name (sort { $times{$a} <=> $times{$b} } keys %times) {
62 $my_time //= $times{$name};
63 printf("%6d %6d %s\n", $times{$name}, $times{$name} - $my_time, $name);