diff --git a/dj_elastictranscoder/__init__.py b/dj_elastictranscoder/__init__.py index cd7ca49..a6221b3 100644 --- a/dj_elastictranscoder/__init__.py +++ b/dj_elastictranscoder/__init__.py @@ -1 +1 @@ -__version__ = '1.0.1' +__version__ = '1.0.2' diff --git a/dj_elastictranscoder/transcoder.py b/dj_elastictranscoder/transcoder.py index 571271b..ee7bbb2 100644 --- a/dj_elastictranscoder/transcoder.py +++ b/dj_elastictranscoder/transcoder.py @@ -120,54 +120,69 @@ def __init__( access_key_id=None, access_key_secret=None, pipeline_id=None, + template_id=None, + bucket_name=None, region=None, + location=None, notify_url=None ): - if not access_key_id: - access_key_id = get_setting_or_raise('ALIYUN_TRANSCODE_ACCESS_KEY_ID') - self.access_key_id = access_key_id + self.access_key_id = access_key_id if access_key_id else get_setting_or_raise('ALIYUN_TRANSCODE_ACCESS_KEY_ID') + self.access_key_secret = access_key_secret if access_key_secret else get_setting_or_raise('ALIYUN_TRANSCODE_ACCESS_KEY_SECRET') + self.pipeline_id = pipeline_id if pipeline_id else get_setting_or_raise('ALIYUN_TRANSCODE_ACCESS_KEY_SECRET') + self.template_id = template_id if template_id else get_setting_or_raise('ALIYUN_TRANSCODE_TEMPLATE_ID') + self.bucket_name = bucket_name if bucket_name else get_setting_or_raise('ALIYUN_AUDIO_OSS_BUCKET_NAME') + self.region = region if region else get_setting_or_raise('ALIYUN_TRANSCODE_REGION') + self.location = location if location else get_setting_or_raise('ALIYUN_OSS_LOCATION') + self.notify_url = notify_url if notify_url else get_setting_or_raise('ALIYUN_TRANSCODE_NOTIFY_URL') - if not access_key_secret: - access_key_secret = get_setting_or_raise('ALIYUN_TRANSCODE_ACCESS_KEY_SECRET') - self.access_key_secret = access_key_secret - - if not pipeline_id: - pipeline_id = get_setting_or_raise('ALIYUN_TRANSCODE_PIPELINE_ID') - self.pipeline_id = pipeline_id + from aliyunsdkcore import client + self.client = client.AcsClient(self.access_key_id, self.access_key_secret, self.region) - if not region: - region = get_setting_or_raise('ALIYUN_TRANSCODE_REGION') - self.region = region + def make_outputs(self, filename): + try: + from urllib import quote # Python 2.X + except ImportError: + from urllib.parse import quote # Python 3+ + import json - if not notify_url: - notify_url = get_setting_or_raise('ALIYUN_TRANSCODE_NOTIFY_URL') - self.notify_url = notify_url + return json.dumps([{'OutputObject': quote(filename), + 'TemplateId': self.template_id}]) - from aliyunsdkcore import client + def make_input(self, filename): + try: + from urllib import quote # Python 2.X + except ImportError: + from urllib.parse import quote # Python 3+ + import json - self.client = client.AcsClient(self.access_key_id, self.access_key_secret, self.region) + return json.dumps({'Location': self.location, + 'Bucket': self.bucket_name, + 'Object': quote(filename)}) def start_job(self, obj, transcode_kwargs, message=''): - """ - https://help.aliyun.com/document_detail/57347.html?spm=5176.doc56767.6.724.AJ8z3E - """ + """Invoking task of AliyunTranscoder + transcode_kwargs(dict): Detail how to invoke task of AliyunTranscoder + input(str): A json string by make_input + outputs(str): A json string by make_outputs + + Transcoder reference: https://help.aliyun.com/document_detail/67664.html + """ import json from aliyunsdkmts.request.v20140618 import SubmitJobsRequest request = SubmitJobsRequest.SubmitJobsRequest() request.set_accept_format('json') - request.set_Input(json.dumps(transcode_kwargs.get('input_file'))) - request.set_OutputBucket(transcode_kwargs.get('bucket')) - request.set_OutputLocation(transcode_kwargs.get('oss_location')) - request.set_Outputs(json.dumps(transcode_kwargs.get('outputs'))) + request.set_Input(json.dumps(transcode_kwargs.get('input'))) + request.set_OutputBucket(self.bucket_name) + request.set_OutputLocation(self.location) + request.set_Outputs(transcode_kwargs.get('outputs')) request.set_PipelineId(self.pipeline_id) response = json.loads(self.client.do_action_with_exception(request).decode('utf-8')) content_type = ContentType.objects.get_for_model(obj) - job = EncodeJob() - job.id = response['JobResultList']['JobResult'][0]['Job']['JobId'] - job.content_type = content_type - job.object_id = obj.pk - job.message = message + job = EncodeJob(id=response['JobResultList']['JobResult'][0]['Job']['JobId'], + content_type=content_type, + object_id=obj.pk, + message=message) job.save()