[download]

local/bin/weather

   1 #!/bin/bash
   2 
   3 f=~/.cache/${0##*/}
   4 
   5 [[ -s $f.json ]] && (( $(date +%s) - $(date +%s -r $f) < 60 * 10 )) || { # {{{
   6     torsocks curl -sfo $f.html https://yandex.ru/pogoda/2
   7 
   8     grep -o "data-bem='[^']*forecastData[^']*'" $f.html | cut -d \' -f 2 |
   9     jq -r '."b-statcounter__metrika".params.forecastData' > $f.json
  10 
  11     {
  12         jq -r '.fact.icon, .fact.temp, .fact.feels_like' $f.json
  13       grep -o 'link__condition[^>]*>[^<]*' $f.html | cut -d \> -f 2
  14         jq -r '.hourly[] | [.hour, .temp]              | @tsv' $f.json | paste -s
  15         jq -r '.days[] | [.date, .temp_min, .temp_max] | @tsv' $f.json | paste -s
  16         jq -r .nowcastAlert $f.json
  17     } > $f
  18 } # }}}
  19 
  20 while getopts h:d: OPT; do
  21     case $OPT in
  22         h) htable=$OPTARG;;
  23         d) dtable=$OPTARG;;
  24     esac
  25 done
  26 shift $(( OPTIND - 1 ))
  27 
  28 IFS=$'\n' read -d '' icon weather feels desc hourly daily alert < $f
  29 
  30 weather=$(printf '%+d' $weather)
  31 feels=$(printf '%+d' $feels)
  32 feels=${feels#$weather}
  33 
  34 table(){ # {{{
  35     sed '
  36         s/\t/\t|\t/g
  37         s/^/|\t/
  38         s/$/\t|/
  39     ' |
  40     column -ts $'\t' |
  41     sed '0~2p; 0~2s/[^\|]/-/g; 1p' | sed '
  42         1s/[^\|]/-/g
  43         1s/-|-/---/g
  44         $s/-|-/---/g
  45     '
  46 } # }}}
  47 
  48 echo "$weather°C${feels:+ ($feels°C)} ${desc,,}"
  49 [[ $alert ]] && echo && echo "${alert}"
  50 
  51 echo
  52 
  53 IFS=x read x y <<< "$htable"
  54 hourly=($hourly) x=${x:-4} y=${y:-4}
  55 for (( i=1; i < x * y; i+=x )); do # {{{
  56     a=$(( i + 0 - 0 ))
  57     b=$(( i + x - 1 ))
  58 
  59     for j in $(seq $a $b); do
  60         j=$(( j * 2 + 0 ))
  61         printf '%0.2d:00\n' ${hourly[$j]}
  62     done | paste -s
  63 
  64     for j in $(seq $a $b); do
  65         j=$(( j * 2 + 1 ))
  66         printf '%+d°C\n' ${hourly[$j]}
  67     done | paste -s
  68 done | table # }}}
  69 
  70 echo
  71 
  72 IFS=x read x y <<< "$dtable"
  73 daily=($daily) x=${x:-3} y=${y:-2}
  74 for (( i=1; i < x * y; i+=x )); do # {{{
  75     a=$(( i + 0 - 0 ))
  76     b=$(( i + x - 1 ))
  77 
  78     for j in $(seq $a $b); do
  79         j=$(( j * 3 + 0 ))
  80         printf '%s %s\n' ${daily[$j]##*-} $(date +%b)
  81     done | paste -s
  82 
  83     for j in $(seq $a $b); do
  84         m=$(( j * 3 + 1 ))
  85         M=$(( j * 3 + 2 ))
  86         printf '%+d%+d°C\n' ${daily[$m]} ${daily[$M]}
  87     done | paste -s
  88 done | table # }}}