Unverified Commit 190b1ccb authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

normalise duration

parent 7ca07d59
Pipeline #17984 passed with stages
in 1 minute and 47 seconds
......@@ -18,6 +18,7 @@ import json
import logging
import numbers
import os
import re
import time
import mysql.connector as mariadb
......@@ -117,7 +118,7 @@ def _get_values_from_digital_object(msg, access_status) -> dict:
width = msg['width']
return_values['width'] = width
if 'duration' in msg:
duration = msg['duration']
duration = _normalize_duration(msg['duration'])
return_values['duration'] = duration
return_values['type'] = msg['isDistributedOn']
if 'hasMimeType' in msg:
......@@ -232,14 +233,31 @@ def _has_http_locator(digital_object) -> bool:
return 'locator' in digital_object and digital_object['locator'].startswith('http')
def _normalize_duration(duration) -> int:
if re.fullmatch(r'\d+:\d{2}', duration, re.ASCII):
split = duration.split(':')
return int(split[0]) * 60 + int(split[1])
elif re.fullmatch(r'\d+:\d{2}:\d{2}', duration, re.ASCII):
split = duration.split(':')
return int(split[0]) * 3600 + int(split[1]) * 60 + int(split[2])
elif re.fullmatch(r'\d+:\d{2}:\d{2}\d{3}', duration, re.ASCII):
split = duration.split(':')
return int(split[0]) * 3600 + int(split[1]) * 60 + int(split[2])
elif re.fullmatch(r'\d+.\d{6}', duration, re.ASCII):
return int(duration.split('.')[0])
else:
logging.warning(f'Can\'t parse duration `{duration}`')
return 0
def _create_audio_snippet_entry(record, mariadb_cursor) -> list:
results = []
snippet_record = record.copy()
snippet_record['sig'] = snippet_record['sig'] + '-intro'
# //@formatter:off
snippet_record['duration'] = \
30.0 if float(snippet_record['duration']) >= 30.0 \
else float(snippet_record['duration'])
30 if _normalize_duration(snippet_record['duration']) >= 30 \
else _normalize_duration(snippet_record['duration'])
# //@formatter:on
snippet_record['mimetype'] = 'audio/mpeg'
snippet_record['uri'] = \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment