dotfiles

regexghost dotfiles and scripts
git clone https://git.regexghost.com/dotfiles.git
Log | Files | Refs | README

metoffice.sh (1865B)


      1 #!/usr/bin/env bash
      2 
      3 # This script is quite ugly and needs re-writing, but it works for now
      4 
      5 [ -d "/tmp/panel_i3_data" ] || mkdir "/tmp/panel_i3_data"
      6 
      7 # Syntax for current_location.csv:
      8 # `lat|lon`
      9 
     10 curLocationFile="$XDG_CONFIG_HOME/regexghost/current_location.csv"
     11 
     12 lat=$(cat "$curLocationFile" | cut -d "|" -f 1)
     13 lon=$(cat "$curLocationFile" | cut -d "|" -f 2)
     14 geohash=$(geohash $lat $lon 9)
     15 today_string="$(date "+%Y-%m-%d")"
     16 # Diffrent date versions need different syntax for specifiying the date to use
     17 if date --version 2>&1 | grep -q "GNU coreutils" > /dev/null; then
     18 	tomorrow_string="$(date -d @$(($(date +%s)+86400)) "+%Y-%m-%d")"
     19 	second_day_string="$(date -d @$(($(date +%s)+86400+86400)) "+%Y-%m-%d")"
     20 else
     21 	tomorrow_string="$(date -d $(($(date +%s)+86400)) "+%Y-%m-%d")"
     22 	second_day_string="$(date -d $(($(date +%s)+86400+86400)) "+%Y-%m-%d")"
     23 fi
     24 
     25 curl --connect-timeout 5 -s -L "https://weather.metoffice.gov.uk/forecast/$geohash" > "/tmp/panel_i3_data/metoffice.html"
     26 # awk instead of grep -A/-B - https://superuser.com/questions/298123/how-to-grep-and-print-the-next-n-lines-after-the-hit/298127#298127
     27 icon_name_today=$(awk -v var="$today_string" '$0 ~ "datetime=\"" var "\"" {p = 5} p > 0 {print $0; p--}' "/tmp/panel_i3_data/metoffice.html" | grep "class=\"tab-icon\"" | sed 's/;" class.*//g' | sed 's/.*alt="//g')
     28 icon_name_tomorrow=$(awk -v var="$tomorrow_string" '$0 ~ "datetime=\"" var "\"" {p = 5} p > 0 {print $0; p--}' "/tmp/panel_i3_data/metoffice.html" | grep "class=\"tab-icon\"" | sed 's/;" class.*//g' | sed 's/.*alt="//g')
     29 icon_name_second_day=$(awk -v var="$second_day_string" '$0 ~ "datetime=\"" var "\"" {p = 5} p > 0 {print $0; p--}' "/tmp/panel_i3_data/metoffice.html" | grep "class=\"tab-icon\"" | sed 's/;" class.*//g' | sed 's/.*alt="//g')
     30 
     31 echo "$icon_name_today"
     32 echo "$icon_name_tomorrow"
     33 echo "$icon_name_second_day"
     34 
     35