def GET(self, isbn): isbn = normalize_isbn(isbn) isbn_type = 'isbn_' + ('13' if len(isbn) == 13 else '10') metadata = { 'amazon': get_amazon_metadata(isbn) or {}, 'betterworldbooks': get_betterworldbooks_metadata(isbn) or {} } # if bwb fails and isbn10, try again with isbn13 if len(isbn) == 10 and \ metadata['betterworldbooks'].get('price') is None: isbn_13 = isbn_10_to_isbn_13(isbn) metadata['betterworldbooks'] = get_betterworldbooks_metadata( isbn_13) or {} # fetch book by isbn if it exists book = web.ctx.site.things({ 'type': '/type/edition', isbn_type: isbn, }) # if no OL edition for isbn, attempt to create if (not book) and metadata.get('amazon'): book = load(clean_amazon_metadata_for_load( metadata.get('amazon'))) # include ol edition metadata in response, if available if book: ed = web.ctx.site.get(book[0]) if ed: metadata['key'] = ed.key if getattr(ed, 'ocaid'): metadata['ocaid'] = ed.ocaid return simplejson.dumps(metadata)
def test_clean_amazon_metadata_for_load_subtitle(): amazon = {"publishers": ["Vintage"], "price": "$4.12 (used)", "physical_format": "paperback", "edition": "Reprint", "authors": [{"name": "David Grann"}], "isbn_13": ["9780307742483"], "price_amt": "4.12", "source_records": ["amazon:0307742482"], "title": "Killers of the Flower Moon: The Osage Murders and the Birth of the FBI", "url": "https://www.amazon.com/dp/0307742482/?tag=internetarchi-20", "offer_summary": {"lowest_new": 869, "amazon_offers": 1, "total_new": 57, "lowest_used": 412, "total_collectible": 2, "total_used": 133, "lowest_collectible": 1475}, "number_of_pages": "400", "cover": "https://images-na.ssl-images-amazon.com/images/I/51PP3iTK8DL.jpg", "languages": ["english"], "isbn_10": ["0307742482"], "publish_date": "Apr 03, 2018", "product_group": "Book", "qlt": "used"} result = clean_amazon_metadata_for_load(amazon) print(result) assert result['title'] == 'Killers of the Flower Moon: The Osage Murders and the Birth of the FBI' assert result.get('subtitle') is None assert result.get('full_title') is None
def GET(self): # @hornc, add: title='', asin='', authors='' i = web.input(isbn='', asin='') if not (i.isbn or i.asin): return simplejson.dumps({ 'error': 'isbn or asin required' }) id_ = i.asin if i.asin else normalize_isbn(i.isbn) id_type = 'asin' if i.asin else 'isbn_' + ('13' if len(id_) == 13 else '10') metadata = { 'amazon': get_amazon_metadata(id_) or {}, 'betterworldbooks': get_betterworldbooks_metadata(id_) if id_type.startswith('isbn_') else {} } # if isbn_13 fails for amazon, we may want to check isbn_10 also # xxx # if bwb fails and isbn10, try again with isbn13 if id_type == 'isbn_10' and \ metadata['betterworldbooks'].get('price') is None: isbn_13 = isbn_10_to_isbn_13(id_) metadata['betterworldbooks'] = isbn_13 and get_betterworldbooks_metadata( isbn_13) or {} # fetch book by isbn if it exists # if asin... for now, it will fail (which is fine) matches = web.ctx.site.things({ 'type': '/type/edition', id_type: id_, }) book_key = matches[0] if matches else None # if no OL edition for isbn, attempt to create if (not book_key) and metadata.get('amazon'): resp = load(clean_amazon_metadata_for_load( metadata.get('amazon'))) if resp and 'edition' in resp: book_key = resp.get('edition').get('key') # include ol edition metadata in response, if available if book_key: ed = web.ctx.site.get(book_key) if ed: metadata['key'] = ed.key if getattr(ed, 'ocaid'): metadata['ocaid'] = ed.ocaid return simplejson.dumps(metadata)
def GET(self): # @hornc, add: title='', asin='', authors='' i = web.input(isbn='', asin='') if not (i.isbn or i.asin): return simplejson.dumps({'error': 'isbn or asin required'}) id_ = i.asin if i.asin else normalize_isbn(i.isbn) id_type = 'asin' if i.asin else 'isbn_' + ( '13' if len(id_) == 13 else '10') metadata = { 'amazon': get_amazon_metadata(id_) or {}, 'betterworldbooks': get_betterworldbooks_metadata(id_) if id_type.startswith('isbn_') else {} } # if isbn_13 fails for amazon, we may want to check isbn_10 also # xxx # if bwb fails and isbn10, try again with isbn13 if id_type == 'isbn_10' and \ metadata['betterworldbooks'].get('price') is None: isbn_13 = isbn_10_to_isbn_13(id_) metadata[ 'betterworldbooks'] = isbn_13 and get_betterworldbooks_metadata( isbn_13) or {} # fetch book by isbn if it exists # if asin... for now, it will fail (which is fine) matches = web.ctx.site.things({ 'type': '/type/edition', id_type: id_, }) book_key = matches[0] if matches else None # if no OL edition for isbn, attempt to create if (not book_key) and metadata.get('amazon'): resp = load(clean_amazon_metadata_for_load(metadata.get('amazon'))) if resp and 'edition' in resp: book_key = resp.get('edition').get('key') # include ol edition metadata in response, if available if book_key: ed = web.ctx.site.get(book_key) if ed: metadata['key'] = ed.key if getattr(ed, 'ocaid'): metadata['ocaid'] = ed.ocaid return simplejson.dumps(metadata)
def test_clean_amazon_metadata_for_load_ISBN(): amazon = { "publishers": ["Oxford University Press"], "price": "$9.50 (used)", "physical_format": "paperback", "edition": "3", "authors": [{ "name": "Rachel Carson" }], "isbn_13": ["9780190906764"], "price_amt": "9.50", "source_records": ["amazon:0190906766"], "title": "The Sea Around Us", "url": "https://www.amazon.com/dp/0190906766/?tag=internetarchi-20", "offer_summary": { "amazon_offers": 1, "lowest_new": 1050, "total_new": 31, "lowest_used": 950, "total_collectible": 0, "total_used": 15 }, "number_of_pages": "256", "cover": "https://images-na.ssl-images-amazon.com/images/I/51XKo3FsUyL.jpg", "languages": ["english"], "isbn_10": ["0190906766"], "publish_date": "Dec 18, 2018", "product_group": "Book", "qlt": "used" } result = clean_amazon_metadata_for_load(amazon) # TODO: implement and test edition number assert isinstance(result['publishers'], list) assert result[ 'cover'] == 'https://images-na.ssl-images-amazon.com/images/I/51XKo3FsUyL.jpg' assert result['authors'][0]['name'] == 'Rachel Carson' assert result.get('isbn') is None assert result.get('isbn_13') == ['9780190906764'] assert result.get('isbn_10') == ['0190906766'] assert result.get('identifiers') is None # No Amazon id present assert result['source_records'] == ['amazon:0190906766'] assert result['publish_date'] == 'Dec 18, 2018' assert result['physical_format'] == 'paperback' assert result['number_of_pages'] == '256' assert result.get('price') is None assert result.get('qlt') is None assert result.get('offer_summary') is None
def test_clean_amazon_metadata_for_load_non_ISBN(): # results from get_amazon_metadata() -> _serialize_amazon_product() # available from /prices?asin=B000KRRIZI amazon = {"publishers": ["Dutton"], "languages": [], "price_amt": "74.00", "source_records": ["amazon:B000KRRIZI"], "title": "The Man With the Crimson Box", "url": "https://www.amazon.com/dp/B000KRRIZI/?tag=internetarchi-20", "price": "$74.00 (used)", "number_of_pages": None, "cover": "https://images-na.ssl-images-amazon.com/images/I/31aTq%2BNA1EL.jpg", "qlt": "used", "physical_format": "hardcover", "edition": "First Edition", "publish_date": "1940", "authors": [{"name": "H.S. Keeler"}], "product_group": "Book", "offer_summary": {"total_used": 1, "total_new": 0, "total_collectible": 0, "lowest_used": 7400, "amazon_offers": 0}} result = clean_amazon_metadata_for_load(amazon) # this result is passed to load() from vendors.create_edition_from_amazon_metadata() print(result) assert isinstance(result['publishers'], list) assert result['publishers'][0] == 'Dutton' assert result['cover'] == 'https://images-na.ssl-images-amazon.com/images/I/31aTq%2BNA1EL.jpg' assert result['authors'][0]['name'] == 'H.S. Keeler' assert result.get('isbn') is None assert result.get('isbn_13') is None assert result.get('isbn_10') is None assert result['identifiers']['amazon'] == ['B000KRRIZI'] assert result['source_records'] == ['amazon:B000KRRIZI'] assert result['publish_date'] == '1940'