# ================================================================================== # Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ================================================================================== # # stvblogTranslationsJobStatus # by: Rob Dachowski # For questions or feedback, please contact robdac@amazon.com # # Purpose: The labmda checks to see if the AWS Translate job is done # # Change Log: # 3/1/2021: Initial version # # ================================================================================== import json import boto3 from botocore.exceptions import ClientError import ssmparms as sp import stmparms as stm import stverrors # ================================================================================== # Function:lambda_handler # Purpose: This is the main function for this lambda. It checks the Transcribe Job Status and populates the response into the JSON structure # Parameters: # event - the input json structure # context - lambda context # ================================================================================== def lambda_handler(event, context): print("===> stvblogCheckTranslationJobStatus: " + "\nEvent:" + str(event) ) print( "\t---> Boto Version: ", boto3.__version__ ) # Load the parms from DynamoDB parms = stm.get_stm_parms( event['input']['Outputs']['process']['ProcessName']) if not parms: # We have an issue, so get out raise stvDynamoDBError( "*** Unable to load parms from DynamoDB ***") # set up a shortcut pc = parms['Item']['Config'] pi = parms['Item']['Inputs'] po = parms['Item']['Outputs'] ptgts = parms['Item']['Targets'] pt = event['item']['translate'] jobId = event['item']['translate']['translateJobId'] print( "\t---> Checking on Translation JOBID: ", jobId) # Make sure to seed the path with the input tgt = event # If the jobID == "N/A", then we don't need to translate if jobId != "N/A": try: print( "\t---> Checking Translation Job Status: ", jobId) translate = boto3.client('translate') status = translate.describe_text_translation_job( JobId=jobId ) tgt['item']['translate']['translateJobStatus'] = status['TextTranslationJobProperties']['JobStatus'] # If the status is completed, then we can update things and get out... if status['TextTranslationJobProperties']['JobStatus'] == "COMPLETED": tgt['item']['translate']['translateEndTimestamp'] = status['TextTranslationJobProperties']['EndTime'].strftime("%Y-%m-%d %H-%M-%S") tgt['item']['translate']['s3Uri'] = status['TextTranslationJobProperties']['OutputDataConfig']["S3Uri"] + event['item']['translate']['targetLanguageShort'] + ".translatableText-" + event['input']['Inputs']['sourceLanguageShort'] + '_' + po['process']['uuid'] + '.txt' except ClientError as e: print("*** Error checking Transcription Job ", jobId + " ***") raise stvError("*** Error Code: ", e.response['Error']['Message'] + " ***") else: print( "\t---> Checking Translation Job Status: SRC == TGT, so, nothing to check") tgt['item']['translate']['s3Uri'] = "s3://" + pc['baseBucketName'] + '/' + po['transcribe']['translatableTextKey'] ptgts[event['index']]['translate'] = tgt['item']['translate'] response = tgt # Put the ouptut back into DynamoDB if stm.update_stm_target( event['input']['Outputs']['process']['ProcessName'], ptgts[event['index']], event['index'] ): print('===> stvblogCheckTranslationJobStatus Complete') return response else: raise stvError( "*** Error writing to the stvblog table ***" )