Skip to content

move get_betterworldbooks_metadata and get_amazon_metadata out of AffiliateLinks template#12393

Merged
RayBB merged 6 commits into
internetarchive:masterfrom
Sanket17052006:move-bwb-metadata-out-of-template
Apr 17, 2026
Merged

move get_betterworldbooks_metadata and get_amazon_metadata out of AffiliateLinks template#12393
RayBB merged 6 commits into
internetarchive:masterfrom
Sanket17052006:move-bwb-metadata-out-of-template

Conversation

@Sanket17052006

Copy link
Copy Markdown
Contributor

Closes #12271

  • Moves the get_betterworldbooks_metadata() network call out of AffiliateLinks.html into AffiliateLinksPartial.generate() in
    partials.py.
    Changes :
    partials.py: Pre-fetch BWB metadata in AffiliateLinksPartial and inject it into opts.
    AffiliateLinks.html: Remove the get_betterworldbooks_metadata call and read bwb_price and bwb_market_price directly from opts safely using .get().

Technical

Testing

  • I tested the /partials/AffiliateLinks.json endpoint locally to verify that the Better World Books metadata is now being successfully intercepted and injected by the Python backend before rendering the template.
  • the data-opts attribute in the response – it now includes bwb_price and bwb_market_price.

Screenshot

Stakeholders

@RayBB

@mekarpeles

Copy link
Copy Markdown
Member

Thanks for the contribution, @Sanket17052006!

🤖 Copilot has been assigned for an initial review.

@RayBB is assigned to this PR and currently has:

  • 8 open PR(s) of equal or higher priority to review first

Note: the linked issue (#12271) hasn't been triaged yet — triage happens on Mondays and Fridays.

PR triage checklist (maintainers / Pam)
  • PR description — not empty; explains what the change does and how to verify it
  • References an issue — PR body contains a #NNN reference
    • Linked issue is triaged — has a Priority: * label (not just Needs: Triage)
    • Linked issue is assigned — has at least one assignee
  • Commit history clean — no WIP/fixup/conflict noise; commit messages are meaningful
  • CI passing — no failing check-runs
  • Test cases present — if the change touches substantive logic, test coverage exists or is explained
  • Proof of testing — PR body includes a description of what was tested, a screenshot, or a video

Note

This comment was automatically generated by Pam, Open Library's Project AI Manager, on behalf of @mekarpeles. Pam is designed to provide status visibility, perform basic project management functions and relevant codebase research, and provide actionable feedback so contributors aren't left waiting.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors the AffiliateLinks partial rendering path so Better World Books (BWB) pricing metadata is fetched in Python before template render, rather than triggering a network call from inside the Templetor macro. This supports cleaner separation of concerns and is a step toward making the partial endpoint async-friendly.

Changes:

  • Pre-fetch BWB metadata in AffiliateLinksPartial.generate() and inject bwb_price / bwb_market_price into opts.
  • Update AffiliateLinks.html to read BWB pricing values from opts (via .get()) instead of calling get_betterworldbooks_metadata().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
openlibrary/plugins/openlibrary/partials.py Fetches and injects BWB pricing metadata into opts before rendering AffiliateLinks.
openlibrary/macros/AffiliateLinks.html Removes template-side BWB fetch; reads pre-injected price fields from opts.

Comment on lines +15 to +17
from openlibrary.core.vendors import get_betterworldbooks_metadata
from openlibrary.i18n import gettext as _
from openlibrary.plugins.openlibrary.code import is_bot

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

Importing is_bot from openlibrary.plugins.openlibrary.code here is very risky: code.py executes setup() at import time (including importing this partials module and calling partials.setup()), which creates a circular import and can break module import / app startup. Instead, read the bot flag directly from the request context (e.g., openlibrary.utils.request_context.req_context.get().is_bot) or move a lightweight is_bot helper into a side-effect-free module so partials.py never needs to import code.py.

Copilot uses AI. Check for mistakes.
Comment on lines +219 to +223
if not is_bot() and opts.get('prices') and isbn:
bwb_metadata = get_betterworldbooks_metadata(isbn)
if isinstance(bwb_metadata, dict):
opts['bwb_price'] = bwb_metadata.get('price')
opts['bwb_market_price'] = bwb_metadata.get('market_price')

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

AffiliateLinksPartial.generate() is invoked from an async FastAPI route (/partials/AffiliateLinks.json), but this code performs a synchronous network call (get_betterworldbooks_metadata) inline. That will block the event loop and reduce concurrency under load. Consider moving this into an async codepath (e.g., generate_async + await an async HTTP client) or at least offloading the blocking call to a threadpool/async_bridge wrapper so the FastAPI handler remains non-blocking.

Copilot uses AI. Check for mistakes.
Comment thread openlibrary/macros/AffiliateLinks.html Outdated
Comment on lines +49 to +53
if not is_bot() and prices and isbn:
bwb_metadata = get_betterworldbooks_metadata(isbn)
bwb['price'] = bwb_metadata and bwb_metadata.get('price')
bwb['price'] = opts.get('bwb_price')
if amazon:
amazon['price'] = bwb_metadata and bwb_metadata.get('market_price')
if amazon and not amazon['price']:
amazon['price'] = opts.get('bwb_market_price')
if amazon and not amazon.get('price'):

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

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

This change removes the template-side Better World Books fetch, but the template now only reads bwb_price/bwb_market_price from opts. Any synchronous (non-partial) renders of this macro that don’t pre-inject these fields will stop showing BWB/Amazon pricing (e.g. DonateModal.html calls macros.AffiliateLinks(book.title, ids) without the partial handler). To keep backward compatibility, either (1) add a fallback to fetch BWB metadata when these keys are absent, or (2) ensure all call sites that render AffiliateLinks with async_load=False populate these opts fields in Python before rendering.

Copilot uses AI. Check for mistakes.
@RayBB RayBB changed the title move get_betterworldbooks_metadata out of AffiliateLinks template move get_betterworldbooks_metadata and get_amazon_metadata out of AffiliateLinks template Apr 17, 2026

@RayBB RayBB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Excellent work here!

I extended this a little to also move the amz metadata out because it just made things a little clearer.

Tested on testing and it's working perfect!

@RayBB RayBB merged commit 0537876 into internetarchive:master Apr 17, 2026
3 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting Review/Merge from Staff to Done in Ray's Project Apr 17, 2026
IvanPisquiy06 pushed a commit to IvanPisquiy06/openlibrary that referenced this pull request Apr 27, 2026
IvanPisquiy06 pushed a commit to IvanPisquiy06/openlibrary that referenced this pull request Apr 27, 2026
Sadashii pushed a commit to Sadashii/openlibrary that referenced this pull request May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Refactor AffiliateLinks to Remove Template-Side Network Requests

4 participants