def get_or_create_product(asin): """ Checks the database for an existing ASIN. If not found, try to fetch it using the Amazon Product API. :return: An instance of `products.models.Product` """ try: product = Product.objects.get(asin=asin) except: amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG) az = amazon.lookup(ItemId=asin) product = Product(asin=asin, upc=az.upc, ean=az.ean, description=az.title, image_url=az.large_image_url, amazon_url=az.offer_url) product.save() generate_thumb.delay(product, '600x400') generate_thumb.delay(product, '125x125') product.manufacturer = az.get_attribute('Manufacturer') product.brand = az.get_attribute('Brand') product.model_number = az.get_attribute('Model') product.mpn = az.mpn product.part_number = az.part_number product.sku = az.sku product.isbn = az.isbn product.length = az.get_attribute('ItemDimensions.Length') product.width = az.get_attribute('ItemDimensions.Width') product.height = az.get_attribute('ItemDimensions.Height') product.weight = az.get_attribute('ItemDimensions.Weight') product.save() return product