def _get_shorthash(self, photo_id): """Get a shorthash for a Flickr photo.""" photo = self.photos[photo_id] req = urllib2.Request( url=photo['url_o'], headers={'Range': "bytes=-%u" % TAILHASH_SIZE}, ) f = urllib2.urlopen(req) if f.code != urllib2.httplib.PARTIAL_CONTENT: raise IOError("Got status %s from Flickr" % f.code) return photo_id, make_shorthash( f.read(), photo['originalformat'], int(f.headers['content-range'].split('/')[-1]), int(photo['o_width']), int(photo['o_height']))
def __getitem__(self, filename): if filename in self: last_modified, shorthash = pif.dictdb.DictDB.__getitem__( self, filename) else: last_modified, shorthash = None, None # Abort if the file hasn't been modified. statinfo = os.stat(filename) if last_modified == statinfo.st_mtime: return shorthash # Validate the potential image. try: image = PIL.Image.open(filename) image.verify() except IOError: raise KeyError(filename) # Gather the metadata to create the shorthash. with file(filename) as f: try: f.seek(-TAILHASH_SIZE, 2) except IOError: # Maybe the file doesn't have TAILHASH_SIZE bytes to spare... f.seek(0) tailhash = f.read(TAILHASH_SIZE) shorthash = make_shorthash( tailhash, image.format, statinfo.st_size, image.size[0], image.size[1], ) # Cache the shorthash. self[filename] = (statinfo.st_mtime, shorthash) return shorthash
def setUp(self): self.tempdir = tempfile.mkdtemp() self.index = FileIndex(os.path.join(self.tempdir, 'index')) # Build some test images! self.shorthashes = {} for num, meta in enumerate(self.FILES.iteritems()): basename, format = meta fn = os.path.join(self.tempdir, basename) i = PIL.Image.new('RGB', (self.IMAGE_WIDTH, self.IMAGE_HEIGHT)) # Draw something different so the hash is unique. d = PIL.ImageDraw.ImageDraw(i) d.text((0, 0), "Test %u" % num) i.save(fn) # Gather the metadata to create the shorthash. statinfo = os.stat(fn) with file(fn) as f: f.seek(-pif.TAILHASH_SIZE, 2) tailhash = f.read() self.shorthashes[fn] = pif.make_shorthash( tailhash, format, statinfo.st_size, self.IMAGE_WIDTH, self.IMAGE_HEIGHT, ) # Load the image into the cache. self.index[fn]