fish-corona

Track corona-virus cases from the fish-shell 😷ī¸

corona.fish (1967B)

 1 #   __ _     _                                          
 2 #  / _(_)   | |                                         
 3 # | |_ _ ___| |__ ______ ___ ___  _ __ ___  _ __   __ _ 
 4 # |  _| / __| '_ \______/ __/ _ \| '__/ _ \| '_ \ / _` |
 5 # | | | \__ \ | | |    | (_| (_) | | | (_) | | | | (_| |
 6 # |_| |_|___/_| |_|     \___\___/|_|  \___/|_| |_|\__,_|
 7 #                                                      
 8 # Pablo (C) 2020
 9 # You are free (as in freedom) to do whatever you please with this!
10 
11 # Filters the cached CSV and adds a header to it
12 function __corona_select_csv -a country
13     echo '🚩ī¸ Country, 😷ī¸ Cases, 🤒ī¸ New Cases, 💀ī¸ Deaths, 😰ī¸ New Deaths'
14 
15     if test (string lower "$country") = all
16         cat "$HOME/.cache/corona.csv"
17     else
18         grep -Ei "^\"?($country|World)" "$HOME/.cache/corona.csv"
19     end
20 end
21 
22 function corona -a country -d "Track corona-virus cases from fish-shell. 😷ī¸"
23     if test -z "$country"
24         if test -f "$HOME/.cache/country.name"
25             set country (cat "$HOME/.cache/country.name" )
26         else
27             set country_code (curl -s https://ipinfo.io/country/)
28             set country (curl -s "https://restcountries.eu/rest/v2/alpha/$country_code/" | jq '.name' | sed 's/"//g')
29             echo "$country" > "$HOME/.cache/country.name" 
30         end
31     end
32 
33     # Update the cache if it doesn't exists or if it hasn't been modified in
34     # over a day
35     set date (date +%Y-%m-%d)
36     not test -f "$HOME/.cache/corona.csv"
37     or test (stat -c %y "$HOME/.cache/corona.csv" | cut -d' ' -f1) != "$date"
38 
39     if test "$status" -eq 0
40         curl -s https://corona-stats.online/\?format=json \
41             | jq -r '.data + [.worldStats] 
42                         | .[] 
43                         | [.country,.cases,.todayCases,.deaths,.todayDeaths] 
44                         | @csv' \
45             > "$HOME/.cache/corona.csv"
46     end
47 
48     __corona_select_csv "$country" | csvlook -d ',' -e UTF-8
49 end
50