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

128 lines
2.7 KiB
Plaintext
Raw Permalink Normal View History

2020-04-30 11:00:52 +00:00
#!/bin/bash
2020-08-10 15:14:19 +00:00
# Variable declarations go here.
2022-06-10 22:00:13 +00:00
w=17
dfmt="+%Y-%m-%d"
2020-08-10 15:14:19 +00:00
data="$HOME/Documents/cv"
country=$1
2020-08-14 17:28:36 +00:00
[ ! -d "$data" ] && mkdir -p "$data"
2020-08-10 15:14:19 +00:00
[ -z $date ] && date=$(date $dfmt)
[ -z $ydate ] && ydate=$(date -d yesterday $dfmt)
2020-08-04 21:48:23 +00:00
2020-08-10 15:14:19 +00:00
# Get yesterday's stats for queries after midnight.
if [ ! -z $2 ]
2020-08-10 15:14:19 +00:00
then
date=$(date -d yesterday $dfmt)
ydate=$(date -d "2 days ago" $dfmt)
fi
# Functions go here.
2020-07-03 21:49:32 +00:00
title(){
2022-06-10 22:00:13 +00:00
local w=57
2020-07-03 21:35:47 +00:00
printf "║ %-${w}s ║\n" "${*}"
2020-07-03 17:58:02 +00:00
}
2020-04-30 11:00:52 +00:00
get(){
2020-05-12 13:17:36 +00:00
if [ -f "$data/$date-$country" ]
2020-04-30 11:00:52 +00:00
then
2020-05-12 13:17:36 +00:00
jq -r ".$1" < "$data/$date-$country"
2020-04-30 11:00:52 +00:00
else
2020-05-12 13:17:36 +00:00
curl -sL corona.lmao.ninja/v2/"$endpoint" > "$data/$date-$country"
jq -r ".$1" < "$data/$date-$country"
2020-04-30 11:00:52 +00:00
fi
}
2020-08-14 14:21:50 +00:00
getpm(){
2022-06-10 22:00:13 +00:00
out "╰> Per 1M " "$(get $1) ($(echo "$(bc <<< "scale = 2; $(get $1) / 10000")%"))" ""
2020-08-02 20:00:01 +00:00
}
yget(){
date=$ydate
if [ -f "$data/$date-$country" ]
then
get "$1"
else
echo 0
fi
}
2020-08-14 13:25:47 +00:00
stats(){
out "$1:" "$(get $2)" "+ $(echo "scale = 2; $(get $2) - $(yget $2)" | bc)"
}
2020-05-17 17:36:15 +00:00
out(){
2020-08-14 13:25:47 +00:00
printf "║ %-${w}s ║ %${w}s ║ %-${w}s ║\n" "$1" "$2" "$3"
2020-05-17 17:36:15 +00:00
}
2020-07-03 20:26:50 +00:00
frame(){
case "$1" in
2020-08-14 17:54:24 +00:00
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" ;;
2020-07-03 20:26:50 +00:00
esac
}
parse(){
2020-08-10 15:10:13 +00:00
frame top
2020-08-14 17:22:39 +00:00
title "Queried at $date for $(get country)."
2020-07-03 20:26:50 +00:00
frame up
2020-08-14 17:22:39 +00:00
out Statistic Today Yesterday
frame sep
2020-08-14 13:25:47 +00:00
stats Total cases
2020-08-14 14:21:50 +00:00
getpm casesPerOneMillion
stats New todayCases
frame sep
2020-08-14 13:25:47 +00:00
stats Active active
stats Critical critical
stats Deaths deaths
2020-08-14 14:21:50 +00:00
getpm deathsPerOneMillion
2020-08-14 13:25:47 +00:00
stats Recovered recovered
stats Tests tests
2020-08-14 14:21:50 +00:00
getpm testsPerOneMillion
2020-07-25 15:34:01 +00:00
if [ ! -f $data/$ydate-$country ] || [ $(get todayCases) == "0" ]
then
frame line
2020-07-25 15:34:01 +00:00
if [ ! -f $data/$ydate-$country ]
then
2020-08-20 21:51:09 +00:00
title "Stats for $(get country) appear new."
2020-07-25 15:34:01 +00:00
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
2020-04-30 11:00:52 +00:00
}
result(){
if [ ! -f "$data/$date-$country-out" ]
then
parse > "$data/$date-$country-out"
fi
cat "$data/$date-$country-out"
}
# Main code starts here.
2020-05-12 13:17:36 +00:00
if [ ! -z "$country" ]
2020-04-30 11:00:52 +00:00
then
2020-08-10 15:10:13 +00:00
endpoint=countries/$country
result
2020-04-30 11:00:52 +00:00
else
endpoint=all
country=world
2020-04-30 11:00:52 +00:00
result
fi
2020-11-15 08:53:25 +00:00
# Cleanup and exit code handling.
if [ ! -f $data/$date-$country ]
then
rm $data/$date-$country-out
exit 1
else
exit 0
fi