"""Common S3 DDEX helper functions.""" import json import boto3 from ddex_ingester_common.schemas.state_machine_schema import TrackSchema def load_ddex_json(event): """Get DDEX JSON data from S3 file. Args: event (event): Event received by lambdas Returns: json """ bucket = event.get('bucket') key = event.get('key') s3_client = boto3.client('s3') split_key = f"{key.rsplit('/', 1)[0]}/" json_file_path = f'{split_key}parsed_ddex.json' ddex_json_data = s3_client.get_object( Bucket=bucket, Key=json_file_path )['Body'].read().decode('utf-8') return json.loads(ddex_json_data) def get_s3_track(parsed_ddex, context_track): """Get S3 track with the same ISRC as a given context track. Args: parsed_ddex (object): S3 DDEX object context_track (Context Track): Track with ISRC to match Returns: S3 Track """ for s3_track in parsed_ddex.tracks: if context_track.isrc == s3_track.isrc: return s3_track raise Exception('No matching S3 track for context track: ' f'{TrackSchema().dump(context_track)}')