ahmadawais / corona-cli

🦠 Track the Coronavirus disease (COVID-19) in the command line. Worldwide for all countries, for one country, and the US States. Fast response time (< 100ms). https://x.com/MrAhmadAwais
https://NodeCLI.com
MIT License
1.84k stars 190 forks source link

Ability to "cache" today's statistics #112

Open rsubtil opened 4 years ago

rsubtil commented 4 years ago

This is a very interesting project, especially for displaying everyday statistics on terminal initialization by adding it to .bashrc.

However, the program always fetches the data online; thus it always takes some time to load. It would be cool if the program only fetched the current day's statistics and cache them locally: this way, any subsequent calls would run considerably faster (and as far as I know, most countries only update their statistics daily anyway).

rsubtil commented 4 years ago

For anyone interested in this functionality, I've developed a script to do this: (change the top variables to your liking and setup)

#!/bin/sh

maxTime=14400 # 14400 seconds = 4 hours; change this to your desired value
dateFormat="%Hh%Mm%Ss" # Format string to format the remaining time before updating, parsed by `date`
tempFile=/tmp/covidStatus # Path for the locally cached statistics
command="corona-cli portugal -m" # How you run the `corona-cli` command

if [ ! -f $tempFile ]; then
    echo "COVID-19 Statistics: (no cached results; updating...)"
    $command > $tempFile
else
    timeCreated=$(date --utc --reference=$tempFile +%s)
    timeNow=$(date --utc +%s)
    delta=$(($timeNow-$timeCreated))
    if [ $delta -gt $maxTime ]; then
        echo "COVID-19 Statistics: (cached results too old; updating...)"
        $command > $tempFile
    else
        echo "COVID-19 Statistics: (cached results; will update again in $(date --utc --date="@$(($maxTime-$delta))" +$dateFormat))"
    fi
fi

It would still be cool if this was properly implemented in the program, but for now, it does the job :stuck_out_tongue: