Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!python
- """
- This script is designed to fetch the last few available
- radar loop information from the BOM radar
- """
- # Let's create a function that downloads a file, and saves it locally.
- # This function accepts a file name, a read/write mode(binary or text),
- # and the base url.
- def downloadData(file_name,file_mode,base_url):
- from urllib2 import Request, urlopen, URLError, HTTPError
- #create the url and the request
- url = base_url + file_name
- req = Request(url)
- req.add_header("User-Agent",
- "Mozilla/5.0 (X11; U; Linux i686) \
- Gecko/20071127 Firefox/2.0.0.11")
- # Open the url
- try:
- f = urlopen(req)
- print "downloading " + url
- # Open our local file for writing
- local_file = open(file_name, "w" + file_mode)
- #Write to our local file
- local_file.write(f.read())
- local_file.close()
- #handle errors
- except HTTPError, e:
- print "HTTP Error:",e.code , url
- except URLError, e:
- print "URL Error:",e.reason , url
- # Set the range of images to 1-50.It says 51 because the
- # range function never gets to the endpoint.
- image_range = range(0,4)
- def construct_filename(index):
- """
- Create time date based filename
- """
- from datetime import date,timedelta,datetime
- back_delta = timedelta(minutes=index*10)
- back_time = long((datetime.utcnow()-back_delta).strftime("%Y%m%d%H%M"))
- pad_time = back_time - back_time%10
- file_name = str(pad_time) + ".png"
- return file_name
- # Iterate over image range
- for index in image_range:
- base_url = 'http://www.bom.gov.au/radar/IDR643.T.'#.201008251130.png'
- #create file name based on known pattern
- file_name = construct_filename(index)
- # Now download the image. If these were text files,
- # or other ascii types, just pass an empty string
- # for the second param ala downloadData(file_name,'',base_url)
- downloadData(file_name,"b",base_url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement