#!/usr/bin/python import sys, os, cgi, re import cgitb; cgitb.enable() THIS_SCRIPT = "/cgi-bin/album.py" ALBUMS_PATH = "../albums" SMALL_EXT = ".small.jpg" THUMB_EXT = ".thumb.jpg" EXIF_EXT = ".exif.txt" # Required header that tells the browser how to render the text. print "Content-Type: text/html\n\n" #--------------------------------------------------------------------------- class Photo: def __init__(self, albumName, name): self.name = name self.albumName = albumName def __str__(self): return self.getImageBase() + ":" + self.getUrl() def getAlbumName(self): return self.albumName def getName(self): return self.name def getImageBase(self): return os.path.join(ALBUMS_PATH, self.albumName, self.name) def getThumbFile(self): return self.getImageBase() + THUMB_EXT def getSmallFile(self): return self.getImageBase() + SMALL_EXT def getExifFile(self): return self.getImageBase() + EXIF_EXT def getAlbumUrl(self): return "%s?album=%s" % (THIS_SCRIPT, self.albumName) def getUrl(self): return "%s?album=%s&photo=%s" % (THIS_SCRIPT, self.albumName, self.name) class Album: def __init__(self, name): self.name = name self.date = "?" self.title = "?" self.count = -1 self.parseName() self.photos = [] def parseName(self): m = re.match("^(\d\d)-(\d\d)-(\d\d)-(.*)$", self.name) if not m: return else: self.date = m.group(1) y = m.group(1) mo = m.group(2) d = m.group(3) self.date = mo + "/" + d + "/" + y self.title = m.group(4) self.formatTitle() def formatTitle(self): t = self.title if t == "": return self.title = "" t = t.replace("_", " ") toks = t.split() for tok in toks: tok = tok[0].upper() + tok[1:] self.title += tok + " " self.title = self.title.strip() def getDate(self): return self.date def getTitle(self): return self.title def getName(self): return self.name def getUrl(self): return "%s?album=%s" % (THIS_SCRIPT, self.name) def getCount(self): self.getPhotoList() return self.count def getPhotoList(self): # load on demand if not self.photos == []: return self.photos # currently if we find a thumb, we assume the small is also there ext = ".thumb.jpg" files = os.listdir(os.path.join(ALBUMS_PATH, self.name)) files.sort() photos = [] for f in files: extStart = f.find(ext) if extStart > -1: # strip off the thumb extension photo = Photo(self.name, f[:extStart]) photos.append(photo) self.count = len(photos) return photos #--------------------------------------------------------------------------- def show_albums(): print "
" print "
" print "
" print "

Photo Albums

" print "" albums = os.listdir(ALBUMS_PATH) albums.sort() albums.reverse() for aN in albums: a = Album(aN) photos = a.getPhotoList() #print "%s (%s)" % (THIS_SCRIPT, a.getName(), a.getName(), a.getCount()) print "" print "" % a.getDate() print "" % (THIS_SCRIPT, a.getName(), a.getTitle()) print "" % a.getCount() #print "%s %s (%s)" % (THIS_SCRIPT, a.getName(), a.getDate(), a.getName(), a.getCount()) print "" print "
%s%s%s pics
" print "
" #--------------------------------------------------------------------------- def show_one_album(album): #photos = getPhotoList(album) #for p in photos: # print p #return a = Album(album) photos = a.getPhotoList() print "
" print "
" print "

%s (%s)

" % (a.getTitle(), a.getDate()) print "" \ "[all albums]" % THIS_SCRIPT print "" i = 1 TABLE_COLS = 5 for p in photos: print "" if (i % TABLE_COLS) == 0: print "" i = i + 1 print "
" print "" % (p.getUrl()) print "" % p.getThumbFile() print "
" # put the hyperlink twice to avoid having a in between which produces # the annoying underscore/dash artifact #print "" % (url) #print "%s" % (thumbToViewableName(p)) print "
" print "
" #--------------------------------------------------------------------------- def getPrevAndNextPhotos(photos, curPhoto): prevPhoto = None nextPhoto = None for i in range(len(photos)): p = photos[i] if curPhoto.getName() == p.getName(): if i > 0: prevPhoto = photos[i-1] if i < len(photos)-1: nextPhoto = photos[i+1] break return (prevPhoto, nextPhoto) def show_photo(albumName, photoName): photo = Photo(albumName, photoName) album = Album(albumName) photos = album.getPhotoList() [prevPhoto, nextPhoto] = getPrevAndNextPhotos(photos, photo) #print "
" #print "
" # big table print "" print "" print "
" # left table print "
" # control panel --- print "" # prev if prevPhoto: print "" % prevPhoto.getUrl() else: print "" # current print "" # next if nextPhoto: print "" % nextPhoto.getUrl() else: print "" # end: control panel print "
PrevPrev" print "[thumbnails]" % photo.getAlbumUrl() print "NextNext
" #print "
" # --- # left table cont. print "
" # exif information if it exists if os.path.exists(photo.getExifFile()): print "
" print "" file = open(photo.getExifFile(), 'r') for line in file: line = line.strip() if not line == "": print "%s
" % line print "
" print "
" # end exif table print "
" # end left table # big table cont. print "
" print "" % photo.getSmallFile() print "
" # end big table #print "
" #def show_photo(album, photoName): # photo = Photo(album, photoName) # photos = getPhotoList(album) # [prevPhoto, nextPhoto] = getPrevAndNextPhotos(photos, photo) # #print "
" # print "
" # # # control panel --- # print "" # # prev # if prevPhoto: # print "" % prevPhoto.getUrl() # else: # print "" # # current # print "" # # next # if nextPhoto: # print "" % nextPhoto.getUrl() # else: # print "" # # end: control panel # print "
PrevPrev" # print "Up" % photo.getAlbumUrl() # print "NextNext
" # #print "
" # # --- # # # image # print "" # print "" # print "" # print "
" # print "" % photo.getSmallFile() # print "" # # exif information if it exists # if os.path.exists(photo.getExifFile()): # print "
" # print "" # file = open(photo.getExifFile(), 'r') # for line in file: # line = line.strip() # if not line == "": # print "%s
" % line # print "
" # print "
" # # end # print "
" ## image name #print "
" #print album #print "
" #print photo.getName() print "
" #--------------------------------------------------------------------------- def main(): form = cgi.FieldStorage() if (form.has_key("photo")): show_photo(form["album"].value, form["photo"].value) elif (form.has_key("album")): show_one_album(form["album"].value) else: show_albums() #show_one_album("05-07-ultimate") main()