From 8b1572167443ecbecbfe08f2ca90f4c50a36ccf5 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 18 Feb 2023 22:32:50 +0000 Subject: [PATCH] Save file containing video transcript --- media.py | 28 +++++++++++++++++++++------- utils.py | 8 ++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/media.py b/media.py index 5dc82260a..0d05fb222 100644 --- a/media.py +++ b/media.py @@ -549,6 +549,19 @@ def _update_etag(media_filename: str) -> None: str(media_filename) + '.etag') +def _store_video_transcript(video_transcript: str, + media_filename: str) -> bool: + """Stores a video transcript + """ + try: + with open(media_filename + '.vtt', 'w+', encoding='utf-8') as fp_vtt: + fp_vtt.write(video_transcript) + return True + except OSError: + print('EX: unable to save video transcript ' + media_filename + '.vtt') + return False + + def attach_media(base_dir: str, http_prefix: str, nickname: str, domain: str, port: int, post_json: {}, image_filename: str, @@ -622,13 +635,14 @@ def attach_media(base_dir: str, http_prefix: str, post_json['attachment'] = [attachment_json] if video_transcript and 'video' in media_type: - video_transcript_json = { - 'mediaType': 'text/vtt', - 'name': system_language, - 'type': 'Document', - 'url': http_prefix + '://' + domain + '/' + media_path - } - post_json['attachment'].append(video_transcript_json) + if _store_video_transcript(video_transcript, media_filename): + video_transcript_json = { + 'mediaType': 'text/vtt', + 'name': system_language, + 'type': 'Document', + 'url': http_prefix + '://' + domain + '/' + media_path + } + post_json['attachment'].append(video_transcript_json) if base_dir: if media_type.startswith('image/'): diff --git a/utils.py b/utils.py index b6c585cda..e8324fda2 100644 --- a/utils.py +++ b/utils.py @@ -1787,6 +1787,8 @@ def can_reply_to(base_dir: str, nickname: str, domain: str, def _remove_attachment(base_dir: str, http_prefix: str, domain: str, post_json: {}): + """Removes media files for an attachment + """ if not post_json.get('attachment'): return if not post_json['attachment'][0].get('url'): @@ -1802,6 +1804,12 @@ def _remove_attachment(base_dir: str, http_prefix: str, domain: str, except OSError: print('EX: _remove_attachment unable to delete media file ' + str(media_filename)) + if os.path.isfile(media_filename + '.vtt'): + try: + os.remove(media_filename + '.vtt') + except OSError: + print('EX: _remove_attachment unable to delete media transcript ' + + str(media_filename) + '.vtt') etag_filename = media_filename + '.etag' if os.path.isfile(etag_filename): try: