Mint Serena 64 bit using the Mate desktop environment. Most of the following scripts will run on this machine, although some have only been tested with the Gnome desktop. Extra software requirements can be met by the Ubuntu and safe PPA repositories. Scripts may be re-written or added to from time to time, the date serves as the version number. Syntax highlighting on these pages is provided by GVim's "Convert to HTML" option.

Weather Scraper


#!/bin/bash
# Filename: weather.sh
# Version.: 21092018
# Author..: robz
#
# Met Office scraper widget.
# Get the url from the web page for your location, chop the date off the end.
# Run from a launcher with this command.
# mate-terminal -t "Current Weather" -e /home/robz/scripts/weather.sh
#
sed1 () { sed 's/<[^>]\+>//g'; }
sed2 () { sed -e 's/^[ \t\n]*//;s/[ \t\n]*//'; }

wsite="https://www.metoffice.gov.uk/public/weather/forecast/qwerty123#?date="
wfile="/home/robz/scripts/weather.txt"
hours=$(date +%H | sed 's/^0//')
today="$(date +%Y-%m-%d)"
check=$(grep 'Timecheck' "$0" | tail -1 | cut -d' ' -f3)

if [[ "$check" != "$hours" ]]; then             # Check stamp at end of script.
    wget -q --retry-connrefused --waitretry=1 \
    "$wsite$today" -O "$wfile"                  # If it's old re-scrape page.
    sed -i '$d' "$0"                            # Delete old time stamp.
    echo "# Timecheck $hours" >> $0             # Insert new.
fi

echo -e "\nTodays weather - $(date +%a' '%d' '%b' - '%H':00') onwards:-\n"

time=$(grep -m1 -A9 'Now' "$wfile" | sed1 | sed2)
aspt=$(grep -m1 -A30 'weatherWX' "$wfile" | sed2 | grep 'title' | \
cut -d\" -f2 | sed -e 's/day\|night//')
prec=$(grep -m1 -A10 'Chance' "$wfile" | sed1 | sed2 | cut -d\; -f2 | tail)
temp=$(grep -m1 -A80 'weatherTemp' "$wfile" | grep 'data-value=' | \
cut -d\" -f2 | sed 's/$/°C/')
wind=$(grep -m10 -A10 '"direction"' "$wfile" | grep 'data-value=' | \
cut -d\" -f2 | sed -e 's/$/mph/')
win2=$(grep -m10 -A10 '"direction"' "$wfile" | grep 'data-value2=' | \
cut -d\" -f2)
gust=$(grep -m11 -A11 '"gust"' "$wfile" | grep 'data-value="' | sed 'n;d;' | \
cut -d\" -f2 | sed -e 's/$/mph/')

hours=$(( 24-hours ))                           # Number of hours to head file.
paste <(echo "$time") <(echo "$aspt") <(echo "$prec") <(echo "$temp") \
<(echo "$wind") <(echo "$win2") <(echo "$gust") | head -n$hours | \
column -s $'\t' -t

echo
echo $(grep -m2 -E 'Sunrise|Sunset' "$wfile" | sed1 | sed2) | column -s -t
read

# Timecheck has to be the last line no blanks.
# Timecheck 13

No comments: