mirror of
https://github.com/kemayo/leech
synced 2025-12-15 21:05:59 +01:00
Download cover images for RoyalRoad Stories
This commit is contained in:
parent
571e262735
commit
ea60ac5122
2 changed files with 17 additions and 1 deletions
|
|
@ -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)))
|
||||
|
||||
|
|
|
|||
|
|
@ -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"):
|
||||
|
|
|
|||
Loading…
Reference in a new issue