Station icons: Basic station icon caching
Storage path for the station icon cache is: ~/.ycast/cache/icons
This commit is contained in:
parent
4b45aa58d0
commit
87c7753fee
3 changed files with 51 additions and 24 deletions
|
@ -1,6 +1,9 @@
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
USER_AGENT = 'YCast'
|
USER_AGENT = 'YCast'
|
||||||
|
VAR_PATH = os.path.expanduser("~") + '/.ycast'
|
||||||
|
CACHE_PATH = VAR_PATH + '/cache'
|
||||||
|
|
||||||
|
|
||||||
class Directory:
|
class Directory:
|
||||||
|
@ -31,3 +34,15 @@ def get_stationid_without_prefix(uid):
|
||||||
logging.error("Could not extract stationid (Invalid station id length)")
|
logging.error("Could not extract stationid (Invalid station id length)")
|
||||||
return None
|
return None
|
||||||
return uid[3:]
|
return uid[3:]
|
||||||
|
|
||||||
|
|
||||||
|
def get_cache_path(cache_name):
|
||||||
|
cache_path = CACHE_PATH + '/' + cache_name
|
||||||
|
try:
|
||||||
|
os.makedirs(cache_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
except PermissionError:
|
||||||
|
logging.error("Could not create cache folders (%s) because of access permissions", cache_path)
|
||||||
|
return None
|
||||||
|
return cache_path
|
||||||
|
|
|
@ -262,9 +262,9 @@ def get_station_icon():
|
||||||
if not hasattr(station, 'icon') or not station.icon:
|
if not hasattr(station, 'icon') or not station.icon:
|
||||||
logging.warning("No icon information found for station with id '%s'", stationid)
|
logging.warning("No icon information found for station with id '%s'", stationid)
|
||||||
abort(404)
|
abort(404)
|
||||||
station_icon = station_icons.get_icon_from_url(station.icon)
|
station_icon = station_icons.get_icon(station)
|
||||||
if not station_icon:
|
if not station_icon:
|
||||||
logging.error("Could not convert station icon for station with id '%s'", stationid)
|
logging.error("Could not get station icon for station with id '%s'", stationid)
|
||||||
abort(404)
|
abort(404)
|
||||||
response = make_response(station_icon)
|
response = make_response(station_icon)
|
||||||
response.headers.set('Content-Type', 'image/jpeg')
|
response.headers.set('Content-Type', 'image/jpeg')
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import io
|
import io
|
||||||
|
import os
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
@ -8,31 +9,42 @@ import ycast.generic as generic
|
||||||
from ycast import __version__
|
from ycast import __version__
|
||||||
|
|
||||||
MAX_SIZE = 290
|
MAX_SIZE = 290
|
||||||
|
CACHE_NAME = 'icons'
|
||||||
|
|
||||||
|
|
||||||
def get_icon_from_url(iconurl):
|
def get_icon(station):
|
||||||
# TODO cache icons on disk
|
cache_path = generic.get_cache_path(CACHE_NAME)
|
||||||
headers = {'User-Agent': generic.USER_AGENT + '/' + __version__}
|
if not cache_path:
|
||||||
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
|
return None
|
||||||
|
station_icon_file = cache_path + '/' + station.id
|
||||||
|
if not os.path.exists(station_icon_file):
|
||||||
|
logging.debug("Station icon cache miss. Fetching and converting station icon for station id '%s'", station.id)
|
||||||
|
headers = {'User-Agent': generic.USER_AGENT + '/' + __version__}
|
||||||
|
try:
|
||||||
|
response = requests.get(station.icon, 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)", station.icon, response.status_code)
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
image = Image.open(io.BytesIO(response.content))
|
||||||
|
image = image.convert("RGB")
|
||||||
|
if image.size[0] > image.size[1]:
|
||||||
|
ratio = MAX_SIZE / image.size[0]
|
||||||
|
else:
|
||||||
|
ratio = MAX_SIZE / image.size[1]
|
||||||
|
image = image.resize((int(image.size[0] * ratio), int(image.size[1] * ratio)), Image.ANTIALIAS)
|
||||||
|
image.save(station_icon_file, format="JPEG")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Station icon conversion error (%s)", e)
|
||||||
|
return None
|
||||||
try:
|
try:
|
||||||
image = Image.open(io.BytesIO(response.content))
|
with open(station_icon_file, 'rb') as file:
|
||||||
image = image.convert("RGB")
|
image_conv = file.read()
|
||||||
if image.size[0] > image.size[1]:
|
except PermissionError:
|
||||||
ratio = MAX_SIZE / image.size[0]
|
logging.error("Could not access station icon file in cache (%s) because of access permissions",
|
||||||
else:
|
station_icon_file)
|
||||||
ratio = MAX_SIZE / image.size[1]
|
|
||||||
image = image.resize((int(image.size[0] * ratio), int(image.size[1] * ratio)), Image.ANTIALIAS)
|
|
||||||
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 None
|
||||||
return image_conv
|
return image_conv
|
||||||
|
|
Loading…
Reference in a new issue