X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=lib%2FY%2FAoC%2FUA.pm;h=966878a535f9b2f3dfc2fd528eb7e900ac89d03a;hb=dcb860dd4089c635c9d7414a631ba183d80e013b;hp=ecee1b0721e8adca9d52ece1762e552fce35419d;hpb=78136e93bf5e13aabceb8834ab3748187ee74eee;p=aoc.git diff --git a/lib/Y/AoC/UA.pm b/lib/Y/AoC/UA.pm index ecee1b0..966878a 100644 --- a/lib/Y/AoC/UA.pm +++ b/lib/Y/AoC/UA.pm @@ -5,7 +5,7 @@ use v5.36; use Mojo::Base -signatures; use Mojo::UserAgent; use Mojo::DOM; -use Y::AoC qw(white); +use Y::AoC qw(white red); our $user_agent = 'kas@yenya.net https://www.fi.muni.cz/~kas/git/aoc.git'; our $cache_dir = '/home/kas/aoc/cache'; @@ -19,7 +19,7 @@ sub request($url, $args) { $cachefile = $cache_dir . '/' . $args->{cache_to}; $cache = Mojo::File->new("$cachefile"); - if ($args->{max_age}) { + if (defined $args->{max_age}) { $cache->remove if $cache->stat && time - $cache->stat->mtime @@ -29,7 +29,9 @@ sub request($url, $args) { if ($cache->stat) { say "\n", white('cached'), " response from $cachefile"; - return Mojo::DOM->new($cache->slurp); + return $args->{body} + ? $cache->slurp + : Mojo::DOM->new($cache->slurp); } } @@ -53,7 +55,7 @@ sub request($url, $args) { } if ($cache) { - $cache->spurt($res->body); + $cache->spew($res->body); # say "Stored response to $cachefile"; } return $args->{body} ? $res->body : $res->dom;