From cc4c1ccb180700470ec027366aa43b2fc3af9c77 Mon Sep 17 00:00:00 2001 From: Gautier P Date: Tue, 19 Jan 2021 09:42:26 +0100 Subject: [PATCH] Do not download svg photos --- flatisfy/filters/cache.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/flatisfy/filters/cache.py b/flatisfy/filters/cache.py index 2b60d65..6a85064 100644 --- a/flatisfy/filters/cache.py +++ b/flatisfy/filters/cache.py @@ -9,10 +9,12 @@ import collections import hashlib import os import requests +import logging from io import BytesIO import PIL.Image +LOGGER = logging.getLogger(__name__) class MemoryCache(object): """ @@ -104,6 +106,11 @@ class ImageCache(MemoryCache): if len(self.map.keys()) > self.max_items: self.map.popitem(last=False) + if url.endswith(".svg"): + # Skip SVG photo which are unsupported and unlikely to be relevant + return None + + filepath = None # Try to load from local folder if self.storage_dir: filepath = os.path.join( @@ -114,13 +121,15 @@ class ImageCache(MemoryCache): return PIL.Image.open(filepath) # Otherwise, fetch it try: + LOGGER.debug(f"Download photo from {url} to {filepath}") req = requests.get(url) req.raise_for_status() image = PIL.Image.open(BytesIO(req.content)) - if self.storage_dir: + if filepath: image.save(filepath, format=image.format) return image - except (requests.HTTPError, IOError): + except (requests.HTTPError, IOError) as exc: + LOGGER.info(f"Download photo from {url} failed: {exc}") return None def __init__(self, max_items=200, storage_dir=None):