#!/usr/bin/perl -w
-use v5.16;
-use Mojo::UserAgent -signatures;
+use v5.36;
+use Y::AoC::UA;
+use Y::AoC;
use Mojo::JSON qw(from_json);
-use Mojo::File qw(curfile);
use Mojo::Util qw(getopt dumper);
use POSIX;
+use Time::Local;
my $board_id = 652041;
my $my_name = 'Yenya';
my $day = (localtime(time))[3];
+my $year = Y::AoC::year;
my $mode = 'r';
my $force;
getopt
'n|name=s' => \$my_name,
'd|day=i' => \$day,
+ 'y|year=i' => \$year,
'1|first-task' => sub { $mode = '1' },
'2|second-task' => sub { $mode = '2' },
'r|relative' => sub { $mode = 'r' },
'b|board=i' => \$board_id,
'f|force' => \$force
or die "Usage: $0 [-n username] [-1|-2|-r] [-b board_id]\n";
-my $cache_filename = "cache-$board_id.json";
-my $cached = curfile->sibling($cache_filename);
-if ($force || !$cached->stat || time - $cached->stat->mtime > 900) {
- warn "Downloading $cache_filename\n";
- chomp(my $cookie = curfile->sibling('cookie')->slurp);
- my $res = Mojo::UserAgent->new->get(
- "https://adventofcode.com/2021/leaderboard/private/view/$board_id.json",
- { Cookie => $cookie }
- )->result;
-
- $cached->spurt($res->body);
-}
-
-my $json = from_json $cached->slurp;
+my $res = Y::AoC::UA::request(
+ "https://adventofcode.com/$year/leaderboard/private/view/$board_id.json",
+ {
+ max_age => $force ? 0 : 900,
+ body => 1,
+ cache_to => "board-$board_id-$year.json",
+ },
+);
+my $json = from_json($res);
my %times;
my $my_time;
+my $min_time;
for my $name (keys %{ $json->{members} }) {
my $member = $json->{members}->{$name};
my $level = $member->{completion_day_level}->{$day}
}
my $id_or_name = $member->{name} // 'Anonymous user #' . $member->{id};
$times{ $id_or_name } = $time;
+ $min_time = $level->{1}->{get_star_ts}
+ if !defined $min_time || $min_time > $level->{1}->{get_star_ts};
$my_time = $time
if $id_or_name eq $my_name;
}
+my @localtime = localtime($min_time);
+$localtime[0] = 0;
+$localtime[1] = 0;
+$localtime[2] = 6;
+my $start = timelocal(@localtime);
+say "Start at ", POSIX::strftime("%Y-%m-%d %H:%M:%S", @localtime);
+
for my $name (sort { $times{$a} <=> $times{$b} } keys %times) {
$my_time //= $times{$name};
- printf("%6d %6d %s\n", $times{$name}, $times{$name} - $my_time, $name);
+ if ($mode eq 'r') {
+ printf("%9d %s\n",
+ $times{$name} - $my_time,
+ $name,
+ );
+ } else {
+ printf("%19.19s %9d %9d %s\n",
+ POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($times{$name})),
+ $times{$name}-$start,
+ $times{$name} - $my_time,
+ $name,
+ );
+ }
}