From 447310a17ef1f90b52677a889d330c8358e2bcb1 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Mon, 30 Nov 2020 12:26:44 +0100 Subject: [PATCH] Add options and bunch of functions to maange the originalDate fields in prismedia, cf #50 --- prismedia/upload.py | 44 ++++++++++++++++++++++++++++++++++++-------- prismedia/utils.py | 10 +++++++--- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/prismedia/upload.py b/prismedia/upload.py index 14b4133..77ad873 100755 --- a/prismedia/upload.py +++ b/prismedia/upload.py @@ -34,6 +34,11 @@ Options: DATE should be in the future --peertubeAt=DATE --youtubeAt=DATE Override publishAt for the corresponding platform. Allow to create preview on specific platform + --originalDate=DATE Configure the video as initially recorded at DATE + DATE should be on the form YYYY-MM-DDThh:mm:ss eg: 2018-03-12T19:00:00 + DATE should be in the past + Default use the last modification date of the file. + --no-originalDate Do not set the initial record field when uploading --thumbnail=STRING Path to a file to use as a thumbnail for the video. Supported types are jpg and jpeg. By default, prismedia search for an image based on video name followed by .jpg or .jpeg @@ -190,11 +195,11 @@ def validateLanguage(language): return False -def validatePublish(publish): +def validatePublishDate(publishDate): # Check date format and if date is future try: now = datetime.datetime.now() - publishAt = datetime.datetime.strptime(publish, '%Y-%m-%dT%H:%M:%S') + publishAt = datetime.datetime.strptime(publishDate, '%Y-%m-%dT%H:%M:%S') if now >= publishAt: return False except ValueError: @@ -202,6 +207,18 @@ def validatePublish(publish): return True +def validateOriginalDate(originalDate): + # Check date format and if date is future + try: + now = datetime.datetime.now() + originalDate = datetime.datetime.strptime(originalDate, '%Y-%m-%dT%H:%M:%S') + if now <= originalDate: + return False + except ValueError: + return False + return True + + def validateThumbnail(thumbnail): supported_types = ['image/jpg', 'image/jpeg'] if os.path.exists(thumbnail) and \ @@ -336,19 +353,25 @@ def main(): Optional('--platform'): Or(None, And(str, validatePlatform, error="Sorry, upload platform not supported")), Optional('--publishAt'): Or(None, And( str, - validatePublish, - error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") + validatePublishDate, + error="Publish Date should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") ), Optional('--peertubeAt'): Or(None, And( str, - validatePublish, - error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") + validatePublishDate, + error="Publish Date should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") ), Optional('--youtubeAt'): Or(None, And( str, - validatePublish, - error="DATE should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") + validatePublishDate, + error="Publish Date should be the form YYYY-MM-DDThh:mm:ss and has to be in the future") ), + Optional('--originalDate'): Or(None, And( + str, + validateOriginalDate, + error="Original date should be the form YYYY-MM-DDThh:mm:ss and has to be in the past") + ), + Optional('--no-originalDate'): bool, Optional('--cca'): bool, Optional('--disable-comments'): bool, Optional('--nsfw'): bool, @@ -387,6 +410,11 @@ def main(): if not options.get('--thumbnail'): options = utils.searchThumbnail(options) + # If after loading NFO we still has no original date and --no-originalDate is not enabled, + # then we need to search from the file + if not options.get('--originalDate') and not options.get('--no-originalDate'): + options['--originalDate'] = utils.searchOriginalDate(options) + try: options = schema.validate(options) except SchemaError as e: diff --git a/prismedia/utils.py b/prismedia/utils.py index d7f85ad..c16a885 100644 --- a/prismedia/utils.py +++ b/prismedia/utils.py @@ -2,12 +2,11 @@ # coding: utf-8 from configparser import RawConfigParser, NoOptionError, NoSectionError -from os.path import dirname, splitext, basename, isfile +from os.path import dirname, splitext, basename, isfile, getmtime import re -from os import devnull -from subprocess import check_call, CalledProcessError, STDOUT import unidecode import logging +import datetime logger = logging.getLogger('Prismedia') @@ -135,6 +134,11 @@ def searchThumbnail(options): return options +def searchOriginalDate(options): + fileModificationDate = getmtime(options.get('--file')) + return datetime.datetime.fromtimestamp(fileModificationDate).isoformat() + + # return the nfo as a RawConfigParser object def loadNFO(filename): try: