From 55e01632ca0993143c7cd6d460776502cccb62fb Mon Sep 17 00:00:00 2001 From: Tim AtLee Date: Sat, 11 Sep 2021 14:22:40 -0600 Subject: [PATCH 1/3] Read Date property from .md for video files --- AUTHORS | 1 + sigal/gallery.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 498f447..50aaa7b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -54,6 +54,7 @@ alphabetical order): - @trapperhoney - @tudacs - Thomas Misilo +- Tim AtLee - Tim Davies - Tobias Preuss - Toke Høiland-Jørgensen (@tohojo) diff --git a/sigal/gallery.py b/sigal/gallery.py index 220c878..51f9ee8 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -4,6 +4,7 @@ # Copyright (c) 2015 - François D. # Copyright (c) 2017 - Mate Lakat # Copyright (c) 2018 - Edwin Steele +# Copyright (c) 2021 - Tim AtLee # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to @@ -294,7 +295,6 @@ class Video(Media): def __init__(self, filename, path, settings): super().__init__(filename, path, settings) - self.date = self._get_file_date() if not settings['use_orig'] or not is_valid_html5_video(self.src_ext): video_format = settings['video_format'] @@ -304,6 +304,20 @@ class Video(Media): else: self.mime = get_mime(self.src_ext) + @cached_property + def date(self): + """The date from the Date metadata if available, or from the file date.""" + # If no date is found in the metadata, return the file date. + assetdate = datetime.now() + try: + assetdate = datetime.fromisoformat(self.meta['date'][0]) + except: + self.logger.debug( + "Either self.meta.data was not set, or was in an incorrect format : %s", + self.src_filename) + assetdate = self._get_file_date() + return assetdate + class Album: """Gather all informations on an album. From e18ece5ae92f44b42d502a1afcce0df64ca84fba Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Fri, 15 Oct 2021 09:06:57 +0200 Subject: [PATCH 2/3] Add test and docs --- docs/image_information.rst | 3 +++ sigal/gallery.py | 18 +++++++++--------- tests/sample/pictures/video/example video.md | 1 + tests/test_video.py | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 tests/sample/pictures/video/example video.md diff --git a/docs/image_information.rst b/docs/image_information.rst index 529b46c..671ed2d 100644 --- a/docs/image_information.rst +++ b/docs/image_information.rst @@ -6,6 +6,7 @@ Additional information on an image can be given in a file using the `markdown`_ syntax, named ``.md`` (example: ``IMG_5206.md``):: Title: My awesome photo + Date: 2020-01-01T09:00:00 And a description with *Markdown* syntax. @@ -13,6 +14,8 @@ EXIF data is directly extracted, see :ref:`simple-exif-data`. Some meta-data keys are used by Sigal to get the useful informations on the gallery: - *Title*: the image title. +- *Date*: the file date, useful when it cannot be read from the EXIF metadata, + e.g. for videos and some image formats. Any additional meta-data is available in the templates. For instance:: diff --git a/sigal/gallery.py b/sigal/gallery.py index 51f9ee8..048f0ef 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -307,16 +307,16 @@ class Video(Media): @cached_property def date(self): """The date from the Date metadata if available, or from the file date.""" + if 'date' in self.meta: + try: + self.logger.debug("Reading date from image metadata : %s", + self.src_filename) + return datetime.fromisoformat(self.meta['date'][0]) + except Exception: + self.logger.debug("Reading date from image metadata failed : %s", + self.src_filename) # If no date is found in the metadata, return the file date. - assetdate = datetime.now() - try: - assetdate = datetime.fromisoformat(self.meta['date'][0]) - except: - self.logger.debug( - "Either self.meta.data was not set, or was in an incorrect format : %s", - self.src_filename) - assetdate = self._get_file_date() - return assetdate + return self._get_file_date() class Album: diff --git a/tests/sample/pictures/video/example video.md b/tests/sample/pictures/video/example video.md new file mode 100644 index 0000000..850aeaa --- /dev/null +++ b/tests/sample/pictures/video/example video.md @@ -0,0 +1 @@ +Date: 2020-01-01T09:00:00 diff --git a/tests/test_video.py b/tests/test_video.py index f606d93..7d161e4 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -1,4 +1,5 @@ import os +from datetime import datetime from unittest.mock import patch import pytest @@ -48,6 +49,21 @@ def test_process_video(tmpdir): assert process_video(video) == Status.FAILURE +def test_metadata(tmpdir): + base, ext = os.path.splitext(TEST_VIDEO) + + settings = create_settings( + video_format='ogv', + use_orig=True, + orig_link=True, + source=os.path.join(SRCDIR, 'video'), + destination=str(tmpdir), + ) + video = Video(TEST_VIDEO, '.', settings) + assert video.meta == {'date': ['2020-01-01T09:00:00']} + assert video.date == datetime(2020, 1, 1, 9, 0) + + @pytest.mark.parametrize("fmt", ['webm', 'mp4']) def test_generate_video_fit_height(tmpdir, fmt): """largest fitting dimension is height""" From fc396a1c5e4ae55fb72e6344d2b132fd1ec7a52f Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Fri, 15 Oct 2021 09:09:52 +0200 Subject: [PATCH 3/3] Update changelog --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index c5c3b8c..fe56ed4 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,6 +12,7 @@ Sigal now requires Python 3.7+. - Add option ``max_img_pixels`` to allow processing huge images (sets ``PIL.Image.MAX_IMAGE_PIXELS``) [:issue:`431`]. - Add webp to the list of images formats supported by default [:issue:`433`]. +- Allow specifying the file date in the Markdown metadata file [:issue:`447`]. Version 2.2 ~~~~~~~~~~~