From e4519b936b779b4534179430c977ebdc627b9e9a Mon Sep 17 00:00:00 2001 From: Andinus Date: Sun, 14 Jun 2020 03:26:58 +0530 Subject: Fix file fetching logic, use mtime, switch to HTTP::Simple The issue with ctime is that it doesn't change & stays the same as when the file was actually created, this means the code was running the if block basically everytime after 8 minutes of file creation. And I couldn't use mtime because HTTP::Tiny's mirror method stores mtime as 'Last Modified' header. This switch to HTTP::Simple will download the file everytime which means the mtime changes & we can make the program not look for file every time. However this also means that we are downloading the whole file everytime instead of only when it changes. Meh... It's okay because the alternative is to write a complicated logic. --- ara.pl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ara.pl b/ara.pl index ab32599..3f59efa 100755 --- a/ara.pl +++ b/ara.pl @@ -98,12 +98,12 @@ foreach my $path ( sort keys %unveil ) { } my $file = "$cache_dir/ara.json"; -my $file_ctime; +my $file_mtime; # If $file exists then get ctime. if ( -e $file ) { my $file_stat = path($file)->stat; - $file_ctime = Time::Moment->from_epoch( $file_stat->ctime ); + $file_mtime = Time::Moment->from_epoch( $file_stat->mtime ); } else { warn "File '$file' doesn't exist\nFetching latest...\n" if $use_local_file; @@ -112,22 +112,23 @@ if ( -e $file ) { # Fetch latest data only if the local data is older than 8 minutes or # if the file doesn't exist. if ( not -e $file - or $file_ctime < Time::Moment->now_utc->minus_minutes(8) + or $file_mtime < Time::Moment->now_utc->minus_minutes(8) or $get_latest ) { - require HTTP::Tiny; + require HTTP::Simple; + + no warnings 'once'; + $HTTP::Simple::UA->verify_SSL(1); # Fetch latest data from api. my $url = 'https://api.covid19india.org/data.json'; - my $response = HTTP::Tiny - ->new( verify_SSL => 1 ) - ->mirror($url, $file); + my $response = HTTP::Simple::getstore($url, $file); die "Failed to fetch latest data... Reason: $response->{reason}\n Content: $response->{content} Status: $response->{status}\n" - unless $response->{success}; + unless HTTP::Simple::is_success($response); } # Slurp api response to $file_data. -- cgit 1.4.1-2-gfad0