def get_page(domain, asin, locale): ecs.setLocale(locale) try: pages = ecs.ItemLookup(asin, ResponseGroup="Medium") except ecs.InvalidParameterValue, e: raise UserException, str(e)
def testVariations(self): shirts = ecs.ItemLookup('B000EI6M5A', ResponseGroup='Variations') self.assertEqual(len(shirts), 1) shirt = shirts[0] self.assertEqual(shirt.Variations[0].ASIN, 'B000EG9PLU') self.assertEqual(shirt.Variations[5].ASIN, 'B000EG5DUM') self.assertEqual(shirt.VariationSummary.HighestPrice.Amount, '699') self.assertEqual(shirt.VariationSummary.LowestPrice.Amount, '699')
def testSubjects(self): books = ecs.ItemLookup(self.ItemId, ResponseGroup='Subjects') self.assertEqual(len(books), 1) book = books[0] subs = book.Subjects self.assertEqual(len(subs), 10) self.assertEqual(subs[0], 'Programming languages') self.assertEqual(subs[2], 'Computers')
def testReviews(self): books = ecs.ItemLookup(self.ItemId, ResponseGroup='Reviews') self.assertEqual(len(books), 1) book = books[0] self.assertEqual(len(book.CustomerReviews), 68) # arbitary reviewer self.assertEqual(book.CustomerReviews[34].Reviewer.Name, 'Sameer') self.assertEqual(book.CustomerReviews[12].Summary, 'An Essential Python Book for Python Programmers')
def testOfferFull(self): txs = ecs.ItemLookup('B000BI7NHY', MerchantId='All', Condition='All', ResponseGroup='OfferFull') self.assertEqual(len(txs), 1) tx = txs[0] # arbitary seller self.assertNotEqual(tx.Offers[23].Merchant.MerchantId, None)
def testSmall(self): books = ecs.ItemLookup(self.ItemId, ResponseGroup='Request,Small') self.assertEqual(len(books), 1) book = books[0] self.assertNotEqual(book, None) self.assertEqual(book.ASIN, '0596009259') self.assertEqual(book.Title, 'Programming Python') self.assertEqual(book.Manufacturer, "O'Reilly Media, Inc.") self.assertEqual(book.ProductGroup, 'Book') self.assertEqual(book.Author, 'Mark Lutz')
def testTracks(self): cds = ecs.ItemLookup('B0000042H4', ResponseGroup='Tracks') self.assertEqual(len(cds), 1) cd = cds[0].Tracks self.assertEqual(len(cd.Disc), 14) self.assertEqual( cd.Disc[13].Track[5], 'Gotterdammerung: Dritter Aufzug, Zweite Szene: Hoiho!') self.assertEqual( cd.Disc[4].Track[3], 'Die Walkure: Zweiter Aufzug, Funfte Szene: Zauberfest bezahmt ein Schlaf der Holden Schmerz und Harm' )
def testSimilarities(self): books = ecs.ItemLookup(self.ItemId, ResponseGroup='Similarities') self.assertEqual(len(books), 1) book = books[0] sim = book.SimilarProducts self.assertEqual(len(sim), 5) self.assertEqual(sim[0].Title, 'Learning Python, Second Edition') self.assertEqual(sim[1].Title, 'Python Cookbook') self.assertEqual( sim[3].Title, "Python Essential Reference (3rd Edition) (Developer's Library)")
def testBrowseNodes(self): books = ecs.ItemLookup(self.ItemId, ResponseGroup='BrowseNodes') self.assertEqual(len(books), 1) book = books[0] self.assertEqual(len(book.BrowseNodes), 11) bn = book.BrowseNodes[0] # iterate all the ancestors names = ('Perl', 'Programming', "O'Reilly", 'By Publisher') for x in names: self.assertEqual(bn.Name, x) bn = bn.Ancestors[0] self.assertEqual(bn.Name, 'Books')
def testMedium(self): books = ecs.ItemLookup(self.ItemId, ResponseGroup='Request,Medium') self.assertEqual(len(books), 1) book = books[0] self.assertEqual(book.ASIN, '0596009259') self.assertEqual(book.Title, 'Programming Python') self.assertEqual(book.Manufacturer, "O'Reilly Media, Inc.") self.assertEqual(book.ProductGroup, 'Book') self.assertEqual(book.Author, 'Mark Lutz') # EditorialReview self.assertEqual(len(book.EditorialReviews), 2) self.assertEqual(book.EditorialReviews[0].Source, 'Amazon.com') self.assertEqual(book.EditorialReviews[1].Source, 'Book Description') # Images self.assertEqual(len(book.ImageSets), 1) self.assertEqual(book.ImageSets[0].LargeImage.Height, '500') self.assertEqual(book.ImageSets[0].MediumImage.Height, '160') self.assertEqual(book.ImageSets[0].SmallImage.Height, '75') # ItemAttributes pass # ItemIds pass # OfferSummary self.assertNotEqual(book.OfferSummary.TotalNew, '0') self.assertIsInteger( book.OfferSummary.LowestNewPrice.Amount, 'book.OfferSummary.LowestNewPrice.Amount is not integer') # SalesRank self.assertNotEqual(book.SalesRank, '0')
def fetch(page, asin, locale): field_map = [ (['Title'], 'title'), (['ISBN'], 'isbn'), (['Publisher'], 'publisher'), (['Edition'], 'edition'), (['Binding'], 'how_published'), ] amazon_type = extract(page, ["ProductGroup"]) if amazon_type != "Book": raise UserException( "This item on Amazon does not appear to be a book. It looks like a %s" % amazon_type) else: yield ("type", "BOOK") for (path, our_name) in field_map: val = extract(page, path) if val: yield (our_name, tidy(val)) date = extract(page, ['PublicationDate']) if date: try: (year, month, day) = date.split("-") yield ("year", year) yield ("month", month) yield ("day", day) except ValueError: pass authors = extract(page, ['Author']) if authors: if isinstance(authors, basestring): yield ("author", tidy(authors)) else: for author in authors: yield ("author", tidy(author)) # We put the images in as linkouts. CiteULike uses these # internally to show cover images. for (theirs, ours) in [('SmallImage', 'IMGS'), ('MediumImage', 'IMGM'), ('LargeImage', 'IMGL')]: path = [theirs, "URL"] img_url = extract(page, path) if img_url: yield ("linkout", "\t".join([ours, "", img_url, "", ""])) isbn = extract(page, ['ISBN']) if isbn: yield ("linkout", "\t".join(["ISBN", "", isbn, "", ""])) title = extract(page, ["Title"]) if not title and isbn: yield ("status", "redirect\thttp://www.worldcat.org/isbn/%s" % isbn) sys.exit(0) def get_abstract(n): abstract = extract(n, ['EditorialReviews', 'EditorialReview', 'Content']) if abstract: return tidy(html2text(abstract)) seen_abstract = False if not seen_abstract: abstract = get_abstract(page) if abstract: yield ("abstract", abstract) seen_abstract = True # Linkouts to the ASINS yield ("linkout", "\t".join(["AZ-%s" % locale.upper(), "", asin, "", ""])) # Different amazons may know this product by a different # ASIN. Trawl through all of them and see who has this one. for other_locale in ["us", "uk", "de", "jp", "fr", "ca"]: if other_locale == locale: continue ecs.setLocale(other_locale) try: pages = ecs.ItemLookup(asin, ResponseGroup="Medium") except ecs.InvalidParameterValue: continue page = pages if not seen_abstract: abstract = get_abstract(page) if abstract: yield ("abstract", abstract) seen_abstract = True yield ("linkout", "\t".join(["AZ-%s" % other_locale.upper(), "", asin, "", ""]))
for searchField in fields: if (searchLocale != "" and searchField != ""): collection = doc.createElement("List") collection.setAttribute("name", "Amazon Import") root.appendChild(collection) pythonBooks = None if searchField == "authors": pythonBooks = ecs.ItemSearch('', Author=query, SearchIndex=searchMode) elif searchField == "asin": pythonBooks = ecs.ItemLookup(query) else: pythonBooks = ecs.ItemSearch('', Title=query, SearchIndex=searchMode) for book in pythonBooks: try: bookElement = doc.createElement("Book") # book = ecs.ItemLookup(book.ASIN)[0] bookElement.setAttribute("title", book.Title) print(book.Title + ": " + str(dir(book))) for key in fieldMap.keys():
def testAccessories(self): # We have to use Palm Tungsten X txs = ecs.ItemLookup('B000BI7NHY', ResponseGroup='Accessories') self.assertEqual(len(txs), 1) tx = txs[0] self.assertEqual(tx.Accessories[4].ASIN, 'B00006BB9E')
def testVariationMinimum(self): shirts = ecs.ItemLookup('B000EI6M5A', ResponseGroup='VariationMinimum') self.assertEqual(len(shirts), 1) shirt = shirts[0] self.assertEqual(shirt.Variations[0].ASIN, 'B000EG9PLU') self.assertEqual(shirt.Variations[5].ASIN, 'B000EG5DUM')