Skip to content

Instantly share code, notes, and snippets.

@Luuk3333
Last active June 10, 2018 23:10
Show Gist options
  • Save Luuk3333/db564cd9aea5ed41d56132029955feae to your computer and use it in GitHub Desktop.
Save Luuk3333/db564cd9aea5ed41d56132029955feae to your computer and use it in GitHub Desktop.
Python scripts to download buienradar.nl images.
import os
import arrow
import urllib
import zipfile
import time
# get times
timezone = 'Europe/Amsterdam'
timestamp_format = 'YYYY-MM-DD_HH-mm-ss'
log_timestamp_format = "YYYY-MM-DD HH:mm:ss.SSS ZZ"
name = "nederland_buienradar_sneeuw"
print("\n")
# save image
filename = name+"_" + arrow.utcnow().to(timezone).format(timestamp_format) + ".png"
def download():
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Downloading '"+filename+"'..")
urllib.urlretrieve("https://api.buienradar.nl/image/1.0/snowmapnl/?ext=png&nt=1&hist=-1&step=0", filename)
maxRetries = 3
retries = 0
download()
while (retries < maxRetries) and (os.stat(filename).st_size == 0):
print("\n["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Error: File '"+filename+"' is empty ("+str(os.stat(filename).st_size)+" bytes). Trying again.. (attempt "+str(retries+1)+" of "+str(maxRetries)+")")
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Removing '"+filename+"'..")
os.remove(filename)
retries += 1
time.sleep(5)
download()
# append file to compressed archive
zip_filename = name+".zip"
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Adding '"+filename+"' to '"+zip_filename+"'..")
z = zipfile.ZipFile(zip_filename, "a",zipfile.ZIP_STORED)
z.write(filename)
#z.printdir()
z.close()
# remove downloaded image
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Removing '"+filename+"'..")
os.remove(filename)
import os
import arrow
import urllib
import zipfile
import time
# get times
timezone = 'Europe/Amsterdam'
timestamp_format = 'YYYY-MM-DD_HH-mm-ss'
log_timestamp_format = "YYYY-MM-DD HH:mm:ss.SSS ZZ"
name = "nederland_buienradar"
print("\n")
# save image
filename = name+"_" + arrow.utcnow().to(timezone).format(timestamp_format) + ".png"
def download():
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Downloading '"+filename+"'..")
urllib.urlretrieve("https://api.buienradar.nl/image/1.0/radarmapnl/?ext=png&nt=1&hist=-1&step=0&ak=069425565a7c4a24b8091616ea18f2d9&h=512", filename)
maxRetries = 3
retries = 0
download()
while (retries < maxRetries) and (os.stat(filename).st_size == 0):
print("\n["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Error: File '"+filename+"' is empty ("+str(os.stat(filename).st_size)+" bytes). Trying again.. (attempt "+str(retries+1)+" of "+str(maxRetries)+")")
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Removing '"+filename+"'..")
os.remove(filename)
retries += 1
time.sleep(5)
download()
# append file to compressed archive
zip_filename = name+".zip"
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Adding '"+filename+"' to '"+zip_filename+"'..")
z = zipfile.ZipFile(zip_filename, "a",zipfile.ZIP_STORED)
z.write(filename)
#z.printdir()
z.close()
# remove downloaded image
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Removing '"+filename+"'..")
os.remove(filename)
#!/bin/bash
#####
# Options:
FPS=60
FONTSIZE=20 # pointsize (https://www.imagemagick.org/script/command-line-options.php#pointsize)
#####
# Variables
ZIPFILE=$1
FILENAME="${ZIPFILE%.*}"
TEMPDIR="${FILENAME}_tmp"
# Creating new working directory
mkdir $TEMPDIR
# Copy zip file to new directory
cp $ZIPFILE $TEMPDIR
# Unzip zip file
cd $TEMPDIR
unzip $ZIPFILE
# Add timestamps to images
echo "Adding timestamps to images.. (this will take a while)"
totalcount=`ls *.png -l | wc -l`
count=1
for file in *.png; do
echo $count/$totalcount;
# Extract date from filename
# and replace underscore with space (https://stackoverflow.com/a/5928254)
# and replace last dash with a colon (https://unix.stackexchange.com/a/187894)
NEWFILE=$(echo "$file" | grep -oP '\d{4}-\d{2}-\d{2}_\d{2}-\d{2}' | sed 's/_/ /g' | sed 's/\(.*\)-/\1:/')
# Add date extracted from filename to image in bottom left corner (https://unix.stackexchange.com/a/207396)
mogrify -fill white -undercolor '#00000080' -pointsize ${FONTSIZE} -gravity SouthWest -annotate +10+10 "$NEWFILE" $file
count=$((count+1));
done
# Create video
echo "Creating video.."
ffmpeg -framerate ${FPS} -pattern_type glob -i '*.png' -pix_fmt yuv420p ${FILENAME}.mp4
# Copy file to parent directory
mv ${FILENAME}.mp4 ..
# Delete all temporary files
echo "Removing temporary files.."
rm $ZIPFILE
rm *.png
cd ..
rmdir $TEMPDIR
echo "--> Done!"
import os
import arrow
import urllib
import zipfile
import time
# get times
timezone = 'Europe/Amsterdam'
timestamp_format = 'YYYY-MM-DD_HH-mm-ss'
log_timestamp_format = "YYYY-MM-DD HH:mm:ss.SSS ZZ"
name = "satinfrared"
print("\n")
# save image
filename = name+"_" + arrow.utcnow().shift(minutes=-30).to(timezone).format(timestamp_format) + ".jpg"
def download():
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Downloading '"+filename+"'..")
urllib.urlretrieve("https://api.buienradar.nl/image/1.0/satinfrared/?ext=jpg&nt=1&hist=-1&forc=1&step=0&type=NL&ak=b8ae5917dcc84600a2f58d0ba27289c6&w=550&h=512", filename)
maxRetries = 3
retries = 0
download()
while (retries < maxRetries) and (os.stat(filename).st_size == 0):
print("\n["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Error: File '"+filename+"' is empty ("+str(os.stat(filename).st_size)+" bytes). Trying again.. (attempt "+str(retries+1)+" of "+str(maxRetries)+")")
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Removing '"+filename+"'..")
os.remove(filename)
retries += 1
time.sleep(5)
download()
# append file to compressed archive
zip_filename = name+".zip"
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Adding '"+filename+"' to '"+zip_filename+"'..")
z = zipfile.ZipFile(zip_filename, "a",zipfile.ZIP_STORED)
z.write(filename)
#z.printdir()
z.close()
# remove downloaded image
print("["+arrow.utcnow().to(timezone).format(log_timestamp_format)+"]: Removing '"+filename+"'..")
os.remove(filename)
@Luuk3333
Copy link
Author

Luuk3333 commented Dec 12, 2017

https://www.reddit.com/r/thenetherlands/comments/7j9aof/buienradar_timelapse_van_de_afgelopen_dagen/

De bijbehorende cronjobs:

*/5 * * * * cd ~/radarbeelden; python nederland-buienradar.py >> nederland-buienradar.log
*/5 * * * * cd ~/radarbeelden; python nederland-buienradar-sneeuw.py >> nederland-buienradar-sneeuw.log
*/15 * * * * cd ~/radarbeelden; python satinfrared.py >> satinfrared.log

@Luuk3333
Copy link
Author

Luuk3333 commented May 28, 2018

To create a video:

  • Previous method:
    ffmpeg:
ffmpeg -framerate 60 -pattern_type glob -i '*.png' -pix_fmt yuv420p video.mp4
  • New automated method (includes timestamps):
    Usage preprocess.sh:
sh preprocess.sh nederland_buienradar.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment