Created
July 17, 2013 03:57
-
-
Save michael-lazar/6017585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import simplejson as json | |
import urllib, urllib2 | |
from datetime import datetime | |
# Generate your own key from.. | |
# http://www.wunderground.com/weather/api/ | |
API_KEY = '<your api key here>' | |
def get_webcams(area_code, api_key): | |
area_code = str(area_code) | |
# Make a request to the Weather Underground API | |
url = ('http://api.wunderground.com/api/'+api_key+ | |
'/webcams/q/'+area_code+'.json') | |
f = urllib2.urlopen(url) | |
reply = json.loads(f.read()) | |
f.close() | |
# Check that the request was successful | |
try: | |
webcams = reply['webcams'] | |
except: | |
print reply['error'] | |
return | |
# Get the current time as a string | |
timestamp = datetime.now() | |
timestamp = timestamp.strftime('%Y-%m-%d %H:%M:%S') | |
# Create a folder to store the captures | |
folderpath = os.path.join(area_code,timestamp) | |
os.makedirs(folderpath) | |
# For each webcam in the reply, get the source url | |
for cam in webcams: | |
camid = cam['camid'] | |
url = cam['CURRENTIMAGEURL'] | |
print camid | |
# Retrieve the image and save it to a file | |
filepath = os.path.join(area_code,timestamp,camid) | |
urllib.urlretrieve(url, filepath) | |
if __name__=='__main__': | |
get_webcams(60120, API_KEY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment