diff --git a/corona.fish b/corona.fish
@@ -8,19 +8,47 @@
# Pablo Emilio Escobar Gavíria (C) 2020
#
# You are free to do whatever you please with this!
-
+
+function __corona_select_csv -a country
+ echo '🚩️ Country, 😷️ Cases, 🤒️ New Cases, 💀️ Deaths, 😰️ New Deaths'
+
+ if [ (string lower "$country") = all ]
+ cat "$HOME/.cache/corona.csv"
+ else
+ grep -E "^\"?($country|World)" "$HOME/.cache/corona.csv"
+ end
+end
+
function corona -a country -d "Track conronavirus cases from command line. 😷️"
- switch "$country"
+ switch (string lower "$country")
case all
set country ""
# Figure out the host's country from his IP
case ""
- set country (curl -s ipinfo.io/country (hostname -I | cut -d' ' -f1))
+ if [ -f "$HOME/.cache/country.name" ]
+ set country (cat "$HOME/.cache/country.name" )
+ else
+ set country_code (curl -s https://ipinfo.io/country/)
+ set country (curl -s "https://restcountries.eu/rest/v2/alpha/$country_code/" | jq '.name' | sed 's/"//g')
+ echo "$country" > "$HOME/.cache/country.name"
+ end
+ end
+
+ # Update the cache if it doesn't exists or if it hasn't been modified in
+ # over a day
+ set date (date +%Y-%m-%d)
+ if ! [ -f "$HOME/.cache/corona.csv" ] \
+ || [ (stat -c %y "$HOME/.cache/corona.csv" | cut -d' ' -f1) = "$date" ]
+ curl -s https://corona-stats.online/\?format=json \
+ | jq -r '.data + [.worldStats]
+ | map({country, cases, todayCases, deaths, todayDeaths})
+ | (.[0] | keys_unsorted) as $keys
+ | .[]
+ | [.[$keys[]]]
+ @csv' \
+ > "$HOME/.cache/corona.csv"
end
-
- curl -s "https://corona-stats.online/$country?format=json" \
- | jq -r '.data + [.worldStats] | map({country: "🚩️ \(.country)", cases, todayCases, deaths, todayDeaths}) | (.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv' \
- | sed 's/,/ ,/g; s/country/🚩️ Country/g; s/cases/😷️ Cases/g; s/todayCases/🤒️ New Cases/g; s/deaths/💀️ Deaths/g; s/todayDeaths/😰️ New Deaths/g; s/🚩️ World/🌐️ World/g' \
- | csvlook -d ',' -e UTF-8
+
+ __corona_select_csv "$country" | csvlook -d ',' -e UTF-8
end