Skip to content

Commit

Permalink
Properly handle Goodreads titles without covers
Browse files Browse the repository at this point in the history
Add some unit test coverage while we're at it.
  • Loading branch information
DavidCain committed Mar 1, 2019
1 parent 9eac4b8 commit 6b20b57
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 4 additions & 0 deletions bibliophile/goodreads.py
Expand Up @@ -43,6 +43,10 @@
def higher_quality_cover(image_url):
""" Modify a book cover to be higher quality. """
parsed = urlparse.urlparse(image_url)
if parsed.path.startswith('/assets/nophoto'):
# No known cover for this book! Just return the "no photo" image
return image_url

match = GOODREADS_IMAGE_REGEX.match(parsed.path)
if not match:
logger.warning("Goodreads image format changed! (%s) "
Expand Down
2 changes: 1 addition & 1 deletion bibliophile/syndetics.py
Expand Up @@ -17,7 +17,7 @@ def higher_quality_cover(image_url):
metadata, are left unmodified.
Documentation on the URL scheme can be found in the 'Syndetics Starter Page':
https://developers.exlibrisgroup.com/resources/voyager/code_contributions/SyndeticsStarterDocument.pdf
https://developers.exlibrisgroup.com/wp-content/uploads/2010/05/SyndeticsStarterDocument.pdf
"""
parsed = urlparse.urlparse(image_url)
params = urlparse.parse_qs(parsed.query)
Expand Down
26 changes: 26 additions & 0 deletions bibliophile/tests/test_goodreads.py
@@ -0,0 +1,26 @@
import unittest

from ..goodreads import higher_quality_cover


class CoverImageTests(unittest.TestCase):
def test_nophoto_url_unchanged(self):
""" We handle Goodreads' "nophoto" URL. """
nophoto = ("https://www.goodreads.com/"
"assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png")
image_url = higher_quality_cover(nophoto)
self.assertEqual(image_url, nophoto)

def test_medium_photo_enlarged(self):
""" We can produce higher-quality images for medium-sized cover. """
self.assertEqual(
higher_quality_cover('https://images.gr-assets.com/books/1436292289m/25663961.jpg'),
'https://images.gr-assets.com/books/1436292289l/25663961.jpg'
)

def test_small_photo_enlarged(self):
""" We can produce higher-quality images for a small-sized cover. """
self.assertEqual(
higher_quality_cover('https://images.gr-assets.com/books/1550917827s/1202.jpg'),
'https://images.gr-assets.com/books/1550917827l/1202.jpg'
)
20 changes: 14 additions & 6 deletions bibliophile/tests/test_syndetics.py
Expand Up @@ -4,9 +4,17 @@


class SyndeticsTest(unittest.TestCase):
def test_nophoto_url_unchanged(self):
""" We handle Goodreads' "nophoto" URL. """
nophoto = ("https://www.goodreads.com/"
"assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png")
image_url = higher_quality_cover(nophoto)
self.assertEqual(image_url, nophoto)
def test_unrecognize_images_returned_as_is(self):
""" If there's no ISBN in the URL, we don't modify the URL. """
img_url = 'https://secure.syndetics.com/index.aspx?fake_url'
self.assertEqual(higher_quality_cover(img_url), img_url)

def test_small_gif_becomes_large_jpeg(self):
""" We transform small images to larger ones. """
base_url = 'https://secure.syndetics.com/index.aspx'
self.assertEqual(
higher_quality_cover(
f'{base_url}?isbn=9780140449266/SC.GIF&client=sfpl&type=xw12&oclc='
),
f'{base_url}?isbn=9780140449266%2FLC.jpg&client=sfpl&type=xw12'
)

0 comments on commit 6b20b57

Please sign in to comment.