Skip to content

Commit de4015d

Browse files
authored
Merge pull request #9567 from cdrini/hotfix/covers-8
Temporarily downscale 8XXXXXX-L.jpg covers
2 parents 707e375 + b1083fb commit de4015d

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

openlibrary/coverstore/code.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,32 @@ def notfound():
255255
elif key != 'id':
256256
value = self.query(category, key, value)
257257

258-
if not value or (value and safeint(value) in config.blocked_covers):
258+
value = safeint(value)
259+
if value is None or value in config.blocked_covers:
259260
return notfound()
260261

262+
if 8_820_000 > value >= 8_000_000 and size == "L":
263+
# This item is currently offline due to heavy traffic;
264+
# Fix incoming in the next ~week; See:
265+
# - https://webarchive.jira.com/browse/PBOX-3879
266+
# - https://github.com/internetarchive/openlibrary/issues/9560
267+
size = "M"
268+
261269
# redirect to archive.org cluster for large size and original images whenever possible
262270
if size in ("L", "") and self.is_cover_in_cluster(value):
263-
url = zipview_url_from_id(int(value), size)
271+
url = zipview_url_from_id(value, size)
264272
return web.found(url)
265273

266274
# covers_0008 batches [_00, _82] are tar'd / zip'd in archive.org items
267-
if isinstance(value, int) or value.isnumeric(): # noqa: SIM102
268-
if 8_820_000 > int(value) >= 8_000_000:
269-
prefix = f"{size.lower()}_" if size else ""
270-
pid = "%010d" % int(value)
271-
item_id = f"{prefix}covers_{pid[:4]}"
272-
item_tar = f"{prefix}covers_{pid[:4]}_{pid[4:6]}.tar"
273-
item_file = f"{pid}{'-' + size.upper() if size else ''}"
274-
path = f"{item_id}/{item_tar}/{item_file}.jpg"
275-
protocol = web.ctx.protocol
276-
return web.found(f"{protocol}://archive.org/download/{path}")
275+
if 8_820_000 > value >= 8_000_000:
276+
prefix = f"{size.lower()}_" if size else ""
277+
pid = "%010d" % value
278+
item_id = f"{prefix}covers_{pid[:4]}"
279+
item_tar = f"{prefix}covers_{pid[:4]}_{pid[4:6]}.tar"
280+
item_file = f"{pid}{'-' + size.upper() if size else ''}"
281+
path = f"{item_id}/{item_tar}/{item_file}.jpg"
282+
protocol = web.ctx.protocol
283+
return web.found(f"{protocol}://archive.org/download/{path}")
277284

278285
d = self.get_details(value, size.lower())
279286
if not d:
@@ -329,14 +336,9 @@ def get_ia_cover_url(self, identifier, size="M"):
329336
h,
330337
)
331338

332-
def get_details(self, coverid, size=""):
333-
try:
334-
coverid = int(coverid)
335-
except ValueError:
336-
return None
337-
339+
def get_details(self, coverid: int, size=""):
338340
# Use tar index if available to avoid db query. We have 0-6M images in tar balls.
339-
if isinstance(coverid, int) and coverid < 6000000 and size in "sml":
341+
if coverid < 6000000 and size in "sml":
340342
path = self.get_tar_filename(coverid, size)
341343

342344
if path:
@@ -350,12 +352,12 @@ def get_details(self, coverid, size=""):
350352

351353
return db.details(coverid)
352354

353-
def is_cover_in_cluster(self, coverid):
355+
def is_cover_in_cluster(self, coverid: int):
354356
"""Returns True if the cover is moved to archive.org cluster.
355357
It is found by looking at the config variable max_coveritem_index.
356358
"""
357359
try:
358-
return int(coverid) < IMAGES_PER_ITEM * config.get("max_coveritem_index", 0)
360+
return coverid < IMAGES_PER_ITEM * config.get("max_coveritem_index", 0)
359361
except (TypeError, ValueError):
360362
return False
361363

0 commit comments

Comments
 (0)