Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing amz isbn lookup to use ImportBot #1960

Merged
merged 1 commit into from Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions openlibrary/catalog/add_book/__init__.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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.

Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion 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='

Expand Down Expand Up @@ -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']

Expand Down
2 changes: 1 addition & 1 deletion openlibrary/plugins/worksearch/code.py
Expand Up @@ -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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes search (if isbn is present, redirects gracefully, even if multiple other query params present)

self.isbn_redirect(i.isbn)

q_list = []
Expand Down