diff --git a/openlibrary/book_providers.py b/openlibrary/book_providers.py index ecca74f1c5d..cab994ec652 100644 --- a/openlibrary/book_providers.py +++ b/openlibrary/book_providers.py @@ -1,5 +1,6 @@ from typing import TypedDict, Literal, cast, TypeVar, Generic from collections.abc import Callable, Iterator +import urllib.parse import web from web import uniq @@ -142,16 +143,43 @@ def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]: def is_own_ocaid(self, ocaid: str) -> bool: return True + @staticmethod + def _has_file_suffix(edition_ia_metadata: dict, suffix: str) -> bool: + return any( + name + for name in edition_ia_metadata.get('_filenames', []) + if name.endswith(suffix) + ) + + @staticmethod + def _make_lcp_url(format: Literal['.lcpdf', 'lcp_epub'], ocaid: str) -> str: + endpoint = 'https://books-yaz.archive.org/services/loans/loan/' + params = { + 'action': 'borrow_book', + 'opds': '1', + 'redirect': '1', + 'identifier': ocaid, + 'format': 'lcp_pdf' if format == '.lcpdf' else format, + } + return f'{endpoint}?{urllib.parse.urlencode(params)}' + def render_download_options(self, edition: Edition, extra_args: list | None = None): - if edition.is_access_restricted(): + if not edition.ia_metadata: return '' - formats = { - 'pdf': edition.get_ia_download_link('.pdf'), - 'epub': edition.get_ia_download_link('.epub'), - 'mobi': edition.get_ia_download_link('.mobi'), - 'txt': edition.get_ia_download_link('_djvu.txt'), - } + formats = {} + if edition.is_access_restricted(): + if self._has_file_suffix(edition.ia_metadata, '.lcpdf'): + formats['lcp_pdf'] = self._make_lcp_url('.lcpdf', edition.ocaid) + if self._has_file_suffix(edition.ia_metadata, '_lcp.epub'): + formats['lcp_epub'] = self._make_lcp_url('lcp_epub', edition.ocaid) + else: + formats |= { + 'pdf': edition.get_ia_download_link('.pdf'), + 'epub': edition.get_ia_download_link('.epub'), + 'mobi': edition.get_ia_download_link('.mobi'), + 'txt': edition.get_ia_download_link('_djvu.txt'), + } if any(formats.values()): return render_template( diff --git a/openlibrary/i18n/messages.pot b/openlibrary/i18n/messages.pot index 58197727e84..4083c229d83 100644 --- a/openlibrary/i18n/messages.pot +++ b/openlibrary/i18n/messages.pot @@ -2337,6 +2337,22 @@ msgid "" "free eBooks." msgstr "" +#: book_providers/ia_download_options.html +msgid "Download an LCP encrypted PDF from Internet Archive" +msgstr "" + +#: book_providers/ia_download_options.html +msgid "Encrypted PDF" +msgstr "" + +#: book_providers/ia_download_options.html +msgid "Download an LCP encrypted ePub from Internet Archive" +msgstr "" + +#: book_providers/ia_download_options.html +msgid "Encrypted ePub" +msgstr "" + #: book_providers/ia_download_options.html msgid "Download a PDF from Internet Archive" msgstr "" diff --git a/openlibrary/templates/book_providers/ia_download_options.html b/openlibrary/templates/book_providers/ia_download_options.html index a32e226ba72..8770cad7c29 100644 --- a/openlibrary/templates/book_providers/ia_download_options.html +++ b/openlibrary/templates/book_providers/ia_download_options.html @@ -4,13 +4,17 @@
$_("Download Options")