Permalink
Switch branches/tags
x-tern-base v1.0 pre-dmr deploy-2018-02-06 deploy-2018-01-31 deploy-2018-01-24 deploy-2018-01-23 deploy-2018-01-18 deploy-2018-01-10 deploy-2018-01-03 deploy-2017-12-07 deploy-2017-11-22 deploy-2017-11-13 deploy-2017-11-13-2 deploy-2017-11-01 deploy-2017-10-17 deploy-2017-10-17-try2 deploy-2017-10-10 deploy-2017-09-27 deploy-2017-09-22 deploy-2017-09-22-2 deploy-2017-09-21 deploy-2017-09-21-2 deploy-2017-09-19 deploy-2017-09-18 deploy-2017-09-05 deploy-2017-09-01 deploy-2017-09-01-2 deploy-2017-08-10 deploy-2017-07-27 deploy-2017-07-16 deploy-2017-06-22 deploy-2017-06-22-2 deploy-2017-06-17 deploy-2017-06-16 deploy-2017-05-17 deploy-2017-05-03 deploy-2017-04-27 deploy-2017-04-17 deploy-2017-04-10 deploy-2017-04-03 deploy-2017-04-03-2 deploy-2017-03-30 deploy-2017-03-28 deploy-2017-03-22 deploy-2017-03-15 deploy-2017-03-09 deploy-2017-03-06 deploy-2017-03-03 deploy-2017-03-01 deploy-2017-02-22 deploy-2017-02-14 deploy-2017-01-31 deploy-2017-01-25 deploy-2017-01-13 deploy-2016-12-21 deploy-2016-11-29 deploy-2016-11-16 deploy-2016-11-16-2 deploy-2016-11-01 deploy-2016-10-27 deploy-2016-10-24 deploy-2016-10-21 deploy-2016-10-21-5 deploy-2016-10-21-2 deploy-2016-10-19 deploy-2016-10-14 deploy-2016-10-12 deploy-2016-10-11 deploy-2016-09-29 deploy-2016-09-28 deploy-2016-08-04 deploy-2016-08-03 deploy-2016-08-03-try2 deploy-2016-07-20 deploy-2016-07-06 deploy-2016-06-15 deploy-2016-06-07 deploy-2016-05-25 deploy-2016-05-24 deploy-2016-05-20 deploy-2016-05-19 deploy-2016-05-19-2 deploy-2016-05-12 deploy-2016-04-26 deploy-2016-04-20 deploy-2016-04-18 deploy-2016-04-15 deploy-2016-04-14 deploy-2016-04-13.2 deploy-2016-04-13 deploy-2016-04-07 deploy-2016-04-01 deploy-2016-03-30 1.1.9 1.1.7 1.1.6 1.0.1 0.9.12 0.9.11
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time. Cannot retrieve contributors at this time
140 lines (119 sloc) 4.72 KB
# Amara, universalsubtitles.org
#
# Copyright (C) 2013 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.
"""externalsites.syncing.kaltura -- Sync subtitles to/from kaltura"""
from xml.dom import minidom
import requests
from externalsites.exceptions import SyncingError
from externalsites.syncing.kaltura_languages import KalturaLanguageMap
KALTURA_API_URL = 'http://www.kaltura.com/api_v3/'
SESSION_TYPE_USER = 0
SESSION_TYPE_ADMIN = 2
CAPTION_TYPE_DFXP = 2
CAPTION_TYPE_SRT = 1
CAPTION_TYPE_WEBVTT = 3
# partnerData value we set for subtitles that we've synced
PARTNER_DATA_TAG = 'synced-from-amara'
def _node_text(node):
return ''.join(child.nodeValue
for child in node.childNodes
if child.nodeType == child.TEXT_NODE)
def _find_child(node, tag_name):
return node.getElementsByTagName(tag_name)[0]
def _has_child(node, tag_name):
return len(node.getElementsByTagName(tag_name)) > 0
def _check_error(result):
"""Checks if we had an error result."""
if _has_child(result, 'error'):
error = _find_child(result, 'error')
code = _node_text(_find_child(error, 'code'))
message = _node_text(_find_child(error, 'message'))
raise SyncingError("%s: %s" % (code, message))
def _make_request(service, action, data):
params = { 'service': service, 'action': action, }
response = requests.post(KALTURA_API_URL, params=params, data=data)
dom = minidom.parseString(response.content)
try:
result = _find_child(dom, 'result')
except IndexError:
return None
_check_error(result)
return result
def _start_session(partner_id, secret):
result = _make_request('session', 'start', {
'secret': secret,
'partnerId': partner_id,
'type': SESSION_TYPE_ADMIN,
})
return _node_text(result)
def _end_session(ks):
_make_request('session', 'end', { 'ks': ks })
def _find_existing_captionset(ks, video_id, language_code):
language = KalturaLanguageMap.get_name(language_code)
result = _make_request('caption_captionasset', 'list', {
'ks': ks,
'filter:entryIdEqual': video_id,
})
objects = _find_child(result, 'objects')
for item in objects.getElementsByTagName('item'):
partner_data = _find_child(item, 'partnerData')
language_node = _find_child(item, 'language')
if (_node_text(partner_data) == PARTNER_DATA_TAG and
_node_text(language_node) == language):
return _node_text(_find_child(item, 'id'))
return None
def _add_captions(ks, video_id, language_code):
language = KalturaLanguageMap.get_name(language_code)
result = _make_request('caption_captionasset', 'add', {
'ks': ks,
'entryId': video_id,
'captionAsset:language': language,
'captionAsset:partnerData': PARTNER_DATA_TAG,
'captionAsset:format': CAPTION_TYPE_SRT,
'captionAsset:fileExt': 'srt',
})
return _node_text(_find_child(result, 'id'))
def _update_caption_content(ks, caption_id, sub_data):
_make_request('caption_captionasset', 'setcontent', {
'ks': ks,
'id': caption_id,
'contentResource:objectType': 'KalturaStringResource',
'contentResource:content': sub_data,
})
def _delete_captions(ks, caption_id):
_make_request('caption_captionasset', 'delete', {
'ks': ks,
'captionAssetId': caption_id,
})
def update_subtitles(partner_id, secret, video_id, language_code,
srt_data):
ks = _start_session(partner_id, secret)
try:
caption_id = _find_existing_captionset(ks, video_id, language_code)
if caption_id is None:
caption_id = _add_captions(ks, video_id, language_code)
_update_caption_content(ks, caption_id, srt_data)
finally:
_end_session(ks)
def delete_subtitles(partner_id, secret, video_id, language_code):
ks = _start_session(partner_id, secret)
try:
caption_id = _find_existing_captionset(ks, video_id, language_code)
if caption_id is not None:
_delete_captions(ks, caption_id)
finally:
_end_session(ks)