SHARE
TWEET

whatnick

a guest Aug 25th, 2010 223 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!python
  2. """
  3. This script is designed to fetch the last few available
  4. radar loop information from the BOM radar
  5. """
  6.  
  7. # Let's create a function that downloads a file, and saves it locally.
  8. # This function accepts a file name, a read/write mode(binary or text),
  9. # and the base url.
  10.  
  11. def downloadData(file_name,file_mode,base_url):
  12.     from urllib2 import Request, urlopen, URLError, HTTPError
  13.  
  14.     #create the url and the request
  15.     url = base_url + file_name
  16.     req = Request(url)
  17.     req.add_header("User-Agent",
  18.         "Mozilla/5.0 (X11; U; Linux i686) \
  19.         Gecko/20071127 Firefox/2.0.0.11")
  20.  
  21.     # Open the url
  22.     try:
  23.         f = urlopen(req)
  24.         print "downloading " + url
  25.  
  26.         # Open our local file for writing
  27.         local_file = open(file_name, "w" + file_mode)
  28.         #Write to our local file
  29.         local_file.write(f.read())
  30.         local_file.close()
  31.  
  32.     #handle errors
  33.     except HTTPError, e:
  34.         print "HTTP Error:",e.code , url
  35.     except URLError, e:
  36.         print "URL Error:",e.reason , url
  37.  
  38. # Set the range of images to 1-50.It says 51 because the
  39. # range function never gets to the endpoint.
  40. image_range = range(0,4)
  41.  
  42. def construct_filename(index):
  43.     """
  44.    Create time date based filename
  45.    """
  46.     from datetime import date,timedelta,datetime
  47.     back_delta = timedelta(minutes=index*10)
  48.     back_time = long((datetime.utcnow()-back_delta).strftime("%Y%m%d%H%M"))
  49.     pad_time = back_time - back_time%10
  50.     file_name = str(pad_time)  + ".png"
  51.     return file_name
  52.    
  53.  
  54. # Iterate over image range
  55. for index in image_range:
  56.    
  57.     base_url = 'http://www.bom.gov.au/radar/IDR643.T.'#.201008251130.png'
  58.     #create file name based on known pattern
  59.     file_name = construct_filename(index)
  60.     # Now download the image. If these were text files,
  61.     # or other ascii types, just pass an empty string
  62.     # for the second param ala downloadData(file_name,'',base_url)
  63.     downloadData(file_name,"b",base_url)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top