-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemperatuur
executable file
·156 lines (124 loc) · 4.78 KB
/
temperatuur
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
156
#!/usr/bin/env bash
###############################################################################
# Made by : Ewoud Dronkert
# Licence : GNU GPL v3
# Platform : Raspberry Pi, Raspbian
# Requires : bash, curl, convert, ffmpeg, rsync, mail (msmtp),
# remote server with ssh key access
# Location : /usr/local/bin
# Name : temperatuur
# Version : 1.0.2
# Date : 2021-08-31
# Purpose : Download new KNMI temperature chart every 20 minutes, make
# daily video of collected images, upload to webserver.
# How to use : Start as a system service
# or from /etc/rc.local as "temperatuur &"
# or from the command line as "sudo nohup temperatuur &"
# Settings : DATA = web location of fresh image file
# LDIR = local directory for saving images and videos
# ID = SSH key for access to remote server
# RUSER/RHOST/RDIR = server info
# TONAME/TOADDR = email info
# URL = public address of the video directory
###############################################################################
DATA='https://cdn.knmi.nl/knmi/map/page/weer/actueel-weer/temperatuur.png'
LDIR='/home/pi/knmi/' # Local directory on the Pi running this script
ID='/home/pi/.ssh/id_rsa' # SSH key with access to the server
RUSER='' # Username with ssh access to the server
RHOST='shell.xs4all.nl' # Server with ssh/scp/rsync access
RDIR='WWW/knmi/' # Remote directory on the server, relative to ssh login
FROMNAME='Pi Home' # Sender name to use on the new video notification mail
TONAME='' # Recipient name to use on the new video notification mail
TOADDR='' # Where to send the new video notification mail
URL="https://$RUSER.home.xs4all.nl/knmi/"
SRV="$RUSER@$RHOST:$RDIR" # Where to upload the video
###############################################################################
PRE='img-' # To distinguish from downloaded files when removing
FMT='%03d' # Three digit format 000-999 needed for 6*24=144 images per day
SUF='.png'
DIM='640x640' # Convert downloaded images to these dimensions, mp4 compatible
PREV=25
while true; do
H=$(date +%-H)
if (( H < PREV )); then
# New day
YDAY=$(date -dyesterday +%Y%m%d)
# Count number of saved image files from yesterday
# Ignore error if none found
n=$(ls "$LDIR$YDAY"*"$SUF" 2>/dev/null | wc -l)
if (( n == 1 )); then
# Just one image from yesterday, might as well upload it I guess
SRC=$(ls "$LDIR$YDAY"*"$SUF")
DST="temperatuur-$YDAY$SUF"
rsync -qau -e "ssh -i '$ID'" "$SRC" "$SRV$DST"
mv -f "$SRC" "$LDIR$DST"
elif (( n > 1 )); then
# Delete all image (but not video) files older than yesterday
#FIRST=$(ls "$LDIR$YDAY"*"$SUF" | head -n1)
#KEEP=$(basename "$FIRST")
#find "$LDIR" -maxdepth 1 -type f -name "*$SUF" ! -newer "$FIRST" ! -name "$KEEP" -delete
# Make room for new sequential files, delete the old ones
rm -f "$LDIR$PRE"*"$SUF"
# Rename yesterday's files sequentially
i=0
for f in "$LDIR$YDAY"*"$SUF"; do
mv -f "$f" "$LDIR$PRE$(printf $FMT $i)$SUF"
(( ++i ))
done
# Make video from sequential files
VIDNAME="temperatuur-$YDAY.mp4"
VID="$LDIR$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 "$LDIR$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
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
# Sleep until :04, :14, :24, :34, :44, :54
M=$(date +%-M) # minutes of the clock
while (( M >= 10 )); do (( M -= 10 )); done # modulo 10
(( N = 4 - M )) # time diff to next img of the interval
if (( N < 0 )); then (( N += 10 )); fi # no negative sleep
sleep ${N}m # sleep N minutes
# Get KNMI temperature chart
FILE="$LDIR$(date +%Y%m%d%H%M)$SUF"
curl -s "$DATA" > "$FILE"
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
done