]> www.fi.muni.cz Git - aoc.git/blobdiff - lib/Y/AoC/UA.pm
Input downloader: use spew() instead of spurt()
[aoc.git] / lib / Y / AoC / UA.pm
index ecee1b0721e8adca9d52ece1762e552fce35419d..966878a535f9b2f3dfc2fd528eb7e900ac89d03a 100644 (file)
@@ -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;