MCPcopy Create free account
hub / github.com/internetarchive/openlibrary / process_amazon_batch

Function process_amazon_batch

scripts/affiliate_server.py:419–459  ·  view source on GitHub ↗

Call the Amazon API to get the products for a list of isbn_10s/ASINs and store each product in memcache using amazon_product_{isbn_13 or b_asin} as the cache key.

(asins: Collection[PrioritizedIdentifier])

Source from the content-addressed store, hash-verified

417
418
419def process_amazon_batch(asins: Collection[PrioritizedIdentifier]) -> None:
420 """
421 Call the Amazon API to get the products for a list of isbn_10s/ASINs and store
422 each product in memcache using amazon_product_{isbn_13 or b_asin} as the cache key.
423 """
424 logger.info(f"process_amazon_batch(): {len(asins)} items")
425 try:
426 identifiers = [prioritized_identifier.identifier for prioritized_identifier in asins]
427 products = web.amazon_api.get_products(identifiers, serialize=True)
428 # stats_ol_affiliate_amazon_imports - Open Library - Dashboards - Grafana
429 # http://graphite.us.archive.org Metrics.stats.ol...
430 stats.increment(
431 "ol.affiliate.amazon.total_items_fetched",
432 n=len(products),
433 )
434 except Exception:
435 logger.exception(f"amazon_api.get_products({asins}, serialize=True)")
436 return
437
438 for product in products:
439 cache_key = make_cache_key(product) # isbn_13 or non-ISBN-10 ASIN.
440 cache.memcache_cache.set( # Add each product to memcache
441 f"amazon_product_{cache_key}", product, expires=WEEK_SECS
442 )
443
444 # Only proceed if config finds infobase db creds
445 if not config.infobase.get("db_parameters"): # type: ignore[attr-defined]
446 logger.debug("DB parameters missing from affiliate-server infobase")
447 return
448
449 # Skip staging no_import_identifiers for for import by checking AMZ source record.
450 no_import_identifiers = {identifier.identifier for identifier in asins if not identifier.stage_import}
451
452 books = [clean_amazon_metadata_for_load(product) for product in products if product.get("source_records")[0].split(":")[1] not in no_import_identifiers]
453
454 if books:
455 stats.increment(
456 "ol.affiliate.amazon.total_items_batched_for_import",
457 n=len(books),
458 )
459 get_current_batch(name="amz").add_items([{"ia_id": b["source_records"][0], "status": "staged", "data": b} for b in books])
460
461
462def seconds_remaining(start_time: float) -> float:

Callers 2

runMethod · 0.85
amazon_lookupFunction · 0.85

Calls 9

lenFunction · 0.85
make_cache_keyFunction · 0.85
get_current_batchFunction · 0.85
infoMethod · 0.80
add_itemsMethod · 0.80
get_productsMethod · 0.45
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected