import boto3 from boto3.dynamodb.conditions import Key, Attr from utils import ows_search import config dynamodb = boto3.resource('dynamodb') table = dynamodb.Table(config.TABLE_NAME) class SoundRecording(object): def __init__(self, **data): self.isrc = data.get('isrc') self.label_id = data.get('label_id') self.track_name = data.get('track_name') self.artist_name = data.get('artist_name') def __repr__(self): return ''.format(self.track_name) @classmethod def get(cls, label_id, isrc): data = get_isrc(label_id, isrc) data['isrc'] = isrc data['label_id'] = label_id return cls(**data) @classmethod def get_via_key(cls, isrc_key): data = get_isrc_via_key(isrc_key) return cls(**data) @classmethod def search(cls, label_id, term): results = ows_search.search_tracks(term, 'Vendor', label_id).message.get('results') return [cls(**result) for result in results.values()] def products(self): related_products = get_products_isrc_found_on(self.label_id, self.isrc) return [Product(**product) for product in related_products] class Product(object): def __init__(self, **data): self.product_id = data.get('product_id') self.upc = data.get('upc') self.label_id = data.get('label_id') self.release_name = data.get('release_name') self.artist_name = data.get('artist_name') self.version = data.get('version') def __repr__(self): return ''.format(self.release_name, self.version) @classmethod def get(cls, label_id, product_id): data = get_product(label_id, product_id) return cls(**data) def tracks(self): tracks = get_isrcs_on_product(self.label_id, self.product_id) return [Track(self, **track) for track in tracks] class Track(object): def __init__(self, product, **data): self._product = product self._sound_recording_key = data.get('second_id') self._sound_recording = None self.track_number = data.get('track_number') def __repr__(self): return ''.format( self.product(), self.track_number, self.sound_recording()) def sound_recording(self): if not self._sound_recording: self._sound_recording = SoundRecording.get_via_key(self._sound_recording_key) return self._sound_recording def product(self): if not self._product: raise NotImplementedError return self._product def get_isrc(label_id, isrc): """Get isrc from dynamodb mobile metadata.""" isrc_key = _isrc_key_generator(label_id, isrc) return get_isrc_via_key(isrc_key) def get_isrc_via_key(isrc_key): 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 batch_get_products(keys): response = dynamodb.batch_get_item( RequestItems={ config.TABLE_NAME: { 'Keys': [{'first_id': key, 'second_id': key} for key in keys] } }) return response['Responses'][config.TABLE_NAME] 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 'could not find item' items = response.get('Items') product_keys = [item['first_id'] for item in items] return batch_get_products(product_keys) #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) return get_product_via_key(product_key) def get_product_via_key(product_key): 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)