#!/usr/bin/env bash ########################################################################### # Made by : Ewoud Dronkert # Licence : GNU GPL v3 # Platform : Raspberry Pi, Raspbian # Requires : bash, curl, convert, avconv # for uploading: rsync, remote server with ssh key access # for notification: mail, locally configured mail system # Location : /usr/local/bin # Name : knmi # Version : 2.0.0 # Date : 2017-06-07 # Purpose : Download new KNMI temperature & wind charts every 10 # minutes, make daily video of collected images, upload # to web server, send notification mail. # How to use : Start as a system service # or from /etc/rc.local as "knmi &" # or from the command line as "sudo nohup knmi &" # Settings : SRC1 = web location of fresh image file (temperature) # SRC2 = web location of fresh image file (wind) # SAVE = local directory for saving images and videos # SRV = remote server (plus user and directory) # ID = SSH key with access to remote server ########################################################################### SRC1='http://cdn.knmi.nl/knmi/map/page/weer/actueel-weer/temperatuur.png' SRC2='http://cdn.knmi.nl/knmi/map/page/weer/actueel-weer/windkracht.png' SAVE='/home/pi/knmi/' # Local working directory SRV='xxxxx@xxxxx.xxxxxx.xx:xxx/xxxx/' # Where to upload the video ID='/home/pi/.ssh/id_rsa' # SSH key IMG='img-' # File name prefix for sequentially numbered images FMT='%03d' # Three digits 000-999 is enough for 6*24=144 images per day EXT='.png' # File name extension of all images DIM='640x640' # Convert downloaded images to these dimensions, mp4 compatible VID1=video-$(basename "$SRC1" "$EXT")- # File name prefix "video-temperatuur-" for video VID2=video-$(basename "$SRC2" "$EXT")- # File name prefix "video-windkracht-" for video DL1=dl-${NAME1:0:4}- # File name prefix "dl-temp-" for downloaded images DL2=dl-${NAME2:0:4}- # File name prefix "dl-wind-" for downloaded images PREV=25 # Hour of the day on the previous loop while true; do H=$(date +%-H) # Hour of the day if (( H < PREV )); then # It's a new day (or a fresh start of the script) YDAY=$(date -dyesterday +%Y%m%d) # Delete all image (but not video) files older than yesterday #FIRST=$(ls "$SAVE"dl-*"$YDAY"*"$SUF" | head -n1) #KEEP=$(basename "$FIRST") #find "$SAVE" -maxdepth 1 -type f -name "*$SUF" ! -newer "$FIRST" ! -name "$KEEP" -delete # Count number of saved image files from yesterday N=$(ls "$SAVE$YDAY"*"$SUF" 2>/dev/null | wc -l) # Ignore error if none found if (( n == 1 )); then # Just one image from yesterday, might as well upload it I guess SRC=$(ls "$SAVE$YDAY"*"$SUF") DST="temperatuur-$YDAY$SUF" rsync -qau -e "ssh -i '$ID'" "$SRC" "$SRV$DST" mv -f "$SRC" "$SAVE$DST" elif (( n > 1 )); then # Delete old sequential files rm -f "$SAVE$PRE"*"$SUF" # Rename yesterday's files sequentially i=0 for f in "$SAVE$YDAY"*"$SUF"; do mv -f "$f" "$SAVE$PRE$(printf $FMT $i)$SUF" (( ++i )) done # Make video from sequential files VIDNAME="temperatuur-$YDAY.mp4" VID="$SAVE$VIDNAME" avconv -y -r 4 -i "$SAVE$PRE$FMT$SUF" -an -r 4 -s $DIM -c:v libx264 "$VID" &>/dev/null # Process video if it exists if [[ -e "$VID" && -f "$VID" && -r "$VID" && -s "$VID" ]]; then chmod 644 "$VID" chown pi:pi "$VID" # Delete just processed files rm -f "$SAVE$PRE"*"$SUF" # Upload, 3 tries for i in {1..3}; do rsync -qau -e "ssh -i '$ID'" "$VID" "$SRV$VIDNAME" done # Send mail notification of new video ADMIN="xxxxx@xxxxxxxx.xxx" TO="$ADMIN" URL="https://xxxxx.xxxx.xxxxxx.xx/xxxx/" BODY="Nieuwe temperatuurvideo: $URL$VIDNAME Oudere video's staan op $URL -- Dit is een automatisch verstuurde e-mail." echo "$BODY" | mail -s "Nieuwe temperatuurvideo" -r "$ADMIN" "$TO" fi fi fi PREV=$H # New temperature and wind data gets recorded every 10 minutes at :00, :10, # etc. The chart usually gets timestamped 8 minutes later at :08, :18, etc. # It may take a few minutes for the new chart to get uploaded to the web # server, say a couple of minutes. With safety margin (not too soon or it # might not be there yet, not too late or creating the video might make us # miss it), check for new charts at :13, :23, etc. where :03 takes care of # the charts from :50 in the previous hour. M=$(date +%-M) # Minutes of the hour (( M1 = M % 10 )) # Minutes modulo 10 for wind (( M2 = M % 20 )) # Minutes modulo 20 for temperature (( S = 5 - M1 )) # Time diff to minute :05 if (( S < 0 )); then (( S += 10 )); fi # No negative sleep sleep ${S}m # Sleep S minutes # Get KNMI temperature chart FILE="$SAVE$(date +%Y%m%d%H%M)$SUF" if curl -s "$DATA" > "$FILE"; then convert "$FILE" -define png:color-type=2 -gravity center -extent $DIM "$FILE" chmod 644 "$FILE" chown pi:pi "$FILE" fi done