Do not download svg photos

This commit is contained in:
Gautier P 2021-01-19 09:42:26 +01:00
parent c659dc6b76
commit cc4c1ccb18
1 changed files with 11 additions and 2 deletions

View File

@ -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):