Exemple #1
0
    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)
Exemple #2
0
    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_from_amazon_metadata(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)
Exemple #3
0
def create_edition_from_amazon_metadata(isbn):
    """Fetches amazon metadata by isbn from affiliates API, attempts to
    create OL edition from metadata, and returns the resulting edition key
    `/key/OL..M` if successful or None otherwise
    """
    md = get_amazon_metadata(isbn)
    if md:
        reply = load_from_amazon_metadata(md)
        if reply and reply.get('success'):
            return reply['edition']['key']
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
    def GET(self):
        i = web.input(isbn='', asin='')
        if not (i.isbn or i.asin):
            return json.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_, id_type=id_type[:4]) or {},
            'betterworldbooks': get_betterworldbooks_metadata(id_)
            if id_type.startswith('isbn_')
            else {},
        }
        # if user supplied isbn_{n} fails for amazon, we may want to check the alternate isbn

        # 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
        # TODO: perform existing OL lookup by ASIN if supplied, if possible
        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'):
            book_key = create_edition_from_amazon_metadata(id_, id_type[:4])

        # 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 json.dumps(metadata)
Exemple #7
0
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