class TestImageCache(unittest.TestCase): """ Checks image cache is working as expected. """ def __init__(self, *args, **kwargs): self.IMAGE_CACHE = ImageCache( # pylint: disable=invalid-name storage_dir=tempfile.mkdtemp(prefix="flatisfy-")) super(TestImageCache, self).__init__(*args, **kwargs) def test_invalid_url(self): """ Check that it returns nothing on an invalid URL. """ # See https://framagit.org/phyks/Flatisfy/issues/116. self.assertIsNone( self.IMAGE_CACHE.get("https://httpbin.org/status/404")) self.assertIsNone( self.IMAGE_CACHE.get("https://httpbin.org/status/500")) def test_invalid_data(self): """ Check that it returns nothing on an invalid data. """ # See https://framagit.org/phyks/Flatisfy/issues/116. self.assertIsNone(self.IMAGE_CACHE.get("https://httpbin.org/"))
def download_images(flats_list, config): """ Download images for all flats in the list, to serve them locally. :param flats_list: A list of flats dicts. :param config: A config dict. """ photo_cache = ImageCache(storage_dir=os.path.join(config["data_directory"], "images")) for flat in flats_list: for photo in flat["photos"]: # Download photo image = photo_cache.get(photo["url"]) # And store the local image # Only add it if fetching was successful if image: photo["local"] = photo_cache.compute_filename(photo["url"])