Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #corona virus functions
- #reuters
- #usage: corona
- #usage: corona 'united kingdom' #data for united kingdom
- #usage: corona json #print json data
- corona() { (
- tmpdir="$(mktemp -d)"
- dfile="$tmpdir/data.txt"
- curl -sL 'https://d3sl9l9bcxfb5q.cloudfront.net/json/corona-regionwise' > "$dfile"
- [[ "$1" = json ]] && { cat "$dfile"; echo; return 0;}
- if [[ -z "$1" ]]; then
- lastdate="$(jq -r '.[-1].date' "$dfile")"
- update="$(jq -r '.[-1].updated' "$dfile")"
- {
- jq -r '.[]|select(.date == "'$lastdate'")|"\(.cases)\t\(.deaths)\t\(.recovered)\t\(((.deaths//0|tonumber)/(.cases//0|tonumber)//0)*100)\t\(.location)"' "${dfile}"
- cases="$(jq -r '.[]|select(.date == "'$lastdate'")|.cases' "${dfile}" | paste -sd+ | bc -l)"
- deaths="$(jq -r '.[]|select(.date == "'$lastdate'")|.deaths' "${dfile}" | paste -sd+ | bc -l)"
- recovered="$(jq -r '.[]|select(.date == "'$lastdate'")|.recovered' "${dfile}" | paste -sd+ | bc -l)"
- letality="$(bc -l <<<"($deaths/$cases)*100")"
- printf '%s\t%s\t%s\t%s\tWorld\n' "$cases" "$deaths" "$recovered" "$letality"
- } | sort -n | tac | cat -n | tac |
- sed -E 's/^\s*([0-9]+)(\s)(.*)/\1\t\3/' |
- column -ets$'\t' -NRNK,CASE,DEATH,RECOVER,MORT%,LOCAL -TMORT%,LOCAL
- printf 'update: %s\n' "$update"
- else
- location="$(tr 'a-z' 'A-Z' <<<"${*}")"
- dfile2="$tmpdir/dataSelect.txt"
- ratef() {
- last=0; day=0
- while read total; do
- date="$(grep -o '[^ ]*$' <<<"$total" | sed -E 's/([0-9]{,2})\/([0-9]{,2})\/([0-9]{4})/\3\/\1\/\2/')"
- total="$(grep -o '^[^ ]*' <<<"$total")"
- #[[ "$total" = *[a-z]* ]] && continue
- perday="$((total-last))"
- ((last != 0)) || last=1
- rate="$(bc -l <<<"( ( $total / $last ) * 100 ) - 100")"
- (( rate != -100 )) || rate=0
- #[[ "$rate" =~ ^-* ]] && rate=0
- printf '%s\t%s\t+%s\t%.2f%%\t%s\n' "$day" "$total" "$perday" "$rate" "$date"
- last="$total"
- ((day++))
- done | column -ets$'\t' -NDAY,$(tr 'a-z' 'A-Z' <<<"$i"),+INC,INC%,DATE
- }
- #select data
- jq -er '.[] | select(.location | ascii_upcase == "'$location'")' "$dfile" > "$dfile2" || return "$?"
- #slipt in smaller parts
- csplit --suppress-matched --prefix="$tmpdir/xx" <(sed 's/^}/&\n--/' <"$dfile2") '/^--/' '{*}' 1>/dev/null
- #make tables
- for i in recovered deaths cases; do
- printf '\n=========================\n'
- printf '\n%s %s\n' "$(tr 'a-z' 'A-Z' <<<"$i")" "$location"
- printf '%s\n' "$tmpdir"/xx* |
- while read file; do
- (( $(wc -l <"$file") < 1 )) && continue
- jq -r '"\(.'$i'//0) \(.date)"' "$file"
- done | ratef
- done
- fi
- rm -rf "$tmpdir"
- printf '<graphics.reuters.com/CHINA-HEALTH-MAP/0100B59S39E/index.html>\n'
- );}
- #worldometers
- corona2() {
- curl --compressed -s 'https://www.worldometers.info/coronavirus/' |
- sed 's/<[^>]*>//g' | grep -Fm1 'Coronavirus Update' |
- tee >(grep -oE '[0-9,]{3,}' |
- tr -d ',' | tac | paste -sd/ |
- xargs printf 'scale=4;(%s)*100\n' | bc -l |
- xargs printf 'Letality: %s%%\n') | sed 's/^\s*//'
- printf '<https://www.worldometers.info/coronavirus/>\n'
- }
- #corona-stats api
- #usage: corona3 [region]
- #[region] is a two letter symbol for country, eg. us, br it
- corona3() {
- local regiao="/$(tr 'a-z' 'A-Z' <<<"$1")"
- curl -s "https://corona-stats.online$regiao?source=2&minimal=true" | tac
- }
- #https://github.com/sagarkarira/coronavirus-tracker-cli
- corona3a() { corona3 | rmansi | cut -c1-15,40-78 | sed 's/([A-Z][A-Z]/ /;s/([A-Z]/ /;s/[)(]/ /';}
- corona3b() {
- curl --compressed -s 'https://corona-stats.online?format=json' |
- jq -r '(.data[],.worldStats)|"\(.country)\t\(.cases)\t\(.todayCases)\t\(.deaths)\t\((.deaths/.cases)*100)%\t\(.casesPerOneMillion)"' |
- sort -nrt$'\t' -k2 | cat -n | tac |
- sed -E 's/^\s*([0-9]+)(\s)(.*)/\1\t\3/' |
- column -ets$'\t' -NRNK,LOCAL,CASES,NEW,DEATHS,MORT%,C/M -TLOCAL,MORT%
- }
- #johns hopkins university center for systems science and engineering (jhu csse)
- #usage: corona4 [location] [confirmed|recovered|deaths] [global|US]
- #usage: corona4 help
- corona4() {
- [[ "$1" = help ]] && cat <<-! && return 0
- usage: corona4 [location] [confirmed|recovered|deaths] [global|US]
- usage: corona4 'united kingdom|Montserrat|Quebec|US' recovered
- usage: corona4 florida '' US
- usage: corona4 US recovered
- usage: corona4 '' '' US
- !
- #local which
- [[ "$3" = us ]] && set -- "$1" "$2" US
- #get data
- 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" |
- if [[ -n "$1" ]]; then
- grep -Ei "(,|^)$1," |
- sed 's/^/\n&/' |
- #cut -d, -f5- |
- tr ',' '\n'
- else
- cat
- fi
- #print some cmd references to stderr
- echo "$1 $2 ${3:-global}" 1>&2
- }
- #corona brasil -- ministério da saúde
- #uso: corona5 arq.csv [csv|last] [estado|br] [coluna]
- #uso: corona5 ajuda
- corona5() {
- { [[ "$1" = help ]] || [[ "$1" = ajuda ]];} && cat<<-! && return 0
- corona brasil -- ministério da saúde
- uso: corona5 arq.csv [csv|last] [estado|brasil] [coluna]
- uso: corona5 #tabela de todos os estados
- uso: corona5 rj 5 #puxa somente a coluna (5) do rio de janeiro
- uso: corona5 '' 5 #puxa somente a coluna (5) de todos os estados
- uso: corona5 csv #imprime o arquivo csv bruto
- uso: corona5 last DF #imprime o último resultado para DF
- uso: corona5 last rj 0 #imprime último linha de csv para RJ
- [estado]: sigla de duas letras
- [coluna]: regiao(1); estado(2); data(3); casosNovos(4);
- [coluna]: casosAcumulados(5); obitosNovos(6); obitosAcumulados(7)
- arg1 'arq.csv' é obrigatório, ou arquivo csv por pipe.
- !
- #padroes
- local data loc last csv
- #total do brasil ticker
- if [[ "$*" =~ brasil ]] || [[ "$*" =~ br ]]; then
- curl -s 'https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral' -H 'X-Parse-Application-Id: unAFkcaNDeXajurGB7LChj8SgQYS2ptm' |
- jq -r '.results[]|"Confirmado: \(.total_confirmado)","Obitos____: \(.total_obitos)","Letalidade: \(.total_letalidade)","Update____: \(.dt_atualizacao)"'
- return
- elif [[ -f "$1" ]]; then
- data="$(cat "$1")"
- shift
- elif [[ ! -t 0 ]]; then
- data="$(cat /dev/stdin)"
- elif csv=($(printf '%s\n' ~/*.csv)); [[ -f "${csv[-1]}" ]]; then
- data="$(cat "${csv[-1]}")"
- elif csv=($(printf '%s\n' ~/Downloads/*.csv)); [[ -f "${csv[-1]}" ]]; then
- data="$(cat "${csv[-1]}")"
- else
- echo 'csv file is required' 1>&2
- echo 'check <https://covid.saude.gov.br/>' 1>&2
- return 1
- fi
- #opções de visualização
- [[ "$1" = csv ]] && printf '%s\n' "$data" && return 0
- [[ "$1" = last ]] && last=1 && shift
- #processa arquivo
- if [[ -n "$1" ]]; then
- printf '%s\n' "$1"
- else
- printf '%s\n' "$data" | cut -d';' -f2 | sort -u
- fi | while read loc; do
- [[ "${#loc}" -le 2 ]] && local flag=-i
- printf '\n'
- grep -a $flag "$loc" <<<"$data" | grep -av '^regiao' |
- if [[ -z "$last" ]]; then
- cut -d';' -f"${2:-1-}"
- else
- tail -1
- fi
- done | if [[ -z "$2" ]]; then
- column -ets';' -NREGIAO,BR,DATA,NOVO,ACUM,OBTO,OBAC
- #cat
- else
- cat
- fi
- echo "$1 $2 $3" 1>&2
- }
- #list total brasil cases per day
- corona5a() {
- [[ -z "$1" ]] && echo 'need csv file\n' 1>&2 && return 1
- trap 'break; return 1' 1 2 6
- aa() {
- corona5 "$1" | grep -Eo '\b[A-Z]{2}\b' |
- sort -u |
- while read line; do
- corona5 "$1" $line 5 | head -n-$c | tail -1
- done 2>/dev/null | paste -sd+ | bc -l
- }
- local points="$(($(corona5 "$1" PR 5 2>/dev/null | wc -l)-2))"
- for ((c=points;c!=-1;c--)); do aa "$1"; done 2>/dev/null
- unset -f aa
- trap - 1 2 6
- }
- #gnu plot simple graph
- plot() { gnuplot -p -e 'plot "/dev/stdin"';}
- plot2() { gnuplot -p -e 'plot "/dev/stdin" with linespoints linestyle 1';}
- plot3() { gnuplot -p -e 'set logscale y "'${1:-10}'"; plot "/dev/stdin" with linespoints linestyle 1';}
- #percentage rate between two values (one value per line)
- percentagef() { (
- last=0; n=0
- while read line; do
- #line="$(grep -o '^[^ ]*' <<<"$line")"
- { [[ "$line" = *[a-zA-Z]* ]] || [[ "$line" =~ ^\s*$ ]];} && echo "$line" 1>&2 && continue
- perline="$((line-last))"
- ((last != 0)) || last=1
- rate="$(bc -l <<<"( $line / $last ) * 100")"
- [[ "$rate" != 0 ]] && rate="$(bc -l <<<"$rate - 100")" || rate=0
- #printf '%s\t%s\t+%s\t%.2f%%\n' "$n" "$line" "$perline" "$rate"
- printf '%s\n' "$rate"
- last="$line"
- ((n++))
- done #| column -ets$'\t' -NN,LINE,+INC,INC%
- #printf 'cols: N,LINE,+INC,INC%%\n' 1>&2
- );}
- #https://unix.stackexchange.com/questions/190337/how-can-i-make-a-graphical-plot-of-a-sequence-of-numbers-from-the-standard-input
- #{ 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 ;}
- #world population
- #usage: wpop [location|world]
- #usage: wpop [italy|uk|us|saudi-arabia|world]
- wpop() {
- [[ -n "$1" ]] && local country="$1-population/"
- if [[ "$1" = world ]] || [[ "$1" = all ]]; then
- curl -s 'https://www.worldometers.info/geography/countries-of-the-world/' |
- awk 'length > max_length { max_length = length; longest_line = $0 } END { print longest_line }' |
- sed -e 's/<td>/\n\n&/g' -e 's/<[^>]*>//g' | sed -e 1,2d -e '$d' |
- sed -e 'N;N;N;s/\n/ /g' -e 's/Dependenc/\n&/g' | sed -e '/^$/d'
- else
- curl -s --compressed "https://www.worldometers.info/world-population/$country" |
- sed -e 's/<[^>]*>//g' -ne '/[:[]new Date/p' |
- sed -Ee 's/([A-Za-z])\s+([A-Za-z])/\1\2/' |
- awk -F' ' '$4 > 50 { print $2" "$4}'
- fi
- echo "${1:-world historical}" 1>&2
- }
- #https://www.worldometers.info/world-population/
- #area by countries
- warea() {
- #curl 'https://raw.githubusercontent.com/mountaineerbr/extra/master/worldCountryArea.txt'
- local cutcols=-c1-25,35-
- w3m -dump -cols 100 'https://www.worldometers.info/geography/largest-countries-in-the-world/' |
- tee >(grep '^\s*#' | cut $cutcols 1>&2) |
- sed -n '/List of countries/,/^\[INS/p' | sed '$d' | tac | cut $cutcols
- }
- warea2() {
- curl -s 'https://raw.githubusercontent.com/mountaineerbr/extra/master/worldCountryAreaWiki.txt' |
- tee >(grep -C1 '^Rank' 1>&2) | tac
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement