Advertisement
Guest User

corona functs

a guest
Apr 10th, 2020
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.18 KB | None | 0 0
  1. #corona virus functions
  2.  
  3. #reuters
  4. #usage: corona
  5. #usage: corona 'united kingdom'  #data for united kingdom
  6. #usage: corona json              #print json data
  7. corona() { (
  8.     tmpdir="$(mktemp -d)"
  9.     dfile="$tmpdir/data.txt"
  10.     curl -sL 'https://d3sl9l9bcxfb5q.cloudfront.net/json/corona-regionwise' > "$dfile"
  11.     [[ "$1" = json ]] && { cat "$dfile"; echo; return 0;}
  12.    
  13.     if [[ -z "$1" ]]; then
  14.         lastdate="$(jq -r '.[-1].date' "$dfile")"
  15.         update="$(jq -r '.[-1].updated' "$dfile")"
  16.  
  17.         {
  18.         jq -r '.[]|select(.date == "'$lastdate'")|"\(.cases)\t\(.deaths)\t\(.recovered)\t\(((.deaths//0|tonumber)/(.cases//0|tonumber)//0)*100)\t\(.location)"' "${dfile}"
  19.  
  20.         cases="$(jq -r '.[]|select(.date == "'$lastdate'")|.cases' "${dfile}" | paste -sd+ | bc -l)"
  21.         deaths="$(jq -r '.[]|select(.date == "'$lastdate'")|.deaths' "${dfile}" | paste -sd+ | bc -l)"
  22.         recovered="$(jq -r '.[]|select(.date == "'$lastdate'")|.recovered' "${dfile}" | paste -sd+ | bc -l)"
  23.         letality="$(bc -l <<<"($deaths/$cases)*100")"
  24.         printf '%s\t%s\t%s\t%s\tWorld\n' "$cases" "$deaths" "$recovered" "$letality"
  25.         } | sort -n | tac | cat -n | tac |
  26.             sed -E 's/^\s*([0-9]+)(\s)(.*)/\1\t\3/' |
  27.             column -ets$'\t' -NRNK,CASE,DEATH,RECOVER,MORT%,LOCAL -TMORT%,LOCAL
  28.         printf 'update: %s\n' "$update"
  29.  
  30.     else
  31.         location="$(tr 'a-z' 'A-Z' <<<"${*}")"
  32.         dfile2="$tmpdir/dataSelect.txt"
  33.  
  34.         ratef() {
  35.             last=0; day=0
  36.             while read total; do
  37.                 date="$(grep -o '[^ ]*$' <<<"$total" | sed -E 's/([0-9]{,2})\/([0-9]{,2})\/([0-9]{4})/\3\/\1\/\2/')"
  38.                 total="$(grep -o '^[^ ]*' <<<"$total")"
  39.                 #[[ "$total" = *[a-z]* ]] && continue
  40.                 perday="$((total-last))"
  41.                 ((last != 0)) || last=1
  42.                 rate="$(bc -l <<<"( ( $total / $last ) * 100 ) - 100")"
  43.                 (( rate != -100 )) || rate=0
  44.                 #[[ "$rate" =~ ^-* ]] && rate=0
  45.                 printf '%s\t%s\t+%s\t%.2f%%\t%s\n' "$day" "$total" "$perday" "$rate" "$date"
  46.                 last="$total"
  47.                 ((day++))
  48.             done | column -ets$'\t' -NDAY,$(tr 'a-z' 'A-Z' <<<"$i"),+INC,INC%,DATE
  49.         }
  50.  
  51.         #select data
  52.         jq -er '.[] | select(.location | ascii_upcase == "'$location'")' "$dfile" > "$dfile2" || return "$?"
  53.  
  54.         #slipt in smaller parts
  55.         csplit  --suppress-matched --prefix="$tmpdir/xx" <(sed 's/^}/&\n--/' <"$dfile2") '/^--/' '{*}' 1>/dev/null
  56.  
  57.         #make tables
  58.         for i in recovered deaths cases; do
  59.             printf '\n=========================\n'
  60.             printf '\n%s  %s\n' "$(tr 'a-z' 'A-Z' <<<"$i")" "$location"
  61.             printf '%s\n' "$tmpdir"/xx* |
  62.                 while read file; do
  63.                     (( $(wc -l <"$file") < 1 )) && continue
  64.                     jq -r '"\(.'$i'//0)  \(.date)"' "$file"
  65.                 done | ratef
  66.             done
  67.     fi
  68.  
  69.     rm -rf "$tmpdir"
  70.     printf '<graphics.reuters.com/CHINA-HEALTH-MAP/0100B59S39E/index.html>\n'
  71. );}
  72.  
  73. #worldometers
  74. corona2() {
  75.     curl --compressed -s 'https://www.worldometers.info/coronavirus/' |
  76.         sed 's/<[^>]*>//g' | grep -Fm1 'Coronavirus Update' |
  77.         tee >(grep -oE '[0-9,]{3,}' |
  78.         tr -d ',' | tac | paste -sd/ |
  79.         xargs printf 'scale=4;(%s)*100\n' | bc -l |
  80.         xargs printf 'Letality: %s%%\n') | sed 's/^\s*//'
  81.     printf '<https://www.worldometers.info/coronavirus/>\n'
  82. }
  83.  
  84. #corona-stats api
  85. #usage: corona3 [region]
  86. #[region] is a two letter symbol for country, eg. us, br it
  87. corona3() {
  88.        local regiao="/$(tr 'a-z' 'A-Z' <<<"$1")"
  89.        curl -s "https://corona-stats.online$regiao?source=2&minimal=true" | tac
  90. }
  91. #https://github.com/sagarkarira/coronavirus-tracker-cli
  92.  
  93. corona3a() { corona3 | rmansi | cut -c1-15,40-78 | sed 's/([A-Z][A-Z]/   /;s/([A-Z]/  /;s/[)(]/ /';}
  94.  
  95. corona3b() {
  96.     curl --compressed -s 'https://corona-stats.online?format=json' |
  97.         jq -r '(.data[],.worldStats)|"\(.country)\t\(.cases)\t\(.todayCases)\t\(.deaths)\t\((.deaths/.cases)*100)%\t\(.casesPerOneMillion)"' |
  98.         sort -nrt$'\t' -k2 | cat -n | tac |
  99.         sed -E 's/^\s*([0-9]+)(\s)(.*)/\1\t\3/' |
  100.         column -ets$'\t' -NRNK,LOCAL,CASES,NEW,DEATHS,MORT%,C/M -TLOCAL,MORT%
  101. }
  102.  
  103. #johns hopkins university center for systems science and engineering (jhu csse)
  104. #usage: corona4 [location] [confirmed|recovered|deaths] [global|US]
  105. #usage: corona4 help
  106. corona4() {
  107.     [[ "$1" = help ]]  && cat <<-! && return 0
  108.     usage: corona4 [location] [confirmed|recovered|deaths] [global|US]
  109.     usage: corona4 'united kingdom|Montserrat|Quebec|US' recovered
  110.     usage: corona4 florida '' US
  111.     usage: corona4 US recovered
  112.     usage: corona4 '' '' US
  113.     !
  114.     #local which
  115.  
  116.     [[ "$3" = us ]] && set -- "$1" "$2" US
  117.     #get data
  118.     curl -s "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_${2:-confirmed}_${3:-global}.csv" |
  119.         if [[ -n "$1" ]]; then
  120.             grep -Ei "(,|^)$1," |
  121.                 sed 's/^/\n&/' |
  122.                 #cut -d, -f5- |
  123.                 tr ',' '\n'
  124.         else
  125.             cat
  126.         fi
  127.     #print some cmd references to stderr
  128.     echo "$1 $2 ${3:-global}" 1>&2
  129. }
  130.  
  131. #corona brasil -- ministério da saúde
  132. #uso: corona5 arq.csv [csv|last] [estado|br] [coluna]
  133. #uso: corona5 ajuda
  134. corona5() {
  135.     { [[ "$1" = help ]]  || [[ "$1" = ajuda ]];} && cat<<-! && return 0
  136.     corona brasil -- ministério da saúde
  137.     uso: corona5 arq.csv [csv|last] [estado|brasil] [coluna]
  138.     uso: corona5             #tabela de todos os estados
  139.     uso: corona5 rj 5        #puxa somente a coluna (5) do rio de janeiro
  140.     uso: corona5 '' 5        #puxa somente a coluna (5) de todos os estados
  141.     uso: corona5 csv         #imprime o arquivo csv bruto
  142.     uso: corona5 last DF     #imprime o último resultado para DF
  143.     uso: corona5 last rj 0   #imprime último linha de csv para RJ
  144.     [estado]: sigla de duas letras
  145.     [coluna]: regiao(1); estado(2); data(3); casosNovos(4);
  146.     [coluna]: casosAcumulados(5); obitosNovos(6); obitosAcumulados(7)
  147.     arg1 'arq.csv' é obrigatório, ou arquivo csv por pipe.
  148.     !
  149.     #padroes
  150.     local data loc last csv
  151.  
  152.     #total do brasil ticker
  153.     if [[ "$*" =~ brasil ]] || [[ "$*" =~ br ]]; then
  154.         curl -s 'https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral' -H 'X-Parse-Application-Id: unAFkcaNDeXajurGB7LChj8SgQYS2ptm' |
  155.             jq -r '.results[]|"Confirmado: \(.total_confirmado)","Obitos____: \(.total_obitos)","Letalidade: \(.total_letalidade)","Update____: \(.dt_atualizacao)"'
  156.         return
  157.     elif [[ -f "$1" ]]; then
  158.         data="$(cat "$1")"
  159.         shift
  160.     elif [[ ! -t 0 ]]; then
  161.         data="$(cat /dev/stdin)"
  162.     elif csv=($(printf '%s\n' ~/*.csv)); [[ -f "${csv[-1]}" ]]; then
  163.         data="$(cat "${csv[-1]}")"
  164.     elif csv=($(printf '%s\n' ~/Downloads/*.csv)); [[ -f "${csv[-1]}" ]]; then
  165.         data="$(cat "${csv[-1]}")"
  166.     else
  167.         echo 'csv file is required' 1>&2
  168.         echo 'check <https://covid.saude.gov.br/>' 1>&2
  169.         return 1
  170.     fi
  171.     #opções de visualização
  172.     [[ "$1" = csv ]] && printf '%s\n' "$data" && return 0
  173.     [[ "$1" = last ]] && last=1 && shift
  174.    
  175.     #processa arquivo
  176.     if [[ -n "$1" ]]; then
  177.         printf '%s\n' "$1"
  178.     else
  179.         printf '%s\n' "$data" | cut -d';' -f2 | sort -u
  180.     fi | while read loc; do
  181.         [[ "${#loc}" -le 2 ]] && local flag=-i
  182.         printf '\n'
  183.         grep -a $flag "$loc" <<<"$data"  | grep -av '^regiao' |
  184.             if [[ -z "$last" ]]; then
  185.                 cut -d';' -f"${2:-1-}"
  186.             else
  187.                 tail -1
  188.             fi
  189.     done | if [[ -z "$2" ]]; then
  190.         column -ets';' -NREGIAO,BR,DATA,NOVO,ACUM,OBTO,OBAC
  191.         #cat
  192.     else
  193.         cat
  194.     fi
  195.     echo "$1 $2 $3" 1>&2
  196. }
  197.  
  198. #list total brasil cases per day
  199. corona5a() {
  200.     [[ -z "$1" ]] && echo 'need csv file\n' 1>&2 && return 1
  201.     trap 'break; return 1' 1 2 6
  202.     aa() {
  203.         corona5 "$1" | grep -Eo '\b[A-Z]{2}\b' |
  204.             sort -u |
  205.             while read line; do
  206.                 corona5 "$1" $line 5 | head -n-$c | tail -1
  207.             done 2>/dev/null | paste -sd+ | bc -l
  208.         }
  209.     local points="$(($(corona5 "$1" PR 5 2>/dev/null | wc -l)-2))"
  210.     for ((c=points;c!=-1;c--)); do aa "$1"; done 2>/dev/null
  211.     unset -f aa
  212.     trap - 1 2 6
  213. }
  214.  
  215. #gnu plot simple graph
  216. plot() { gnuplot -p -e 'plot "/dev/stdin"';}
  217. plot2() { gnuplot -p -e 'plot "/dev/stdin" with linespoints linestyle 1';}
  218. plot3() { gnuplot -p -e 'set logscale y "'${1:-10}'"; plot "/dev/stdin" with linespoints linestyle 1';}
  219.  
  220. #percentage rate between two values (one value per line)
  221. percentagef() { (
  222.     last=0; n=0
  223.     while read line; do
  224.         #line="$(grep -o '^[^ ]*' <<<"$line")"
  225.         { [[ "$line" = *[a-zA-Z]* ]] || [[ "$line" =~ ^\s*$ ]];} && echo "$line" 1>&2 && continue
  226.         perline="$((line-last))"
  227.         ((last != 0)) || last=1
  228.         rate="$(bc -l <<<"( $line / $last ) * 100")"
  229.         [[ "$rate" != 0 ]] && rate="$(bc -l <<<"$rate - 100")" || rate=0
  230.         #printf '%s\t%s\t+%s\t%.2f%%\n' "$n" "$line" "$perline" "$rate"
  231.         printf '%s\n' "$rate"
  232.         last="$line"
  233.         ((n++))
  234.     done #| column -ets$'\t' -NN,LINE,+INC,INC%
  235.     #printf 'cols: N,LINE,+INC,INC%%\n' 1>&2
  236. );}
  237.  
  238. #https://unix.stackexchange.com/questions/190337/how-can-i-make-a-graphical-plot-of-a-sequence-of-numbers-from-the-standard-input
  239. #{ gnuplot -p -e 'set title "'$tt'"; show title; set key off; set xlabel "day"; set ylabel "'$ylabel'"'  -e "set terminal png size 800,600; set output '$(head -1 "$arq" | tr ' ' '_' | tr -d "'," | sed -Ee 's/_+/_/g' -e 's/_$//' )$2.$ylabel.png'" -e 'plot "'<(sed $1 "$arq")'" with linespoints linestyle 1' 2>/dev/null ;}
  240.  
  241. #world population
  242. #usage: wpop [location|world]
  243. #usage: wpop [italy|uk|us|saudi-arabia|world]
  244. wpop() {
  245.     [[ -n "$1" ]] && local country="$1-population/"
  246.     if [[ "$1" = world ]] || [[ "$1" = all ]]; then
  247.         curl -s 'https://www.worldometers.info/geography/countries-of-the-world/' |
  248.             awk 'length > max_length { max_length = length; longest_line = $0 } END { print longest_line }' |
  249.             sed -e 's/<td>/\n\n&/g' -e 's/<[^>]*>//g' | sed -e 1,2d  -e '$d' |
  250.             sed -e 'N;N;N;s/\n/ /g' -e 's/Dependenc/\n&/g' | sed -e '/^$/d'
  251.     else
  252.         curl -s --compressed "https://www.worldometers.info/world-population/$country" |
  253.             sed -e 's/<[^>]*>//g' -ne '/[:[]new Date/p' |
  254.             sed -Ee 's/([A-Za-z])\s+([A-Za-z])/\1\2/' |
  255.             awk -F' ' '$4 > 50 { print $2" "$4}'
  256.     fi
  257.     echo "${1:-world historical}" 1>&2
  258. }
  259. #https://www.worldometers.info/world-population/
  260.  
  261. #area by countries
  262. warea() {
  263.     #curl 'https://raw.githubusercontent.com/mountaineerbr/extra/master/worldCountryArea.txt'
  264.     local cutcols=-c1-25,35-
  265.     w3m -dump -cols 100 'https://www.worldometers.info/geography/largest-countries-in-the-world/' |
  266.         tee >(grep '^\s*#' | cut $cutcols 1>&2) |
  267.         sed -n '/List of countries/,/^\[INS/p' | sed '$d' | tac | cut $cutcols
  268. }
  269.  
  270. warea2() {
  271.     curl -s 'https://raw.githubusercontent.com/mountaineerbr/extra/master/worldCountryAreaWiki.txt' |
  272.         tee >(grep -C1 '^Rank' 1>&2) | tac
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement