Skip to content

Instantly share code, notes, and snippets.

@zzacharo
Last active March 2, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzacharo/303a8c52f00ce938d651330b26853c4c to your computer and use it in GitHub Desktop.
Save zzacharo/303a8c52f00ce938d651330b26853c4c to your computer and use it in GitHub Desktop.
import re
from invenio_db import db
from invenio_files_rest.models import as_object_version
from cds.modules.records.api import CDSRecord, CDSVideosFilesIterator
from cds.modules.deposit.api import CDSDeposit
from invenio_records.models import RecordMetadata
def _rename_subtitles(record):
"""Rename subtitles."""
# Pattern to extract subtitle's filename and iso language
pattern = re.compile('.*_(?P<iso_lang>[a-zA-Z]{2})\.vtt$')
subtitles = CDSVideosFilesIterator.get_video_subtitles(record)
for subtitle_file in subtitles:
subtitle_obj = as_object_version(subtitle_file['version_id'])
match = pattern.match(subtitle_file['key'])
if match:
subtitle_obj.key = '{}_{}.vtt'.format(self['report_number'][0],
match.group('iso_lang'))
db.session.add(subtitle_obj)
def _rename_master_file(record):
"""Rename master file."""
master_file = CDSVideosFilesIterator.get_master_video_file(record)
master_obj = as_object_version(master_file['version_id'])
master_obj.key = '{}.{}'.format(
record['report_number'][0],
master_file.get('content_type')
or splitext(master_file['key'])[1][1:].lower())
db.session.add(master_obj)
def _rename_and_commit_record(record):
_rename_master_file(record)
_rename_subtitles(record)
record['_files'] = record.files.dumps()
record.commit()
def update_master_file_and_subtitles():
records = RecordMetadata.query.filter(RecordMetadata.json.op('->>')('$schema') == 'https://cdslabs-qa.cern.ch/schemas/records/videos/video/video-v1.0.0.json').all()
for record in records:
record = CDSRecord(record.json, record)
_rename_and_commit_record(record)
deposit = CDSDeposit.get_record(record.depid.object_uuid)
_rename_and_commit_record(deposit)
db.session.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment