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)