Skip to content

Instantly share code, notes, and snippets.

@meleu
Last active March 18, 2020 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meleu/271580d0771fbec56b63575c9fb5862d to your computer and use it in GitHub Desktop.
Save meleu/271580d0771fbec56b63575c9fb5862d to your computer and use it in GitHub Desktop.
Check the numbers of COVID-19 (caused by Coronavirus) cases/deaths/recovered directly at your terminal.
#!/usr/bin/env bash
# covid.sh
##########
readonly API_URL='https://coronavirus-19-api.herokuapp.com'
readonly USAGE="Usage: $0 [country1 [country2] [countryN]] "
declare -A data
json2array() {
local jsonData="$1"
# check if it's a valid JSON
jq empty <<<"$jsonData" 2>/dev/null || return 1
while IFS== read -r key value; do
data[$key]="$value"
done < <(jq -r '. | to_entries | .[] | .key + "=" + (.value|tostring)' <<<"$jsonData")
}
main() {
local json
# global info first
json="$(curl --silent "${API_URL}/all")"
if ! json2array "$json"; then
echo "ERROR: failed to get data from $API_URL" >&2
echo "$USAGE" >&2
exit 1
fi
echo -n "
=====================================
OFFICIALLY REPORTED CASES OF COVID-19
=====================================
---> global info
Cases.........................: ${data[cases]}
Deaths........................: ${data[deaths]}
Recovered patients............: ${data[recovered]}
"
for country in "$@"; do
json="$(curl --silent "${API_URL}/countries/${country}")"
if ! json2array "$json"; then
echo "ERROR: failed to get data from $API_URL" >&2
echo "$USAGE" >&2
exit 1
fi
echo -n "
---> info for ${data[country]}
Cases.........................: ${data[cases]}
Cases reported today..........: ${data[todayCases]}
Deaths........................: ${data[deaths]}
Deaths reported today.........: ${data[todayDeaths]}
Recovered patients............: ${data[recovered]}
Patients in critical situation: ${data[critical]}
"
done
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment