From 3487c6f92ac31ca9ce13ceba78ad63454edd7636 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Wed, 21 Dec 2022 09:07:56 +0100 Subject: [PATCH] Infrastructure cleanups --- .gitignore | 6 ++-- RESOURCES.otl | 17 ++++++++++ 2022/get => get | 2 +- 2021/leaderboard => leaderboard | 56 ++++++++++++++++++++++----------- lib/Y/AoC.pm | 4 +++ 5 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 RESOURCES.otl rename 2022/get => get (98%) rename 2021/leaderboard => leaderboard (57%) diff --git a/.gitignore b/.gitignore index 68be32c..ab99345 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ .*.swp *.txt -cache-*.json -cookie -id.txt +cache a.out -leaderboard +*/backup diff --git a/RESOURCES.otl b/RESOURCES.otl new file mode 100644 index 0000000..8da38bd --- /dev/null +++ b/RESOURCES.otl @@ -0,0 +1,17 @@ +Eric Wastl's talk + https://www.youtube.com/watch?v=gibVyxpi-qA + +mcpowers' tips + https://gist.github.com/mcpower/87427528b9ba5cac6f0c679370789661 + +epsalon's Perl repo + https://github.com/epsalon/advent_of_code + +betaveros' howto + https://blog.vero.site/post/advent-leaderboard + https://www.youtube.com/watch?v=jls4zhEhlog + +Vim: + increment a visual block + visual-block g Ctrl-A (or Ctrl-X decrements) + diff --git a/2022/get b/get similarity index 98% rename from 2022/get rename to get index 760e024..13a39df 100755 --- a/2022/get +++ b/get @@ -3,7 +3,7 @@ use v5.36; use strict; -use Y::AoC qw(grey); +use Y::AoC qw(grey red); use Y::AoC::UA; use Mojo::UserAgent -signatures; diff --git a/2021/leaderboard b/leaderboard similarity index 57% rename from 2021/leaderboard rename to leaderboard index 42874cd..3f1e7ba 100755 --- a/2021/leaderboard +++ b/leaderboard @@ -1,45 +1,44 @@ #!/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} @@ -57,11 +56,32 @@ for my $name (keys %{ $json->{members} }) { } 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, + ); + } } diff --git a/lib/Y/AoC.pm b/lib/Y/AoC.pm index 8fbb606..31b4532 100644 --- a/lib/Y/AoC.pm +++ b/lib/Y/AoC.pm @@ -7,6 +7,7 @@ our @EXPORT_OK = qw(red grey white yellow day year); use FindBin qw($Bin); use Term::ANSIColor; +use Cwd; sub red { colored(['bright_red on_black'], @_); } sub white { colored(['bright_white on_black'], @_); } @@ -20,6 +21,9 @@ sub day { sub year { my ($num) = $Bin =~ /.*(\d{4})/; + if (!defined $num) { + ($num) = getcwd =~ /.*(\d{4})/; + } $num; } -- 2.43.5