-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathknmi
More file actions
executable file
·155 lines (127 loc) · 5.34 KB
/
knmi
File metadata and controls
executable file
·155 lines (127 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/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.1
# Date : 2021-08-31
# 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='https://cdn.knmi.nl/knmi/map/page/weer/actueel-weer/temperatuur.png'
SRC2='https://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"
ffmpeg -y -hide_banner -r 8 -i "$LDIR$PRE$FMT$SUF" -an -r 8 -s $DIM -c:v libx264 -pix_fmt yuv420p "$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
TONAME=''
TOADDR=''
FROMNAME=''
MAIL="To: $TONAME <$TOADDR>
From: $FROMNAME
Subject: Nieuwe temperatuurvideo
Nieuwe temperatuurvideo:
$URL$VIDNAME
Oudere video's staan op $URL
--
Dit is een automatisch verstuurde e-mail. Uw antwoord wordt niet gelezen."
echo "$MAIL" | msmtp -t
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
if [ -e "$FILE" ]; then
if [ -s "$FILE" ]; then
ISPNG=$(file "$FILE" | grep -F 'PNG image')
if [ -n "$ISPNG" ]; then
convert "$FILE" -strip -define png:color-type=2 -gravity center -extent $DIM "$FILE"
chmod 644 "$FILE"
chown pi:pi "$FILE"
else
rm -f "$FILE"
fi
else
rm -f "$FILE"
fi
fi
fi
done