"""Cache yearly warning shapefiles

Sadly, it takes the system 6-10 minutes to generate these online, so best to
pre-generate them and allow folks to download.

    This is a cron job from RUN_2AM.sh
"""
import requests
import datetime
import sys

FINAL = "/mesonet/share/pickup/wwa/"
URL = "http://iem.local/cgi-bin/request/gis/watchwarn.py"


def get_files(year):
    """Go get our files and then cache them! """
    myuri = ('%s?simple=yes&year1=%s&month1=1&day1=1&hour1=0&minute1=0'
             '&year2=%s&month2=12&day2=31&hour2=23&minute2=59'
             ) % (URL, year, year)

    req = requests.get(myuri)
    if req.status_code != 200:
        print("warn_cache[%s] failed with status: %s\n%s" % (req.status, myuri)
              )
        return
    else:
        o = open("%s/%s_all.zip" % (FINAL, year), 'wb')
        o.write(req.content)
        o.close()

    # Now do SBW variant
    myuri = "%s&limit0=yes" % (myuri,)
    req = requests.get(myuri)
    if req.status_code != 200:
        print("warn_cache[%s] failed with status: %s\n%s" % (req.status, myuri)
              )
    else:
        o = open("%s/%s_tsmf.zip" % (FINAL, year), 'wb')
        o.write(req.content)
        o.close()

    if year > 2001:
        # Do SBW
        myuri = "%s&limit1=yes" % (myuri,)
        req = requests.get(myuri)
        if req.status_code != 200:
            print(("warn_cache[%s] failed with status: %s\n%s"
                   ) % (req.status, myuri))
        else:
            o = open("%s/%s_tsmf_sbw.zip" % (FINAL, year), 'wb')
            o.write(req.content)
            o.close()

if __name__ == "__main__":
    # Lets go!
    if len(sys.argv) == 2:
        get_files(sys.argv[1])
    else:
        yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
        get_files(yesterday.year)

@@ -201,23 +201,45 @@
 %22%22%22%0A
-import requests
+from __future__ import print_function
 %0Aimp
@@ -263,16 +263,33 @@
 rt sys%0A%0A
+import requests%0A%0A
 FINAL = 
@@ -685,32 +685,33 @@
 :%0A        print(
+(
 %22warn_cache%5B%25s%5D 
@@ -729,32 +729,49 @@
  status: %25s%5Cn%25s%22
+%0A               )
  %25 (req.status, 
@@ -768,39 +768,42 @@
 q.status
+_code
 , myuri
-)%0A              
+, req.content)
 )%0A      
@@ -822,32 +822,37 @@
  else:%0A        o
+utput
  = open(%22%25s/%25s_a
@@ -883,32 +883,37 @@
  'wb')%0A        o
+utput
 .write(req.conte
@@ -917,32 +917,37 @@
 ntent)%0A        o
+utput
 .close()%0A%0A    # 
@@ -1079,16 +1079,17 @@
   print(
+(
 %22warn_ca
@@ -1123,16 +1123,33 @@
  %25s%5Cn%25s%22
+%0A               )
  %25 (req.
@@ -1158,31 +1158,34 @@
 atus
+_code
 , myuri
-)%0A              
+, req.content)
 )%0A  
@@ -1193,32 +1193,37 @@
  else:%0A        o
+utput
  = open(%22%25s/%25s_t
@@ -1255,32 +1255,37 @@
  'wb')%0A        o
+utput
 .write(req.conte
@@ -1289,32 +1289,37 @@
 ntent)%0A        o
+utput
 .close()%0A%0A    if
@@ -1565,16 +1565,29 @@
 s, myuri
+, req.content
 ))%0A     
@@ -1608,16 +1608,21 @@
        o
+utput
  = open(
@@ -1678,16 +1678,21 @@
        o
+utput
 .write(r
@@ -1716,16 +1716,21 @@
        o
+utput
 .close()
@@ -1735,34 +1735,43 @@
 ()%0A%0A
-if __name__ == %22__main__%22:
+%0Adef main(argv):%0A    %22%22%22Do Stuff%22%22%22
 %0A   
@@ -1793,20 +1793,16 @@
  if len(
-sys.
 argv) ==
@@ -1827,12 +1827,8 @@
 les(
-sys.
 argv
@@ -1945,12 +1945,60 @@
 erday.year)%0A
+%0A%0Aif __name__ == %22__main__%22:%0A    main(sys.argv)%0A

fix requests error log message