Skip to content

Instantly share code, notes, and snippets.

@calebmadrigal
Created September 6, 2017 19:22
Show Gist options
  • Save calebmadrigal/5f1380561166307f93f0ec7ce71358c1 to your computer and use it in GitHub Desktop.
Save calebmadrigal/5f1380561166307f93f0ec7ce71358c1 to your computer and use it in GitHub Desktop.
Milwaukee Traffic Cameras
import sys
from urllib.request import urlopen
CAM_RANGE = range(188)
CAM_URL_TEMPLATE = 'http://content.dot.wi.gov/travel/milwaukee/cameras/cam{}.jpg'
def get_camera_pic(cam_num, verbose=True):
if type(cam_num) != str or len(cam_num) != 3:
cam_num = str(cam_num).zfill(3)
if verbose:
print('Getting camera {}'.format(cam_num))
image_data = urlopen(CAM_URL_TEMPLATE.format(cam_num)).read()
with open('cam{}.jpg'.format(cam_num), 'wb') as f:
f.write(image_data)
def is_valid_cam_num(val):
try:
return int(val) in CAM_RANGE
except ValueError:
return False
if __name__ == '__main__':
def get_cam_with_exception_handling(cam_num):
try:
get_camera_pic(cam_num)
except Exception as e:
print('Error getting camera {}: {}'.format(cam_num, e))
if len(sys.argv) > 1 and is_valid_cam_num(sys.argv[1]):
get_cam_with_exception_handling(sys.argv[1])
else:
for cam_num in CAM_RANGE:
get_cam_with_exception_handling(cam_num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment