def test_get_products_from_response(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() self.assertTrue(len(products) == 3) papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) p2 = papi2.get_products_from_response() self.assertTrue(len(p2) == 1)
def set_mws_product_information(asins, response): """ Get product from MWS API. Then make a MWS Product API object (Papi). Then set lowest price, lowest fba price, and seller in *response*. param str asins: Amazon's unique identifier for listing param obj response: Response object for this search. """ if len(asins) > 10: sections = sectionize_into_lists(asins) else: sections = [asins] for section in sections: print('searching mws....') try: result = amazon_api.get_lowest_offer_listings_for_asin(section) pprint(result.parsed) except: # I need to deal more gracefully with errors in the request. # I should find out what asin triggered the error and remove it # and try again print('Unable to process {0}'.format(section)) continue papi = PAPI(result.parsed) papi.make_products() for asin in section: try: product = papi.products[asin] response.set_lowest_price_mws(asin, product.lowest_price) response.set_lowest_fba_price_mws(asin, product.lowest_fba_price) response.set_seller(asin, product.get_cheapest_seller()) except: #asin not in papi.products pass
def test_products(self): papi = PAPI(deepcopy(TEST_RESPONSE)) papi.make_products() ps = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(ps[0]) products = papi.products lp = products["B00KUP84L2"].lowest_price self.assertEqual(lp, 17.99)
def test_make_products(self): papi = PAPI(deepcopy(TEST_RESPONSE)) papi.make_products() self.assertTrue(papi.products['B00KUP84L2']) self.assertTrue(papi.products['B00HEATHXK']) self.assertTrue(papi.products['B00KURKYTK']) self.assertTrue(len(papi.products) == 3) self.assertFalse(len(papi.products) == 4)
def test_lowest_prices(self): papi = PAPI(deepcopy(TEST_RESPONSE_SM)) papi.make_products() products = papi.products listings = products['B00M2ALI02'].listings lp, lfbap = papi.lowest_prices(listings) self.assertEqual(lp, 15.99) self.assertEqual(lfbap, 15.99)
def test_lowest_prices(self): papi = PAPI(deepcopy(TEST_RESPONSE_SM)) papi.make_products() products = papi.products listings = products["B00M2ALI02"].listings lp, lfbap = papi.lowest_prices(listings) self.assertEqual(lp, 15.99) self.assertEqual(lfbap, 15.99)
def test_make_products(self): papi = PAPI(deepcopy(TEST_RESPONSE)) papi.make_products() self.assertTrue(papi.products["B00KUP84L2"]) self.assertTrue(papi.products["B00HEATHXK"]) self.assertTrue(papi.products["B00KURKYTK"]) self.assertTrue(len(papi.products) == 3) self.assertFalse(len(papi.products) == 4)
def test_get_offerings_from_product(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) offerings1 = papi.get_offerings_from_product(products[1]) offerings2 = papi.get_offerings_from_product(products[2]) self.assertEqual(len(offerings0), 3) self.assertEqual(len(offerings1), 4) self.assertEqual(len(offerings2), 1) self.assertEqual(offerings0[0]["Price"]["LandedPrice"]["Amount"]["value"], "17.99") # test raise KeyError self.assertRaises(KeyError, papi.get_offerings_from_product, {})
def test_get_offerings_from_product(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) offerings1 = papi.get_offerings_from_product(products[1]) offerings2 = papi.get_offerings_from_product(products[2]) self.assertEqual(len(offerings0), 3) self.assertEqual(len(offerings1), 4) self.assertEqual(len(offerings2), 1) self.assertEqual( offerings0[0]['Price']['LandedPrice']['Amount']['value'], '17.99') # test raise KeyError self.assertRaises(KeyError, papi.get_offerings_from_product, {})
def test_get_seller_from_listing(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) offerings1 = papi.get_offerings_from_product(products[1]) offerings2 = papi.get_offerings_from_product(products[2]) self.assertEqual(papi.get_seller_from_listing(offerings0[0]), "Amazon") self.assertEqual(papi.get_seller_from_listing(offerings0[1]), "Merchant") offerings0[0]["Qualifiers"]["FulfillmentChannel"] = {} # catch KeyError self.assertEqual(papi.get_seller_from_listing(offerings0[0]), None) # Test response as a dict papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) products = papi2.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) seller = papi2.get_seller_from_listing(offerings0[0]) self.assertEqual(seller, "Amazon")
def test_get_asin_from_product(self): # Test response as a list papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() asin0 = papi.get_asin_from_product(products[0]) asin1 = papi.get_asin_from_product(products[1]) asin2 = papi.get_asin_from_product(products[2]) self.assertEqual(asin0, 'B00KUP84L2') self.assertEqual(asin1, 'B00HEATHXK') self.assertEqual(asin2, 'B00KURKYTK') # IndexError and KeyError should raise KeyError self.assertRaises(KeyError, papi.get_asin_from_product, {}) # Test response as a dict papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) products = papi2.get_products_from_response() asin = papi2.get_asin_from_product(products[0]) self.assertEqual(asin, 'B00M2ALI02')
def set_mws_product_information(asins, response): """ Get product from MWS API. Then make a MWS Product API object (Papi). Then set lowest price, lowest fba price, and seller in *response*. param str asins: Amazon's unique identifier for listing param obj response: Response object for this search. """ if len(asins) > 10: sections = sectionize_into_lists(asins) else: sections = [asins] for section in sections: print('searching mws....') try: result = amazon_api.get_lowest_offer_listings_for_asin( section ) pprint(result.parsed) except: # I need to deal more gracefully with errors in the request. # I should find out what asin triggered the error and remove it # and try again print('Unable to process {0}'.format(section)) continue papi = PAPI(result.parsed) papi.make_products() for asin in section: try: product = papi.products[asin] response.set_lowest_price_mws(asin, product.lowest_price) response.set_lowest_fba_price_mws( asin, product.lowest_fba_price ) response.set_seller(asin, product.get_cheapest_seller()) except: #asin not in papi.products pass
def test_products(self): papi = PAPI(deepcopy(TEST_RESPONSE)) papi.make_products() ps = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(ps[0]) products = papi.products lp = products['B00KUP84L2'].lowest_price self.assertEqual(lp, 17.99)
def test_get_asin_from_product(self): # Test response as a list papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() asin0 = papi.get_asin_from_product(products[0]) asin1 = papi.get_asin_from_product(products[1]) asin2 = papi.get_asin_from_product(products[2]) self.assertEqual(asin0, "B00KUP84L2") self.assertEqual(asin1, "B00HEATHXK") self.assertEqual(asin2, "B00KURKYTK") # IndexError and KeyError should raise KeyError self.assertRaises(KeyError, papi.get_asin_from_product, {}) # Test response as a dict papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) products = papi2.get_products_from_response() asin = papi2.get_asin_from_product(products[0]) self.assertEqual(asin, "B00M2ALI02")
def test_get_price_from_listing(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) offerings1 = papi.get_offerings_from_product(products[1]) offerings2 = papi.get_offerings_from_product(products[2]) self.assertEqual(papi.get_price_from_listing(offerings0[0]), 17.99) self.assertEqual(papi.get_price_from_listing(offerings0[1]), 21.39) offerings0[0]['Price']['LandedPrice']['Amount']['value'] = '$17.99' # catch ValueError self.assertEqual(papi.get_price_from_listing(offerings0[0]), '$17.99') # catch KeyError offerings0[0]['Price'] = {} self.assertEqual(papi.get_price_from_listing(offerings0[0]), None) # Test response as a dict papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) products = papi2.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) price = papi2.get_price_from_listing(offerings0[0]) self.assertEqual(price, 15.99)
def test_get_seller_from_listing(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) offerings1 = papi.get_offerings_from_product(products[1]) offerings2 = papi.get_offerings_from_product(products[2]) self.assertEqual(papi.get_seller_from_listing(offerings0[0]), 'Amazon') self.assertEqual(papi.get_seller_from_listing(offerings0[1]), 'Merchant') offerings0[0]['Qualifiers']['FulfillmentChannel'] = {} # catch KeyError self.assertEqual(papi.get_seller_from_listing(offerings0[0]), None) # Test response as a dict papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) products = papi2.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) seller = papi2.get_seller_from_listing(offerings0[0]) self.assertEqual(seller, 'Amazon')
def test_get_price_from_listing(self): papi = PAPI(deepcopy(TEST_RESPONSE)) products = papi.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) offerings1 = papi.get_offerings_from_product(products[1]) offerings2 = papi.get_offerings_from_product(products[2]) self.assertEqual(papi.get_price_from_listing(offerings0[0]), 17.99) self.assertEqual(papi.get_price_from_listing(offerings0[1]), 21.39) offerings0[0]["Price"]["LandedPrice"]["Amount"]["value"] = "$17.99" # catch ValueError self.assertEqual(papi.get_price_from_listing(offerings0[0]), "$17.99") # catch KeyError offerings0[0]["Price"] = {} self.assertEqual(papi.get_price_from_listing(offerings0[0]), None) # Test response as a dict papi2 = PAPI(deepcopy(TEST_RESPONSE_SM)) products = papi2.get_products_from_response() offerings0 = papi.get_offerings_from_product(products[0]) price = papi2.get_price_from_listing(offerings0[0]) self.assertEqual(price, 15.99)