This repository has been archived on 2022-06-10. You can view files and clone it, but cannot push or open issues/pull-requests.
cv/cv

128 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Variable declarations go here.
w=17
dfmt="+%Y-%m-%d"
data="$HOME/Documents/cv"
country=$1
[ ! -d "$data" ] && mkdir -p "$data"
[ -z $date ] && date=$(date $dfmt)
[ -z $ydate ] && ydate=$(date -d yesterday $dfmt)
# Get yesterday's stats for queries after midnight.
if [ ! -z $2 ]
then
date=$(date -d yesterday $dfmt)
ydate=$(date -d "2 days ago" $dfmt)
fi
# Functions go here.
title(){
local w=57
printf "║ %-${w}s ║\n" "${*}"
}
get(){
if [ -f "$data/$date-$country" ]
then
jq -r ".$1" < "$data/$date-$country"
else
curl -sL corona.lmao.ninja/v2/"$endpoint" > "$data/$date-$country"
jq -r ".$1" < "$data/$date-$country"
fi
}
getpm(){
out "╰> Per 1M " "$(get $1) ($(echo "$(bc <<< "scale = 2; $(get $1) / 10000")%"))" ""
}
yget(){
date=$ydate
if [ -f "$data/$date-$country" ]
then
get "$1"
else
echo 0
fi
}
stats(){
out "$1:" "$(get $2)" "+ $(echo "scale = 2; $(get $2) - $(yget $2)" | bc)"
}
out(){
printf "║ %-${w}s ║ %${w}s ║ %-${w}s ║\n" "$1" "$2" "$3"
}
frame(){
case "$1" in
top) printf "╔ %-${w}s ═ %-${w}s ═ %-${w}s ╗\n" | sed "s/\ /═/g" ;;
sep) printf "╟ %-${w}s ╫ %-${w}s ╫ %-${w}s ╢\n" | sed "s/\ /─/g" ;;
up) printf "╠ %-${w}s ╦ %-${w}s ╦ %-${w}s ╣\n" | sed "s/\ /═/g" ;;
down) printf "╚ %-${w}s ╩ %-${w}s ╩ %-${w}s ╝\n" | sed "s/\ /═/g" ;;
line) printf "╠ %-${w}s ╩ %-${w}s ╩ %-${w}s ╣\n" | sed "s/\ /═/g" ;;
bot) printf "╚ %-${w}s ═ %-${w}s ═ %-${w}s ╝\n" | sed "s/\ /═/g" ;;
esac
}
parse(){
frame top
title "Queried at $date for $(get country)."
frame up
out Statistic Today Yesterday
frame sep
stats Total cases
getpm casesPerOneMillion
stats New todayCases
frame sep
stats Active active
stats Critical critical
stats Deaths deaths
getpm deathsPerOneMillion
stats Recovered recovered
stats Tests tests
getpm testsPerOneMillion
if [ ! -f $data/$ydate-$country ] || [ $(get todayCases) == "0" ]
then
frame line
if [ ! -f $data/$ydate-$country ]
then
title "Stats for $(get country) appear new."
fi
if [ $(get todayCases) == "0" ]
then
if [ -z $norm ]
then
title "New cases are 0. Removing data file."
rm $data/$date-$country
else
title "New cases are 0. Keeping data file."
fi
fi
frame bot
else
frame down
fi
}
result(){
if [ ! -f "$data/$date-$country-out" ]
then
parse > "$data/$date-$country-out"
fi
cat "$data/$date-$country-out"
}
# Main code starts here.
if [ ! -z "$country" ]
then
endpoint=countries/$country
result
else
endpoint=all
country=world
result
fi
# Cleanup and exit code handling.
if [ ! -f $data/$date-$country ]
then
rm $data/$date-$country-out
exit 1
else
exit 0
fi