From bdcfe0e24feae560f5987c32ce1c10925068ca5d Mon Sep 17 00:00:00 2001 From: "Michael E. Karpeles" Date: Fri, 8 Mar 2019 02:16:51 +0000 Subject: [PATCH] fixing amz isbn lookup to use ImportBot --- openlibrary/catalog/add_book/__init__.py | 18 +++++++++--------- openlibrary/core/vendors.py | 21 ++++++++++++++++++++- openlibrary/plugins/worksearch/code.py | 2 +- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/openlibrary/catalog/add_book/__init__.py b/openlibrary/catalog/add_book/__init__.py index 6d8cee509d5..94426a147eb 100644 --- a/openlibrary/catalog/add_book/__init__.py +++ b/openlibrary/catalog/add_book/__init__.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 diff --git a/openlibrary/core/vendors.py b/openlibrary/core/vendors.py index a24a44f95b9..e05330b2214 100644 --- a/openlibrary/core/vendors.py +++ b/openlibrary/core/vendors.py @@ -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'] diff --git a/openlibrary/plugins/worksearch/code.py b/openlibrary/plugins/worksearch/code.py index 787b9a1789a..6a295a2b95f 100644 --- a/openlibrary/plugins/worksearch/code.py +++ b/openlibrary/plugins/worksearch/code.py @@ -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 = []