|
| 1 | +from typing import List, Optional, Union, Literal, cast |
| 2 | + |
| 3 | +import web |
| 4 | +from web import uniq |
| 5 | + |
| 6 | +from openlibrary.app import render_template |
| 7 | +from openlibrary.plugins.upstream.models import Edition |
| 8 | +from openlibrary.plugins.upstream.utils import get_coverstore_public_url |
| 9 | + |
| 10 | + |
| 11 | +class AbstractBookProvider: |
| 12 | + short_name: str |
| 13 | + |
| 14 | + """ |
| 15 | + The key in the identifiers field on editions; |
| 16 | + see https://openlibrary.org/config/edition |
| 17 | + """ |
| 18 | + identifier_key: str |
| 19 | + |
| 20 | + def get_identifiers(self, ed_or_solr: Union[Edition, dict]) -> List[str]: |
| 21 | + return ( |
| 22 | + # If it's an edition |
| 23 | + ed_or_solr.get('identifiers', {}).get(self.identifier_key, []) or |
| 24 | + # if it's a solr work record |
| 25 | + ed_or_solr.get(f'id_{self.identifier_key}', []) |
| 26 | + ) |
| 27 | + |
| 28 | + def choose_best_identifier(self, identifiers: List[str]) -> str: |
| 29 | + return identifiers[0] |
| 30 | + |
| 31 | + def get_best_identifier(self, ed_or_solr: Union[Edition, dict]) -> str: |
| 32 | + identifiers = self.get_identifiers(ed_or_solr) |
| 33 | + assert identifiers |
| 34 | + return self.choose_best_identifier(identifiers) |
| 35 | + |
| 36 | + def get_best_identifier_slug(self, ed_or_solr: Union[Edition, dict]) -> str: |
| 37 | + """Used in eg /work/OL1W?edition=ia:foobar URLs, for example""" |
| 38 | + return f'{self.short_name}:{self.get_best_identifier(ed_or_solr)}' |
| 39 | + |
| 40 | + def get_template_path(self, typ: Literal['read_button', 'download_options']) -> str: |
| 41 | + return f"book_providers/{self.short_name}_{typ}.html" |
| 42 | + |
| 43 | + def render_read_button(self, ed_or_solr: Union[Edition, dict]): |
| 44 | + return render_template( |
| 45 | + self.get_template_path('read_button'), |
| 46 | + self.get_best_identifier(ed_or_solr) |
| 47 | + ) |
| 48 | + |
| 49 | + def render_download_options(self, edition: Edition, extra_args: List = None): |
| 50 | + return render_template( |
| 51 | + self.get_template_path('download_options'), |
| 52 | + self.get_best_identifier(edition), |
| 53 | + *(extra_args or []) |
| 54 | + ) |
| 55 | + |
| 56 | + def get_cover_url(self, ed_or_solr: Union[Edition, dict]) -> Optional[str]: |
| 57 | + """ |
| 58 | + Get the cover url most appropriate for this copy when made available by this |
| 59 | + provider |
| 60 | + """ |
| 61 | + size = 'M' |
| 62 | + |
| 63 | + # Editions |
| 64 | + if isinstance(ed_or_solr, Edition): |
| 65 | + return ed_or_solr.get_cover().url(size) |
| 66 | + |
| 67 | + # Solr document augmented with availability |
| 68 | + availability = ed_or_solr.get('availability', {}) |
| 69 | + |
| 70 | + if availability.get('openlibrary_edition'): |
| 71 | + olid = availability.get('openlibrary_edition') |
| 72 | + return f"{get_coverstore_public_url()}/b/olid/{olid}-{size}.jpg" |
| 73 | + if availability.get('identifier'): |
| 74 | + ocaid = ed_or_solr['availability']['identifier'] |
| 75 | + return f"//archive.org/services/img/{ocaid}" |
| 76 | + |
| 77 | + # Plain solr - we don't know which edition is which here, so this is most |
| 78 | + # preferable |
| 79 | + if ed_or_solr.get('cover_i'): |
| 80 | + cover_i = ed_or_solr["cover_i"] |
| 81 | + return f'{get_coverstore_public_url()}/b/id/{cover_i}-{size}.jpg' |
| 82 | + if ed_or_solr.get('cover_edition_key'): |
| 83 | + olid = ed_or_solr['cover_edition_key'] |
| 84 | + return f"{get_coverstore_public_url()}/b/olid/{olid}-{size}.jpg" |
| 85 | + if ed_or_solr.get('ocaid'): |
| 86 | + return f"//archive.org/services/img/{ed_or_solr.get('ocaid')}" |
| 87 | + |
| 88 | + # No luck |
| 89 | + return None |
| 90 | + |
| 91 | + def is_own_ocaid(self, ocaid: str) -> bool: |
| 92 | + """Whether the ocaid is an archive of content from this provider""" |
| 93 | + return False |
| 94 | + |
| 95 | + |
| 96 | +class InternetArchiveProvider(AbstractBookProvider): |
| 97 | + short_name = 'ia' |
| 98 | + identifier_key = 'ocaid' |
| 99 | + |
| 100 | + def get_identifiers(self, ed_or_solr: Union[Edition, dict]) -> List[str]: |
| 101 | + # Solr work record augmented with availability |
| 102 | + if ed_or_solr.get('availability', {}).get('identifier'): |
| 103 | + return [ed_or_solr['availability']['identifier']] |
| 104 | + |
| 105 | + # Edition |
| 106 | + if ed_or_solr.get('ocaid'): |
| 107 | + return [ed_or_solr['ocaid']] |
| 108 | + |
| 109 | + # Solr work record |
| 110 | + return ed_or_solr.get('ia', []) |
| 111 | + |
| 112 | + def is_own_ocaid(self, ocaid: str) -> bool: |
| 113 | + return True |
| 114 | + |
| 115 | + def render_download_options(self, edition: Edition, extra_args: List = None): |
| 116 | + if edition.is_access_restricted() or not edition.ia_metadata: |
| 117 | + return '' |
| 118 | + |
| 119 | + formats = { |
| 120 | + 'pdf': edition.get_ia_download_link('.pdf'), |
| 121 | + 'epub': edition.get_ia_download_link('.epub'), |
| 122 | + 'mobi': edition.get_ia_download_link('.mobi'), |
| 123 | + 'txt': edition.get_ia_download_link('_djvu.txt'), |
| 124 | + } |
| 125 | + |
| 126 | + if any(formats.values()): |
| 127 | + return render_template( |
| 128 | + self.get_template_path('download_options'), |
| 129 | + formats, |
| 130 | + edition.url('/daisy')) |
| 131 | + else: |
| 132 | + return '' |
| 133 | + |
| 134 | + |
| 135 | +class LibriVoxProvider(AbstractBookProvider): |
| 136 | + short_name = 'librivox' |
| 137 | + identifier_key = 'librivox' |
| 138 | + |
| 139 | + def render_download_options(self, edition: Edition, extra_args: List = None): |
| 140 | + # The template also needs the ocaid, since some of the files are hosted on IA |
| 141 | + return super().render_download_options(edition, [edition.get('ocaid')]) |
| 142 | + |
| 143 | + def is_own_ocaid(self, ocaid: str) -> bool: |
| 144 | + return 'librivox' in ocaid |
| 145 | + |
| 146 | + |
| 147 | +class ProjectGutenbergProvider(AbstractBookProvider): |
| 148 | + short_name = 'gutenberg' |
| 149 | + identifier_key = 'project_gutenberg' |
| 150 | + |
| 151 | + def is_own_ocaid(self, ocaid: str) -> bool: |
| 152 | + return ocaid.endswith('gut') |
| 153 | + |
| 154 | + |
| 155 | +class StandardEbooksProvider(AbstractBookProvider): |
| 156 | + short_name = 'standard_ebooks' |
| 157 | + identifier_key = 'standard_ebooks' |
| 158 | + |
| 159 | + def is_own_ocaid(self, ocaid: str) -> bool: |
| 160 | + # Standard ebooks isn't archived on IA |
| 161 | + return False |
| 162 | + |
| 163 | + |
| 164 | +PROVIDER_ORDER: List[AbstractBookProvider] = [ |
| 165 | + # These providers act essentially as their own publishers, so link to the first when |
| 166 | + # we're on an edition page |
| 167 | + LibriVoxProvider(), |
| 168 | + ProjectGutenbergProvider(), |
| 169 | + StandardEbooksProvider(), |
| 170 | + # Then link to IA |
| 171 | + InternetArchiveProvider(), |
| 172 | +] |
| 173 | + |
| 174 | + |
| 175 | +def is_non_ia_ocaid(ocaid: str) -> bool: |
| 176 | + """ |
| 177 | + Check if the ocaid "looks like" it's from another provider |
| 178 | + """ |
| 179 | + providers = ( |
| 180 | + provider |
| 181 | + for provider in PROVIDER_ORDER |
| 182 | + if provider.short_name != 'ia') |
| 183 | + return any( |
| 184 | + provider.is_own_ocaid(ocaid) |
| 185 | + for provider in providers) |
| 186 | + |
| 187 | + |
| 188 | +def get_book_provider_by_name(short_name: str) -> Optional[AbstractBookProvider]: |
| 189 | + return next( |
| 190 | + (p for p in PROVIDER_ORDER if p.short_name == short_name), |
| 191 | + None |
| 192 | + ) |
| 193 | + |
| 194 | + |
| 195 | +def get_book_provider( |
| 196 | + ed_or_solr: Union[Edition, dict] |
| 197 | +) -> Optional[AbstractBookProvider]: |
| 198 | + |
| 199 | + # On search results, we want to display IA copies first. |
| 200 | + # Issue is that an edition can be provided by multiple providers; we can easily |
| 201 | + # choose the correct copy when on an edition, but on a solr record, with all copies |
| 202 | + # of all editions aggregated, it's more difficult. |
| 203 | + # So we do some ugly ocaid sniffing to try to guess :/ Idea being that we ignore |
| 204 | + # OCAIDs that look like they're from other providers. |
| 205 | + prefer_ia = not isinstance(ed_or_solr, Edition) |
| 206 | + if prefer_ia: |
| 207 | + ia_provider = cast(InternetArchiveProvider, get_book_provider_by_name('ia')) |
| 208 | + ia_ocaids = [ |
| 209 | + ocaid |
| 210 | + # Subjects/publisher pages have ia set to a specific value :/ |
| 211 | + for ocaid in uniq(ia_provider.get_identifiers(ed_or_solr) or []) |
| 212 | + if not is_non_ia_ocaid(ocaid) |
| 213 | + ] |
| 214 | + prefer_ia = bool(ia_ocaids) |
| 215 | + |
| 216 | + default_order = PROVIDER_ORDER |
| 217 | + if prefer_ia: |
| 218 | + ia_provider = cast(InternetArchiveProvider, get_book_provider_by_name('ia')) |
| 219 | + default_order = uniq([ia_provider, *PROVIDER_ORDER]) |
| 220 | + |
| 221 | + provider_order = default_order |
| 222 | + provider_overrides = web.input(providerPref=None).providerPref |
| 223 | + |
| 224 | + if provider_overrides: |
| 225 | + new_order: List[AbstractBookProvider] = [] |
| 226 | + for name in provider_overrides.split(','): |
| 227 | + if name == '*': |
| 228 | + new_order += default_order |
| 229 | + else: |
| 230 | + provider = get_book_provider_by_name(name) |
| 231 | + if not provider: |
| 232 | + # TODO: Show the user a warning somehow |
| 233 | + continue |
| 234 | + new_order.append(provider) |
| 235 | + new_order = uniq(new_order + default_order) |
| 236 | + if new_order: |
| 237 | + provider_order = new_order |
| 238 | + |
| 239 | + for provider in provider_order: |
| 240 | + if provider.get_identifiers(ed_or_solr): |
| 241 | + return provider |
| 242 | + |
| 243 | + # No luck |
| 244 | + return None |
| 245 | + |
| 246 | + |
| 247 | +setattr(get_book_provider, 'ia', get_book_provider_by_name('ia')) |
0 commit comments