Do not download svg photos

This commit is contained in:
Gautier P 2021-01-19 09:42:26 +01:00
parent c659dc6b76
commit cc4c1ccb18

View File

@ -9,10 +9,12 @@ import collections
import hashlib import hashlib
import os import os
import requests import requests
import logging
from io import BytesIO from io import BytesIO
import PIL.Image import PIL.Image
LOGGER = logging.getLogger(__name__)
class MemoryCache(object): class MemoryCache(object):
""" """
@ -104,6 +106,11 @@ class ImageCache(MemoryCache):
if len(self.map.keys()) > self.max_items: if len(self.map.keys()) > self.max_items:
self.map.popitem(last=False) 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 # Try to load from local folder
if self.storage_dir: if self.storage_dir:
filepath = os.path.join( filepath = os.path.join(
@ -114,13 +121,15 @@ class ImageCache(MemoryCache):
return PIL.Image.open(filepath) return PIL.Image.open(filepath)
# Otherwise, fetch it # Otherwise, fetch it
try: try:
LOGGER.debug(f"Download photo from {url} to {filepath}")
req = requests.get(url) req = requests.get(url)
req.raise_for_status() req.raise_for_status()
image = PIL.Image.open(BytesIO(req.content)) image = PIL.Image.open(BytesIO(req.content))
if self.storage_dir: if filepath:
image.save(filepath, format=image.format) image.save(filepath, format=image.format)
return image return image
except (requests.HTTPError, IOError): except (requests.HTTPError, IOError) as exc:
LOGGER.info(f"Download photo from {url} failed: {exc}")
return None return None
def __init__(self, max_items=200, storage_dir=None): def __init__(self, max_items=200, storage_dir=None):