"""Ows-asset-transcoder api calls module.""" from owsrequest import request from podcast.constants import asset_types as asset_types_consts from podcast.utils.api_utils import get_ows_headers from podcast.utils.exc import OwsError OWS_ASSET_TRANSCODER = 'ows-asset-transcoder' def get_episode_assets(episode_id, signed_url_duration): """Return assets for an episode that are already in assets_final. Args: episode_id (int): The unique identifier of the episode Returns: dict: dict with episode assets """ return get_simple_asset_dict(_get_assets(episode_id, 'episode', signed_url_duration)) def get_episode_input_audio(episode_id): """Return assets for an episode that are already in assets_final. Args: episode_id (int): The unique identifier of the episode Returns: dict: dict with episode assets """ assets = _get_assets(episode_id, 'episode') for asset in assets: if asset['media_type'] == asset_types_consts.MEDIA_AUDIO_TYPE: return '{}.{}'.format(asset['filename'], asset['asset_type'].lower()) return None def get_object_asset_by_id_and_type(object_id, object_type, asset_type): """Return object asset based on object_id, object_type, and asset_type. Used for following use cases: 1) Episode audio wav asset upload whose status is enoding_completed in asset status table. 2) Podcast artwork asset upload whose status is enoding_completed in asset status table. The below OAT endpoint only accepts object_type as episode and asset_type as WAV. Args: object_id (int): unique identifier of the episode object_type (str): type of object like episode or podcast asset_type (str or list of str): file type like 'WAV' for episode or ['TIF', 'JPG'] for podcast Returns: dict: contains object asset from asset upload table in OAT db. """ oat_response = request.get( OWS_ASSET_TRANSCODER, '/asset-by-id-and-type', params={ 'object_id': object_id, 'object_type': object_type, 'asset_type': asset_type }, headers=get_ows_headers() ) if oat_response.status_code > 399: raise OwsError.response_error(oat_response) return oat_response.json() def get_podcast_assets(podcast_id): """Return artwork asset for a podcast that are already in assets_final. Args: podcast_id (int): The unique identifier of the podcast Returns: dict: dict with podcast asset """ return get_simple_asset_dict(_get_assets(podcast_id, 'podcast')) def get_ad_assets(ad_id): """Return assets for an advertisement that are already in assets_final. Args: ad_id (int): The unique identifier of the ad Returns: dict: dict with ad assets """ return get_simple_asset_dict(_get_assets(ad_id, 'adupload')) def commit_or_delete_asset(filename, object_id, asset_type, object_type): """Commit or delete an asset based on presence of filename. Args: object_id (int): The unique identifier of the asset to attach to Returns: dict: dict with podcast asset """ if filename: commit(filename, object_id) else: delete_asset(asset_type, object_id, object_type) def delete_asset(frontend_asset_type, object_id, object_type): """Delete asset. Args: frontend_asset_type (str): artwork | audio object_id (str): Unigue identifier of podcast or episode object_type (str): podcast | episode """ response = request.delete( OWS_ASSET_TRANSCODER, '/upload', params={ 'asset_type': frontend_asset_type, 'object_id': object_id, 'object_type': object_type }, headers=get_ows_headers() ) if response.status_code > 399: raise OwsError.response_error(response) return response def commit(filename, object_id): """Commit asset. Args: filename (str): unique id of asset object_id (str): Unigue identifier item to attach to """ response = request.post( OWS_ASSET_TRANSCODER, '/commit/{}'.format(filename), json={ 'object_id': str(object_id) }, headers=get_ows_headers() ) if response.status_code > 399: raise OwsError.response_error(response) return response def _get_assets(object_id, object_type, signed_url_duration=asset_types_consts.DEFAULT_SIGNED_URL_DURATION): oat_response = request.get( OWS_ASSET_TRANSCODER, '/assets-by-ids-and-types', params={ 'object_ids': [object_id], 'object_types': [object_type], 'signed_url_duration': signed_url_duration }, headers=get_ows_headers() ) if oat_response.status_code > 399: raise OwsError.response_error(oat_response) return oat_response.json()['items'] def get_simple_asset_dict(assets): """Merge asset_upload and relevant asset_final row into one row.""" result = {} for asset in assets: for final_asset in asset['asset_finals']: is_image = asset['media_type'] == asset_types_consts.MEDIA_IMAGE_TYPE is_xlarge_cover_art = final_asset['asset_subtype'] == asset_types_consts.SUBTYPE_XLARGE_COVER if is_image and is_xlarge_cover_art: result[asset_types_consts.SUBTYPE_XLARGE_COVER] = final_asset['filename'] result['x_large_image_url'] = final_asset['url'] if final_asset['asset_type'] == asset_types_consts.TYPE_FILE_FLAC: result['audio_duration'] = final_asset['duration'] if final_asset['asset_type'] == asset_types_consts.TYPE_FILE_MP3: result['audio_path'] = final_asset['filename'] result['audio_url'] = final_asset['url'] return result def replicate_episodes_audio_assets(objects): """Replicate audio assets. Args: objects (list of dict): Dict of original_episode_id & episode_id """ response = request.post( OWS_ASSET_TRANSCODER, '/replicate-episodes-audio-assets', json={ 'objects': objects }, headers=get_ows_headers() ) return response