diff --git a/app/common/static/images/nothumb.jpg b/app/common/static/images/nothumb.jpg deleted file mode 100644 index c186a3c..0000000 Binary files a/app/common/static/images/nothumb.jpg and /dev/null differ diff --git a/app/common/static/images/nothumb.png b/app/common/static/images/nothumb.png new file mode 100644 index 0000000..34dec03 Binary files /dev/null and b/app/common/static/images/nothumb.png differ diff --git a/app/sync/models.py b/app/sync/models.py index 7fedb7f..34282cf 100644 --- a/app/sync/models.py +++ b/app/sync/models.py @@ -429,7 +429,11 @@ class Media(models.Model): def loaded_metadata(self): if self.pk in _metadata_cache: return _metadata_cache[self.pk] - _metadata_cache[self.pk] = json.loads(self.metadata) + try: + unpacked_data = json.loads(self.metadata) + except Exception: + return {} + _metadata_cache[self.pk] = unpacked_data return _metadata_cache[self.pk] @property @@ -460,9 +464,9 @@ class Media(models.Model): dateobj = upload_date if upload_date else self.created datestr = dateobj.strftime('%Y-%m-%d') source_name = slugify(self.source.name) - title = slugify(self.title.replace('&', 'and').replace('+', 'and')) + name = slugify(self.name.replace('&', 'and').replace('+', 'and')) ext = self.source.extension - fn = f'{datestr}_{source_name}_{title}'[:100] + fn = f'{datestr}_{source_name}_{name}'[:100] return f'{fn}.{ext}' @property diff --git a/app/sync/tasks.py b/app/sync/tasks.py index fd19244..b464145 100644 --- a/app/sync/tasks.py +++ b/app/sync/tasks.py @@ -15,7 +15,7 @@ from background_task import background from background_task.models import Task from common.logger import log from .models import Source, Media -from .utils import get_remote_image +from .utils import get_remote_image, resize_image_to_height def delete_index_source_task(source_id): @@ -57,10 +57,7 @@ def index_source_task(source_id): media.metadata = json.dumps(video) upload_date = media.upload_date if upload_date: - if timezone.is_aware(upload_date): - media.published = upload_date - else: - media.published = timezone.make_aware(upload_date) + media.published = timezone.make_aware(upload_date) media.save() log.info(f'Indexed media: {source} / {media}') @@ -76,13 +73,12 @@ def download_media_thumbnail(media_id, url): except Media.DoesNotExist: # Task triggered but the media no longer exists, ignore task return + width = getattr(settings, 'MEDIA_THUMBNAIL_WIDTH', 430) + height = getattr(settings, 'MEDIA_THUMBNAIL_HEIGHT', 240) i = get_remote_image(url) - ratio = i.width / i.height - new_height = getattr(settings, 'MEDIA_THUMBNAIL_HEIGHT', 240) - new_width = math.ceil(new_height * ratio) log.info(f'Resizing {i.width}x{i.height} thumbnail to ' - f'{new_width}x{new_height}: {url}') - i = i.resize((new_width, new_height), Image.ANTIALIAS) + f'{width}x{height}: {url}') + i = resize_image_to_height(i, width, height) image_file = BytesIO() i.save(image_file, 'JPEG', quality=80, optimize=True, progressive=True) image_file.seek(0) diff --git a/app/sync/templates/sync/media.html b/app/sync/templates/sync/media.html index 3a97bda..fa394b4 100644 --- a/app/sync/templates/sync/media.html +++ b/app/sync/templates/sync/media.html @@ -10,7 +10,7 @@
- + {{ m.source }}
{{ m.name }}
{{ m.published|date:'Y-m-d' }} diff --git a/app/sync/utils.py b/app/sync/utils.py index 0db29a6..a5c1cda 100644 --- a/app/sync/utils.py +++ b/app/sync/utils.py @@ -1,5 +1,6 @@ import os import re +import math from pathlib import Path import requests from PIL import Image @@ -59,6 +60,27 @@ def get_remote_image(url): return Image.open(r.raw) +def resize_image_to_height(image, width, height): + ''' + Resizes an image to 'height' pixels keeping the ratio. If the resulting width + is larger than 'width' then crop it. If the resulting width is smaller than + 'width' then stretch it. + ''' + ratio = image.width / image.height + scaled_width = math.ceil(height * ratio) + if scaled_width < width: + # Width too small, stretch it + scaled_width = width + image = image.resize((scaled_width, height), Image.ANTIALIAS) + if scaled_width > width: + # Width too large, crop it + delta = scaled_width - width + left, upper = (delta / 2), 0 + right, lower = (left + width), height + image = image.crop((left, upper, right, lower)) + return image + + def file_is_editable(filepath): ''' Checks that a file exists and the file is in an allowed predefined tuple of diff --git a/app/tubesync/settings.py b/app/tubesync/settings.py index 1a032e9..3ab4050 100644 --- a/app/tubesync/settings.py +++ b/app/tubesync/settings.py @@ -127,6 +127,7 @@ MEDIA_PER_PAGE = 36 INDEX_SOURCE_EVERY = 21600 # Seconds between indexing sources, 21600 = every 6 hours +MEDIA_THUMBNAIL_WIDTH = 430 # Width in pixels to resize thumbnails to MEDIA_THUMBNAIL_HEIGHT = 240 # Height in pixels to resize thumbnails to