#!/usr/bin/python
# Downloads JPEG images from ALLNET ALL2281 WLAN Camera
import time,urllib,os
threshold=10
user='admin'
password='password'
url='http://%s:%s@192.168.1.117/cgi/mjpg/mjpg.cgi' % (user,password)

def getMJPG(url,readLength=20000):
 stream=urllib.urlopen(url).read(readLength)
 header=stream.split('\r\n\r\n')[0]
 content_length=int(header.split('Length: ')[1])
 if content_length>readLength: return getMJPG(url,readLength=readLength*2)
 data_start=len(header)+4
 return stream[data_start:(data_start+content_length)]

last=False
while True:
 jpeg=getMJPG(url)
 ofn='tower%d.jpg' % time.time()
 open(ofn,'w').write(jpeg)
 print '%d byte written to %s' % (len(jpeg),ofn)
 time.sleep(1)
 if last:
  d=int(os.popen('compare_images %s %s' % (last,ofn)).read().strip()) 
  if d<threshold: os.unlink(last)
 last=ofn