From d020005721181de4797ad84f979336a12a5df5eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Sun, 9 Aug 2020 17:14:31 +0200 Subject: [PATCH] feat(convert): ajoute la fouille des fichiers audio --- drupal2spip_lal/base/convert.py | 23 +++++++++++++++++++---- requirements/base.txt | 4 ++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/drupal2spip_lal/base/convert.py b/drupal2spip_lal/base/convert.py index 6dea37f..7f0a8ad 100644 --- a/drupal2spip_lal/base/convert.py +++ b/drupal2spip_lal/base/convert.py @@ -9,6 +9,7 @@ from django.conf import settings from django.db.models import F from django.utils.timezone import make_aware, now +import mediafile import request from bs4 import BeautifulSoup as bs @@ -68,6 +69,7 @@ def download(src, filename, force_download): with open(path, 'wb') as fd: for chunk in r.iter_content(chunk_size=128): fd.write(chunk) + return path def fetch_document(src, filename, force_download): @@ -80,11 +82,12 @@ def fetch_document(src, filename, force_download): logger.warn('Echec à creer le dossier: {}'.format(e)) cible = os.path.join(extension, filename) + path = None try: - download(src, cible, force_download) + path = download(src, cible, force_download) except Exception as e: logger.warn('Echec au download: {}'.format(e)) - return extension, cible + return extension, cible, path def fetch_and_remove_logo(article, force_download): @@ -376,7 +379,7 @@ def convert_node(node, options): is_image = upload.file.filemime in images_mimes is_video = upload.file.filemime in video_mimes - extension, fichier = fetch_document( + extension, fichier, path = fetch_document( upload.file.filepath, upload.file.filename, force_download ) @@ -396,7 +399,19 @@ def convert_node(node, options): document_attributes['largeur'] = 1 elif is_audio: document_attributes['media'] = 'audio' - document_attributes['duree'] = 1 + try: + m = mediafile.MediaFile(path) + document_attributes['duree'] = m.length + if m.artist and m.album: + document_attributes['credits'] = '{} / {}'.format( + m.artist, m.album + ) + elif m.artist: + document_attributes['credits'] = m.artist + elif m.album: + document_attributes['credits'] = m.album + except Exception as e: + logger.warn('Echec de lecture: {}'.format(e)) elif is_video: document_attributes['media'] = 'video' document_attributes['duree'] = 1 diff --git a/requirements/base.txt b/requirements/base.txt index 680b91a..1336d51 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -15,3 +15,7 @@ beautifulsoup4 # Logs # ------------------------------------------------------------------------------ colorlog + +# Media +# ------------------------------------------------------------------------------ +mediafile