Skip to content

Commit

Permalink
Merge pull request #9555 from scottbarnes/9554/feature/add-lcp-downlo…
Browse files Browse the repository at this point in the history
…ad-links-to-internetarchiveprovider

Add LCP download links for IA
  • Loading branch information
mekarpeles authored Jul 12, 2024
2 parents 4affd73 + ad85270 commit 745e6ec
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
42 changes: 35 additions & 7 deletions openlibrary/book_providers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
12 changes: 8 additions & 4 deletions openlibrary/templates/book_providers/ia_download_options.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
<div class="cta-section">
<p class="cta-section-title">$_("Download Options")</p>
<ul class="ebook-download-options">
$if formats['pdf']:
$if formats.get('lcp_pdf'):
<li><a href="$formats['lcp_pdf']" title="$_('Download an LCP encrypted PDF from Internet Archive')">$_("Encrypted PDF")</a></li>
$if formats.get('lcp_epub'):
<li><a href="$formats['lcp_epub']" title="$_('Download an LCP encrypted ePub from Internet Archive')">$_("Encrypted ePub")</a></li>
$if formats.get('pdf'):
<li><a href="$formats['pdf']" title="$_('Download a PDF from Internet Archive')">$_("PDF")</a></li>
$if formats['txt']:
$if formats.get('txt'):
<li><a href="$formats['txt']" title="$_('Download a text version from Internet Archive')">$_("Plain text")</a></li>
$if formats['epub']:
$if formats.get('epub'):
<li><a href="$formats['epub']" title="$_('Download an ePub from Internet Archive')">$_("ePub")</a></li>
$if formats['mobi']:
$if formats.get('mobi'):
<li><a href="$formats['mobi']" title="$_('Download a MOBI file from Internet Archive')">$_("MOBI")</a></li>
<li><a href="$daisy" title="$_('Download open DAISY from Internet Archive (print-disabled format)')">$_("DAISY")</a></li>
</ul>
Expand Down

0 comments on commit 745e6ec

Please sign in to comment.