Skip to content
Permalink
Browse files

Merge pull request #1960 from internetarchive/refactor/isbn-api

fixing amz isbn lookup to use ImportBot
  • Loading branch information...
mekarpeles committed Mar 8, 2019
2 parents 1b610b5 + bdcfe0e commit c0e48d8883b295b66fe879b00f2a686b816ec1f3
Showing with 30 additions and 11 deletions.
  1. +9 −9 openlibrary/catalog/add_book/__init__.py
  2. +20 −1 openlibrary/core/vendors.py
  3. +1 −1 openlibrary/plugins/worksearch/code.py
@@ -158,7 +158,7 @@ def new_work(edition, rec, cover_id=None):
w['key'] = wkey
return w

def load_data(rec):
def load_data(rec, account=None):
"""
Adds a new Edition to Open Library. Creates a new Work if required,
otherwise associates the new Edition with an existing Work.
@@ -193,7 +193,7 @@ def load_data(rec):
ekey = web.ctx.site.new_key('/type/edition')
cover_id = None
if cover_url:
cover_id = add_cover(cover_url, ekey)
cover_id = add_cover(cover_url, ekey, account=account)
edition['covers'] = [cover_id]

edits = []
@@ -427,7 +427,7 @@ def find_exact_match(rec, edition_pool):
return ekey
return False

def add_cover(cover_url, ekey):
def add_cover(cover_url, ekey, account=None):
"""
Adds a cover to coverstore and returns the cover id.
@@ -441,9 +441,9 @@ def add_cover(cover_url, ekey):
upload_url = coverstore_url + '/b/upload2'
if upload_url.startswith("//"):
upload_url = "{0}:{1}".format(web.ctx.get("protocol", "http"), upload_url)
user = accounts.get_current_user()
user = account or accounts.get_current_user()
params = {
'author': user.key,
'author': user.get('key') or user.get('_key'),
'data': None,
'source_url': cover_url,
'olid': olid,
@@ -518,7 +518,7 @@ def update_ia_metadata_for_ol_edition(edition_id):
data = item.metadata
return data

def load(rec):
def load(rec, account=None):
"""Given a record, tries to add/match that edition in the system.
Record is a dictionary containing all the metadata of the edition.
@@ -537,7 +537,7 @@ def load(rec):
edition_pool = build_pool(rec)
if not edition_pool:
# No match candidates found, add edition
return load_data(rec)
return load_data(rec, account=account)

match = early_exit(rec)
if not match:
@@ -553,7 +553,7 @@ def load(rec):

if not match:
# No match found, add edition
return load_data(rec)
return load_data(rec, account=account)

# We have an edition match at this point
need_work_save = need_edition_save = False
@@ -581,7 +581,7 @@ def load(rec):
# Add cover to edition
if 'cover' in rec and not e.covers:
cover_url = rec['cover']
cover_id = add_cover(cover_url, e.key)
cover_id = add_cover(cover_url, e.key, account=account)
if cover_id:
e['covers'] = [cover_id]
need_edition_save = True
@@ -1,12 +1,15 @@
import re
import web
import urllib2
import simplejson
from infogami import config
from infogami.utils.view import public
from . import lending, cache, helpers as h
from openlibrary.utils import dateutil
from openlibrary.utils.isbn import (
normalize_isbn, isbn_13_to_isbn_10, isbn_10_to_isbn_13)
from openlibrary.catalog.add_book import load
from openlibrary import accounts

BETTERWORLDBOOKS_API_URL = 'http://products.betterworldbooks.com/service.aspx?ItemId='

@@ -95,7 +98,23 @@ def create_edition_from_amazon_metadata(isbn):
"""
md = get_amazon_metadata(isbn)
if md:
reply = load(clean_amazon_metadata_for_load(md))
# Save token of currently logged in user (or no-user)
account = accounts.get_current_user()
auth_token = account.generate_login_code() if account else ''

try:
# Temporarily behave (act) as ImportBot for import
tmp_account = accounts.find(username='ImportBot')
web.ctx.conn.set_auth_token(tmp_account.generate_login_code())
reply = load(clean_amazon_metadata_for_load(md),
account=tmp_account)
except Exception as e:
web.ctx.conn.set_auth_token(auth_token)
raise e

# Return auth token to original user or no-user
web.ctx.conn.set_auth_token(auth_token)

if reply and reply.get('success'):
return reply['edition']['key']

@@ -477,7 +477,7 @@ def GET(self):

self.redirect_if_needed(i)

if 'isbn' in i and all(not v for k, v in i.items() if k != 'isbn'):
if 'isbn' in i:
self.isbn_redirect(i.isbn)

q_list = []

0 comments on commit c0e48d8

Please sign in to comment.
You can’t perform that action at this time.