def qualifies_for_sponsorship(edition): """ :param edition edition: An infogami book edition :rtype: dict :return: A dict with book eligibility: { "edition": { "publishers": ["University of Wisconsin Press"], "number_of_pages": 262, "publish_date": "September 22, 2004", "cover": "https://covers.openlibrary.org/b/id/2353907-L.jpg", "title": "Lords of the Ring", "isbn": "9780299204204" "openlibrary_edition": "OL2347684W", "openlibrary_work": "OL10317216M" }, "price": { "scan_price_cents": 3444, "book_cost_cents": 298, "total_price_display": "$37.42", "total_price_cents": 3742 }, "is_eligible": true, "sponsor_url": "https://archive.org/donate?isbn=9780299204204&type=sponsorship&context=ol&campaign=pilot" } """ resp = { 'is_eligible': False, 'price': None } edition.isbn = edition.get_isbn13() edition.cover = edition.get('covers') and ( 'https://covers.openlibrary.org/b/id/%s-L.jpg' % edition.covers[0]) amz_metadata = get_amazon_metadata(edition.isbn) or {} req_fields = ['isbn', 'publishers', 'title', 'publish_date', 'cover', 'number_of_pages'] edition_data = dict((field, (amz_metadata.get(field) or edition.get(field))) for field in req_fields) work = edition.works and edition.works[0] if not (work and all(edition_data.values())): resp['error'] = { 'reason': 'Open Library is missing book metadata necessary for sponsorship', 'values': edition_data } return resp work_id = work.key.split("/")[-1] edition_id = edition.key.split('/')[-1] dwwi, matches = do_we_want_it(edition.isbn, work_id) if dwwi: bwb_price = get_betterworldbooks_metadata(edition.isbn).get('price_amt') if bwb_price: SETUP_COST_CENTS = 300 PAGE_COST_CENTS = 12 num_pages = int(edition_data['number_of_pages']) scan_price_cents = SETUP_COST_CENTS + (PAGE_COST_CENTS * num_pages) book_cost_cents = int(float(bwb_price) * 100) total_price_cents = scan_price_cents + book_cost_cents resp['price'] = { 'book_cost_cents': book_cost_cents, 'scan_price_cents': scan_price_cents, 'total_price_cents': total_price_cents, 'total_price_display': '${:,.2f}'.format( total_price_cents / 100. ), } resp['is_eligible'] = eligibility_check(edition) else: resp['error'] = { 'reason': 'matches', 'values': matches } edition_data.update({ 'openlibrary_edition': work_id, 'openlibrary_work': edition_id }) resp.update({ 'edition': edition_data, 'sponsor_url': lending.config_ia_domain + '/donate?' + urllib.urlencode({ 'campaign': 'pilot', 'type': 'sponsorship', 'context': 'ol', 'isbn': edition.isbn }) }) return resp
def qualifies_for_sponsorship(edition, scan_only=False, donate_only=False, patron=None): """ :param edition edition: An infogami book edition :rtype: dict :return: A dict with book eligibility: { "edition": { "publishers": ["University of Wisconsin Press"], "number_of_pages": 262, "publish_date": "September 22, 2004", "cover": "https://covers.openlibrary.org/b/id/2353907-L.jpg", "title": "Lords of the Ring", "isbn": "9780299204204" "openlibrary_edition": "OL2347684M", "openlibrary_work": "OL10317216W" }, "price": { "scan_price_cents": 3444, "book_cost_cents": 298, "total_price_display": "$37.42", "total_price_cents": 3742 }, "is_eligible": True, "sponsor_url": "https://archive.org/donate?isbn=9780299204204&type=sponsorship&context=ol&campaign=pilot" } """ resp = {'is_eligible': False, 'price': None} edition.isbn = edition.get_isbn13() edition.cover = edition.get('covers') and ( 'https://covers.openlibrary.org/b/id/%s-L.jpg' % edition.covers[0]) amz_metadata = edition.isbn and get_amazon_metadata(edition.isbn) or {} req_fields = [ 'isbn', 'publishers', 'title', 'publish_date', 'cover', 'number_of_pages', ] edition_data = { field: (amz_metadata.get(field) or edition.get(field)) for field in req_fields } work = edition.works and edition.works[0] if not (work and all(edition_data.values())): resp['error'] = { 'reason': 'Open Library is missing book metadata necessary for sponsorship', 'values': edition_data, } return resp work_id = edition.works[0].key.split("/")[-1] edition_id = edition.key.split('/')[-1] dwwi, matches = do_we_want_it(edition.isbn) if dwwi: num_pages = int(edition_data['number_of_pages']) bwb_price = None if not donate_only: if not scan_only: bwb_price = get_betterworldbooks_metadata( edition.isbn).get('price_amt') if scan_only or bwb_price: scan_price_cents = SETUP_COST_CENTS + (PAGE_COST_CENTS * num_pages) book_cost_cents = int(float(bwb_price) * 100) if not scan_only else 0 total_price_cents = scan_price_cents + book_cost_cents resp['price'] = { 'book_cost_cents': book_cost_cents, 'scan_price_cents': scan_price_cents, 'total_price_cents': total_price_cents, 'total_price_display': f'${total_price_cents / 100.0:,.2f}', } if donate_only or scan_only or bwb_price: resp['is_eligible'] = eligibility_check(edition, patron=patron) else: resp['error'] = {'reason': 'matches', 'values': matches} edition_data.update({ 'openlibrary_edition': edition_id, 'openlibrary_work': work_id }) resp.update({ 'edition': edition_data, 'sponsor_url': 'https://openlibrary.org/bookdrive', }) return resp