"""Coverart logic.""" from typing import List from artwork import config from artwork.constants.features import V2_COVERART from artwork.logic.features import is_enabled from artwork.models import ows_assets class CoverArtClient: """Coverart client class.""" def get_v1_path(self, product_ids: List[str], image_format: str) -> List[dict]: """Get v1 artwork path.""" items = [] assets = ows_assets.get( '/image/{image_type}/{image_format}/location'.format( image_type='product', image_format=image_format ), {'ids': ','.join(product_ids)}, ) for product_id, url in assets.message.items(): items.append( { 'productId': product_id, 'url': url, 'imageFormat': image_format, 'apiVersion': 'v1', } ) return items def get_v2_path(self, product_ids: List[str], image_format: str) -> List[dict]: """Get v2 artwork path.""" items = [] assets = ows_assets.get( '/image/{image_type}/{image_format}/location'.format( image_type='product', image_format=image_format ), {'ids': ','.join(product_ids), 'fallback': 0}, ) for product_id, url in assets.message.items(): items.append( { 'productId': product_id, 'url': url, 'imageFormat': image_format, 'apiVersion': 'v2', } ) return items def add_default_images( self, product_ids: List[str], paths: List[dict], image_format: str, version: str ) -> List[dict]: """Add default placeholder images. Args: product_ids (list): A list of product ids. paths (list): A list of image paths. image_format (str): An image format. version (str): Version of api responding with. Returns: list(dict): Returns the artwork objects """ all_paths = paths.copy() all_product_ids = [x['productId'] for x in all_paths] missing_product_ids = [x for x in product_ids if x not in all_product_ids] for product_id in missing_product_ids: cdn_url = config.CDN_URL if image_format == 'large_cover': cdn_url = config.LARGE_COVER_CDN_URL elif image_format == 'xlarge_cover': cdn_url = config.XLARGE_COVER_CDN_URL all_paths.append( { 'productId': product_id, 'url': '{}/placeholder.jpg'.format(cdn_url), 'imageFormat': image_format, 'apiVersion': version, } ) return all_paths def get_path(self, product_ids: List[str], image_format: str) -> List[dict]: """Get artwork path. Checks all locations and prioritizes in order of v2, v1, legacy. Args: product_ids (list): A list of product ids. image_format (str): An image format. Returns: list(dict): Returns the artwork objects """ if is_enabled(V2_COVERART, {}): v2_paths = self.get_v2_path(product_ids, image_format) v2_product_ids = [x['productId'] for x in v2_paths] missing_product_ids = [x for x in product_ids if x not in v2_product_ids] v1_paths = [] if missing_product_ids: v1_paths = self.get_v1_path(missing_product_ids, image_format) all_paths = v1_paths + v2_paths return self.add_default_images(product_ids, all_paths, image_format, 'v2') v1_paths = self.get_v1_path(product_ids, image_format) return self.add_default_images(product_ids, v1_paths, image_format, 'v1') def get_cover_art(product_ids: List[str], image_format: str) -> list: """Get artwork path entrypoint function.""" c = CoverArtClient() return c.get_path(product_ids, image_format)