import boto3 from boto3.dynamodb.conditions import Key, Attr import config dynamodb = boto3.resource('dynamodb') table = dynamodb.Table(config.TABLE_NAME) def get_isrc(label_id, isrc): """Get isrc from dynamodb mobile metadata.""" isrc_key = _isrc_key_generator(label_id, isrc) response = table.query( KeyConditionExpression=Key('first_id').eq(isrc_key)) if response.get('Count') == 1: return response.get('Items')[0] return 'could not find item' def get_products_isrc_found_on(label_id, isrc): """Get product from dynamodb mobile metadata.""" isrc_key = _isrc_key_generator(label_id, isrc) response = table.query( IndexName='second_id-first_id', KeyConditionExpression=Key('second_id').eq(isrc_key), FilterExpression=Attr('artist_name').not_exists()) if response.get('Count') > 0: return response.get('Items') return 'could not find item' #TO DO Handle for sub def _isrc_key_generator(label_id, isrc): return 'label:{}:isrc:{}'.format(label_id, isrc) def get_product(label_id, product_id): """Get product from dynamodb mobile metadata.""" product_key = _product_key_generator(label_id, product_id) response = table.query( KeyConditionExpression=Key('first_id').eq(product_key), FilterExpression=Attr('artist_name').exists()) if response.get('Count') == 1: return response.get('Items')[0] return 'could not find item' def get_isrcs_on_product(label_id, product_id): """Get product from dynamodb mobile metadata.""" product_key = _product_key_generator(label_id, product_id) response = table.query( KeyConditionExpression=Key('first_id').eq(product_key), FilterExpression=Attr('artist_name').not_exists()) if response.get('Count') > 0: return response.get('Items') return 'could not find item' #TO DO Handle for sub def _product_key_generator(label_id, product_id): return 'label:{}:product:{}'.format(label_id, product_id)