[tech] webcam archives

David Basden davidb-f00f @.at.@ rcpt.to
Sun Aug 7 13:07:56 WST 2005


Here is some python code to generate dependency-free static
HTML from the webcam archives, allowing people to browse back
and forth in time like they can with the current dynamic pages.

The only suckage is that it will generate a page for every minute
there is a file, and I haven't written the stuff to call this
script yet. It should be something along the lines of (pseudocode):

	TMPDIR=/tmp/genwebstatic$$
	cd /services/webcam
	mkdir ${TMPDIR}
	find . -name '*jpg' > ${TMPDIR}/candidates
	cat ${TMPDIR}/candidates | sed 's/[^\/]+//' | sort | uniq > \
				${TMPDIR}/picfiles
	cat ${TMPDIR}/picfiles | gen_html_for_each_file_on_stdin
	rm -r ${TMPDIR}

The code below actually takes a filename of a jpg relative to
/services/webcam, and the pseudocode above strips out one of the
levels, but a dummy value can be thrown in if altering the code
is too much hassle (which it probably is). The code below also
doesn't get a list of files from anywhere, but it should be trivial.

That should be the hard bit done anyway.

David

#! /usr/bin/python

"""
generate static HTML pages to browse through webcam archives

mainly useful when burning onto DVD away from the UCC webserver
"""

__author__ = "David Basden <davidb @.at.@ ucc.asn.au>"
__version__ = "1.0"
__licence__ = "General Public Licence v2.0: http://www.gnu.org/"

import datetime

simple_layout = """
<html>
<body bgcolor="white" base="%(basedir)s">
<table>
<tr><th colspan="2"> UCC Webcam </th></tr>
<tr>
<td>	<img src="%(bw)s">	</td>
<td>	<img src="%(colour)s">	</td>
</tr><tr>
<td>	<img src="%(colour1)s">	</td>
<td>	<img src="%(colour2)s">	</td>
</tr>

<p>
 <a href="%(-30d)s">back 30 days</a> |
 <a href="%(-7d)s">back 7 days</a> |
 <a href="%(-1d)s">back 1 day</a> |
 <a href="%(-4h)s">back 4 hours</a> |
 <a href="%(-2h)s">back 2 hours</a> |
 <a href="%(-1h)s">back 1 hours</a> |
 <a href="%(-30m)s">back 30 minutes</a> |
 <a href="%(-10m)s">back 10 minutes</a> |
 <a href="%(-1m)s">back 1 minute</a> 
</p>
<hr>
<p>
 <a href="%(+30d)s">ahead 30 days</a> |
 <a href="%(+7d)s">ahead 7 days</a> |
 <a href="%(+1d)s">ahead 1 day</a> |
 <a href="%(+4h)s">ahead 4 hours</a> |
 <a href="%(+2h)s">ahead 2 hours</a> |
 <a href="%(+1h)s">ahead 1 hours</a> |
 <a href="%(+30m)s">ahead 30 minutes</a> |
 <a href="%(+10m)s">ahead 10 minutes</a> |
 <a href="%(+1m)s">ahead 1 minute</a> 
</p>

</table>
</body>
</html>
"""

class WebcamHTMLFactory(object):
	def __init__(self, layout):
		self.layout = layout
	def fromWebcamImage(self, image):
		min1 = datetime.timedelta(minutes=1)
		min10 = datetime.timedelta(minutes=10)
		min30 = datetime.timedelta(minutes=30)
		hour1 = datetime.timedelta(hours=1)
		hour2 = datetime.timedelta(hours=2)
		hour4 = datetime.timedelta(hours=4)
		day1 = datetime.timedelta(days=1)
		day7 = datetime.timedelta(days=7)
		day30 = datetime.timedelta(days=30)

		cameranames = ('bw', 'colour', 'colour1', 'colour2')
		cameras = {}
		for c in cameranames:
			img = image.clone()
			img.camera = c
			cameras[c] = img.filename
		cameras['basedir'] = image.basedir()
		cameras['+1m'] = (image+min1) 
		cameras['-1m'] = (image-min1) 
		cameras['+10m'] = (image+min10) 
		cameras['-10m'] = (image-min10) 
		cameras['+30m'] = (image+min30) 
		cameras['-30m'] = (image-min30) 
		cameras['+1h'] = (image+hour1) 
		cameras['-1h'] = (image-hour1) 
		cameras['+2h'] = (image+hour2) 
		cameras['-2h'] = (image-hour2) 
		cameras['+4h'] = (image+hour4) 
		cameras['-4h'] = (image-hour4) 
		cameras['+1d'] = (image+day1) 
		cameras['-1d'] = (image-day1) 
		cameras['+7d'] = (image+day7) 
		cameras['-7d'] = (image-day7) 
		cameras['+30d'] = (image+day30) 
		cameras['-30d'] = (image-day30) 

		return self.layout % cameras

class WebcamImage(object):
	def __init__(self, camera, year, month, day, hour, minute, xtn):
		self.camera = camera
		self.year = year
		self.month = month
		self.day = day
		self.hour = hour
		self.minute = minute
		self.xtn = xtn

	def __get_timestamp(self):
		return datetime.datetime(year = int(self.year), \
			month = int(self.month), day = int(self.day), \
			hour = int(self.hour), minute = int(self.minute))
	timestamp = property(__get_timestamp)

	def __get_filename(self):
		return "%s/%s%s/%s/%s/%s.%s" % \
			(self.camera, self.year, self.month,
			self.day, self.hour, self.minute, self.xtn)
	filename = property(__get_filename)

	def __str__(self):
		return '/'.join( (self.year+self.month,self.day, self.hour,self.minute) ) + '.html'
	
	def __add__(self, delta):
		assert delta.__class__ == datetime.timedelta
		newdt = self.timestamp + delta
		return self.__fromTimestamp(newdt)

	def __sub__(self, delta):
		assert delta.__class__ == datetime.timedelta
		newdt = self.timestamp - delta
		return self.__fromTimestamp(newdt)

	def basedir(self):
		levels = len(str(self).split('/')) - 1
		basedir = "./"
		for r in range(levels):
			basedir = basedir + '../'
		return basedir
			

	def __fromTimestamp(self, ts):
		return WebcamImage(self.camera, \
			"%004d" % (ts.year), 
			"%002d" % (ts.month), 
			"%002d" % (ts.day),
			"%002d" % (ts.hour), 
			"%002d" % (ts.minute), self.xtn)
	def clone(self):
		return WebcamImage(self.camera, \
			self.year, self.month, self.day,
			self.hour, self.minute, self.xtn)

class WebcamImageFactory(object):
	def __init__(self):
		self.imagetype = WebcamImage

	def fromPathname(self, pathname):
		ps = pathname.split('/')
		if len(ps) != 5:
			print 'len is ' + str(len(ps))
			return None	
		(camera, yearmonth, day, hour, minutefile) = ps
		year = yearmonth[0:4]
		month = yearmonth[4:]
		minute = minutefile[:minutefile.rindex('.')]
		xtn = minutefile[minutefile.rindex('.')+1:]
		if not (camera and year and month and day and hour\
			and minute and xtn and pathname):
			return None
		return self.imagetype(camera, year, month, day, hour,\
					minute, xtn)



if __name__ == '__main__':
	fn = "fishycam/200410/21/05/23.jpg"
	wi = WebcamImageFactory().fromPathname(fn)
	#print wi
	#print wi + datetime.timedelta(days=30)
	#print wi - datetime.timedelta(minutes=30)
	print WebcamHTMLFactory(simple_layout).fromWebcamImage(wi)



More information about the tech mailing list