72a8df3ed9
Station icons now get converted to a JPEG with a maxmimum dimension of 290 on icon request. Although we need to implement caching and aspect ratio keeping, this should fix issues with incompatible station icons and HTTPS icon URLs. Adds a new package dependency: Pillow
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
import logging
|
|
import requests
|
|
import io
|
|
|
|
from PIL import Image
|
|
|
|
import ycast.generic as generic
|
|
from ycast import __version__
|
|
|
|
MAX_SIZE = 290
|
|
|
|
|
|
def get_icon_from_url(iconurl):
|
|
# TODO cache icons on disk
|
|
headers = {'User-Agent': generic.USER_AGENT + '/' + __version__}
|
|
try:
|
|
response = requests.get(iconurl, headers=headers)
|
|
except requests.exceptions.ConnectionError as err:
|
|
logging.error("Connection to station icon URL failed (%s)", err)
|
|
return None
|
|
if response.status_code != 200:
|
|
logging.error("Could not get station icon data from %s (HTML status %s)", iconurl, response.status_code)
|
|
return None
|
|
try:
|
|
image = Image.open(io.BytesIO(response.content))
|
|
image = image.convert("RGB")
|
|
image = image.resize((MAX_SIZE, MAX_SIZE), Image.ANTIALIAS) # TODO: keep aspect ratio
|
|
with io.BytesIO() as output_img:
|
|
image.save(output_img, format="JPEG")
|
|
image_conv = output_img.getvalue()
|
|
except Exception as e:
|
|
logging.error("Station icon conversion error (%s)", e)
|
|
return None
|
|
return image_conv
|