diff --git a/ebook/__init__.py b/ebook/__init__.py index 3934dbc..ed52ebd 100644 --- a/ebook/__init__.py +++ b/ebook/__init__.py @@ -1,5 +1,6 @@ from .epub import make_epub from .cover import make_cover +from .cover import make_cover_from_url import datetime import requests @@ -105,7 +106,12 @@ def generate_epub(story, output_filename=None, cover_options={}): # The cover is static, and the only change comes from the image which we generate html = [('Cover', 'cover.html', cover_template)] - cover_image = ('images/cover.png', make_cover(story.title, story.author, **cover_options).read(), 'image/png') + if story.cover_url: + image = make_cover_from_url(story.cover_url, story.title, story.author) + else: + image = make_cover(story.title, story.author, **cover_options) + + cover_image = ('images/cover.png', image.read(), 'image/png') html.append(('Front Matter', 'frontmatter.html', frontmatter_template.format(now=datetime.datetime.now(), **metadata))) diff --git a/ebook/cover.py b/ebook/cover.py index cf56232..3f16622 100644 --- a/ebook/cover.py +++ b/ebook/cover.py @@ -2,6 +2,7 @@ from PIL import Image, ImageDraw, ImageFont from io import BytesIO import textwrap +import requests def make_cover(title, author, width=600, height=800, fontname="Helvetica", fontsize=40, bgcolor=(120, 20, 20), textcolor=(255, 255, 255), wrapat=30): @@ -27,6 +28,15 @@ def make_cover(title, author, width=600, height=800, fontname="Helvetica", fonts output.seek(0) return output +def make_cover_from_url(url, title, author): + try: + img = requests.Session().get(url) + cover = BytesIO(img.content) + except: + cover = make_cover(title, author) + + return cover + def _safe_font(preferred, *args, **kwargs): for font in (preferred, "Helvetica", "FreeSans", "Arial"):