#!/usr/bin/python
#should be executed every 10 minutes with cron
#2,12,22,32,42,52 * * * * /path/getRadarImages.py
import shutil
import requests
from datetime import datetime
from os import makedirs
from PIL import Image
import sys
import os.path
import time

if(len(sys.argv) != 2 and len(sys.argv) != 1):
    print("-1");
    sys.exit("Wrong number of parameters ("+str(len(sys.argv))+")")

if(len(sys.argv) == 1):
    imageDir = "/tmp/Meteo-Backend/radarImages"
else:
    imageDir = sys.argv[1]

if (os.path.isdir(imageDir) == False):
  makedirs(imageDir)

now = datetime.utcnow()
minutes = int(now.strftime("%M"))//10*10;
imageURLLightning = "http://radar.bourky.cz/data/celdn/pacz2gmaps.blesk."+now.strftime("%Y%m%d.%H")+str(minutes).zfill(2)+".png";
imageURLRadar = "http://radar.bourky.cz/data/pacz2gmaps.z_max3d."+now.strftime("%Y%m%d.%H")+str(minutes).zfill(2)+".0.png";

response = requests.get(imageURLLightning, stream=True)
with open(imageDir+"/lightning"+now.strftime("%Y%m%d.%H")+str(minutes).zfill(2)+".png", "wb") as out_file:
  shutil.copyfileobj(response.raw, out_file)
del response

response = requests.get(imageURLRadar, stream=True)
with open(imageDir+"/radar"+now.strftime("%Y%m%d.%H")+str(minutes).zfill(2)+".png", "wb") as out_file:
  shutil.copyfileobj(response.raw, out_file)
del response

now = time.time()
for f in os.listdir(imageDir):
  f = os.path.join(imageDir, f)
  if os.stat(f).st_mtime < now - 2*60*60:
    if os.path.isfile(f):
      os.remove(os.path.join(imageDir, f))

@@ -63,24 +63,24 @@
 on%0A#
-2,12,22,32,42,52
+5,15,25,35,45,55
  * *
@@ -619,33 +619,76 @@
 /10*10;%0A
-imageURLLightning
+minutesLight = int(now.strftime(%22%25M%22))//15*15;%0AimageURLRadar
  = %22http
@@ -715,14 +715,8 @@
 ata/
-celdn/
 pacz
@@ -726,13 +726,15 @@
 aps.
-blesk
+z_max3d
 .%22+n
@@ -778,24 +778,26 @@
 .zfill(2)+%22.
+0.
 png%22;%0AimageU
@@ -790,37 +790,41 @@
 0.png%22;%0AimageURL
-Radar
+Lightning
  = %22http://radar
@@ -831,32 +831,38 @@
 .bourky.cz/data/
+celdn/
 pacz2gmaps.z_max
@@ -848,39 +848,37 @@
 eldn/pacz2gmaps.
-z_max3d
+blesk
 .%22+now.strftime(
@@ -893,32 +893,37 @@
 %25H%22)+str(minutes
+Light
 ).zfill(2)+%22.0.p
@@ -915,26 +915,24 @@
 .zfill(2)+%22.
-0.
 png%22;%0A%0Arespo

Fix lightning radar images bug