#! /usr/bin/env python # pyPanda # PandaCam Viewer # Version 0.6 # David Clark # silenus@telus.net # If the camdown.jpg and camrefresh.jpg images were not installed # to the same directory as the pypanda.py script, indicate their new # location below: image_location='/usr/local/share/pypanda/' url = 'http://zoorr.camzone.com/cams/zoocam/.camzone?87' import urllib, sys, os, StringIO def create_display(): pygame.display.init() pygame.display.set_caption('PyPanda 0.6') display = pygame.display.set_mode((306, 240)) pygame.event.set_blocked( (ACTIVEEVENT, KEYUP, MOUSEMOTION, MOUSEBUTTONUP, JOYHATMOTION, JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONUP, JOYBUTTONDOWN) ) return display def get_picture(cam): picture = '' for i in (0,1,2): # skip header eoftest = cam.readline() if eoftest == '': # we're out of frames return 0 elif eoftest == 'Content-type: image/gif\012': # the cam is down return -1 # for maintainence while 1: # per line line = cam.readline() picture = picture + line if line =="--CamZoneRS\n": return picture def display_picture(display, IO_str): try: src = pygame.image.load(IO_str).convert(display) except RuntimeError: return 0 display.blit(src, (0,0)) pygame.display.flip() IO_str.close() # might not be necessary return 1 def display_message(display, src_filename): filename = image_location + src_filename src = pygame.image.load(filename).convert(display) display.blit(src, (0,0)) pygame.display.flip() def check_for_quit(): if pygame.event.poll().type in (NOEVENT, MOUSEBUTTONDOWN): return 0 else: return 1 def wait_for_decision(cam): input = pygame.event.wait().type if input in (KEYDOWN, QUIT): quit(cam) elif input == MOUSEBUTTONDOWN: return else: print "Serious error! Event type:" + str(input) def quit(cam): pygame.quit() if cam: cam.close() sys.exit() # main listing import pygame, pygame.image from pygame.locals import * display = create_display() while 1: try: cam = urllib.urlopen(url) # is the cam responding at all? except: display_message(display, 'camdown.jpg') wait_for_decision(None) continue for line in range(0,8): # skip the MIME header cam.readline() while 1: # for each frame picture = get_picture(cam) if picture == 0: # if the feed has stopped display_message(display, 'camrefresh.jpg') # because we're out of frames wait_for_decision(cam) cam.close() break elif picture == -1: # if we've received the display_message(display, 'camdown.jpg') # cam-down gif wait_for_decision(cam) cam.close() break IO_str = StringIO.StringIO(picture) if display_picture(display, IO_str) == 0: # if the feed isn't there display_message(display, 'camdown.jpg') # to begin with wait_for_decision(cam) cam.close() if check_for_quit() == 1: quit(cam)