Skip to content

Commit 08edaba

Browse files
committed
Open Library integration for high-res covers 📚
1 parent ea2b754 commit 08edaba

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pythonbits/bb.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from . import imagehosting
2727
from . import goodreads
2828
from .googlebooks import find_cover, find_categories
29+
from .openlibrary import format_cover_url
2930
from .ffmpeg import FFMpeg
3031
from . import templating as bb
3132
from .submission import (Submission, form_field, finalize, cat_map,
@@ -1099,9 +1100,11 @@ def _render_description(self):
10991100
@finalize
11001101
@form_field('image')
11011102
def _render_cover(self):
1103+
if(config.get('Books', 'use_openlibrary').lower() == "true"):
1104+
return format_cover_url('isbn', self['summary']['isbn'], 'L')
11021105
# Goodreads usually won't give you a cover image as they don't have the
11031106
# the right to distribute them
1104-
if 'nophoto' in self['summary']['image_url']:
1107+
elif 'nophoto' in self['summary']['image_url']:
11051108
return find_cover(self['summary']['isbn'])
11061109
else:
11071110
return self['summary']['image_url']

pythonbits/openlibrary.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Note, read https://openlibrary.org/dev/docs/api/covers
6+
The cover access by ids other than CoverID and OLID are rate-limited.
7+
Currently only 100 requests/IP are allowed for every 5 minutes.
8+
If any IP tries to access more that the allowed limit,
9+
the service will return "403 Forbidden" status.
10+
"""
11+
12+
from .logging import log
13+
14+
API_URL = 'http://covers.openlibrary.org/b/{}/{}-{}.jpg'
15+
16+
17+
"""
18+
key can be any one of ISBN, OCLC, LCCN, OLID and ID (case-insensitive)
19+
value is the value of the chosen key
20+
size can be one of S, M and L for small, medium and large respectively.
21+
"""
22+
23+
24+
def format_cover_url(key, value, size):
25+
return API_URL.format(key, value, size)

0 commit comments

Comments
 (0)