Skip to content

Commit

Permalink
WIP: Remove Solr step + use /borrow endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbarnes committed Aug 1, 2024
1 parent 8e7659a commit b3fb885
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 93 deletions.
47 changes: 17 additions & 30 deletions openlibrary/book_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,26 @@ def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:
Note: This will only work for solr records if the provider field was fetched
in the solr request. (Note: this field is populated from db)
"""
if providers := ed_or_solr.get('providers', []):

if ed_or_solr.get("ebook_access") == "public":
for tbp in PROVIDER_ORDER:
if isinstance(tbp, (DirectProvider, InternetArchiveProvider)):
continue
# Exit if another provider has an identifier.
if tbp.get_identifiers(ed_or_solr):
return []

return ["truthy-response"]

else:
identifiers = [
provider.url
for provider in map(Acquisition.from_json, ed_or_solr['providers'])
for provider in map(
Acquisition.from_json, ed_or_solr.get('providers', [])
)
if provider.ebook_access >= EbookAccess.PRINTDISABLED
]

to_remove = set()
for tbp in PROVIDER_ORDER:
# Avoid infinite recursion.
Expand All @@ -463,37 +477,10 @@ def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:
identifier for identifier in identifiers if identifier not in to_remove
]

else:
# TODO: Not implemented for search/solr yet
return []

def render_read_button(
self, ed_or_solr: Edition | dict, analytics_attr: Callable[[str], str]
):
acq_sorted = sorted(
(
p
for p in map(Acquisition.from_json, ed_or_solr.get('providers', []))
if p.ebook_access >= EbookAccess.PRINTDISABLED
),
key=lambda p: p.ebook_access,
reverse=True,
)
if not acq_sorted:
return ''

acquisition = acq_sorted[0]
# pre-process acquisition.url so ParseResult.netloc is always the domain. Only netloc is used.
url = (
"https://" + acquisition.url
if not acquisition.url.startswith("http")
else acquisition.url
)
parsed_url = parse.urlparse(url)
domain = parsed_url.netloc
return render_template(
self.get_template_path('read_button'), acquisition, domain
)
return render_template(self.get_template_path('read_button'), ed_or_solr)

def render_download_options(self, edition: Edition, extra_args: list | None = None):
# Return an empty string until #9581 is addressed.
Expand Down
8 changes: 2 additions & 6 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ msgstr ""
msgid "YAML Representation:"
msgstr ""

#: BookPreview.html book_providers/direct_read_button.html
#: books/edit/edition.html editpage.html
#: BookPreview.html books/edit/edition.html editpage.html
msgid "Preview"
msgstr ""

Expand Down Expand Up @@ -3099,10 +3098,7 @@ msgid "Read free online"
msgstr ""

#: book_providers/direct_read_button.html
#, python-format
msgid ""
"This book is freely available from <a href=\"%s\">%s</a>, an external "
"third-party book provider."
msgid "This book is freely available from an external third-party book provider."
msgstr ""

#: book_providers/gutenberg_download_options.html
Expand Down
3 changes: 0 additions & 3 deletions openlibrary/plugins/worksearch/schemes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,3 @@ def q_to_solr_params(
cur_solr_params: list[tuple[str, str]],
) -> list[tuple[str, str]]:
return [('q', q)]

def add_non_solr_fields(self, solr_fields: set[str], solr_result: dict) -> None:
raise NotImplementedError()
36 changes: 1 addition & 35 deletions openlibrary/plugins/worksearch/schemes/works.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class WorkSearchScheme(SearchScheme):
}
non_solr_fields = {
'description',
'providers',
}
facet_fields = {
"has_fulltext",
Expand Down Expand Up @@ -169,6 +168,7 @@ class WorkSearchScheme(SearchScheme):
'author_key',
'title',
'subtitle',
'ebook_access',
'edition_count',
'ia',
'has_fulltext',
Expand Down Expand Up @@ -514,40 +514,6 @@ def convert_work_query_to_edition_query(work_query: str) -> str:
new_params.append(('editions.fl', ','.join(edition_fields)))
return new_params

def add_non_solr_fields(self, non_solr_fields: set[str], solr_result: dict) -> None:
from openlibrary.plugins.upstream.models import Edition

# Augment with data from db
edition_keys = [
ed_doc['key']
for doc in solr_result['response']['docs']
for ed_doc in doc.get('editions', {}).get('docs', [])
]
editions = cast(list[Edition], web.ctx.site.get_many(edition_keys))
ed_key_to_record = {ed.key: ed for ed in editions if ed.key in edition_keys}

from openlibrary.book_providers import get_book_provider

for doc in solr_result['response']['docs']:
for ed_doc in doc.get('editions', {}).get('docs', []):
# `ed` could be `None` if the record has been deleted and Solr not yet updated.
if not (ed := ed_key_to_record.get(ed_doc['key'])):
continue

for field in non_solr_fields:
val = getattr(ed, field)
if field == 'providers':
provider = get_book_provider(ed)
if not provider:
continue
ed_doc[field] = [
p.__dict__ for p in provider.get_acquisitions(ed)
]
elif isinstance(val, infogami.infobase.client.Nothing):
continue
elif field == 'description':
ed_doc[field] = val if isinstance(val, str) else val.value


def lcc_transform(sf: luqum.tree.SearchField):
# e.g. lcc:[NC1 TO NC1000] to lcc:[NC-0001.00000000 TO NC-1000.00000000]
Expand Down
41 changes: 22 additions & 19 deletions openlibrary/templates/book_providers/direct_read_button.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
$def with(acquisition, domain)
$# :param Acquisition acquisition:
$# :param domain str:
$ clean_domain = domain.replace(".", "")
$def with(doc)
$# :param Edition | web.Storage doc:

$if acquisition.access == 'open-access':
$ key = doc['key']
$ title = doc['title']
$ url = "%s/%s/borrow?action=read" % (key, title)

$# $if acquisition.access == 'open-access':
$if True:
<div class="cta-button-group">
<a
href="$(acquisition.url)"
href="$(url)"
title="$_('Read free online')"
class="cta-btn cta-btn--available cta-btn--read cta-btn--external cta-btn--direct"
target="_blank"
aria-haspopup="true"
aria-controls="direct-provider-toast-$(clean_domain)"
aria-controls="direct-provider-toast"
>$_('Read')</a>
</div>

$elif acquisition.access == 'sample':
<div class="cta-button-group">
<a class="cta-btn cta-btn--shell cta-btn--external"
data-ol-link-track="CTAClick|Preview"
target="_blank"
href="$(acquisition.url)"
>$_('Preview')</a>
</div>
$# $elif acquisition.access == 'sample':
$# <div class="cta-button-group">
$# <a class="cta-btn cta-btn--shell cta-btn--external"
$# data-ol-link-track="CTAClick|Preview"
$# target="_blank"
$# href="$(acquisition.url)"
$# >$_('Preview')</a>
$# </div>

$if render_once('direct-provider-toast-' + clean_domain):
$if render_once('direct-provider-toast'):
<div
class="toast toast--book-provider"
data-toast-trigger="[aria-controls=direct-provider-toast-$(clean_domain)]"
id="direct-provider-toast-$(clean_domain)"
data-toast-trigger="[aria-controls=direct-provider-toast]"
id="direct-provider-toast"
style="display:none"
>
<div class="toast__body">
$:_('This book is freely available from <a href="%s">%s</a>, an external third-party book provider.', acquisition.url, domain)
$:_('This book is freely available from an external third-party book provider.')
<a href="/trusted-book-providers#web-books">$_('Learn more')<a>
</div>
<a class="toast__close">&times;<span class="shift">$_("Close")</span></a>
Expand Down

0 comments on commit b3fb885

Please sign in to comment.