1
0
Fork 0
mirror of https://github.com/kemayo/leech synced 2026-03-23 12:52:51 +01:00

Clean up cover downloading and add logging

This commit is contained in:
Alex Raubach 2018-09-02 21:47:18 -04:00
parent e765594e9e
commit d357bd17e2

View file

@ -5,6 +5,7 @@ import textwrap
import requests
import logging
logger = logging.getLogger(__name__)
def make_cover(title, author, width=600, height=800, fontname="Helvetica", fontsize=40, bgcolor=(120, 20, 20), textcolor=(255, 255, 255), wrapat=30):
img = Image.new("RGBA", (width, height), bgcolor)
@ -31,20 +32,22 @@ def make_cover(title, author, width=600, height=800, fontname="Helvetica", fonts
def make_cover_from_url(url, title, author):
try:
logger.info("Downloading cover from " + url)
img = requests.Session().get(url)
cover = BytesIO(img.content)
if Image.open(cover).format != "PNG":
cover = _convert_to_png(cover)
except:
#logger.info("Encountered an error downloading cover, reverting to default cover")
except Exception as e:
logger.info("Encountered an error downloading cover: " + e)
cover = make_cover(title, author)
return cover
def _convert_to_png(image_bytestream):
img = Image.open(image_bytestream)
png_image = BytesIO()
img.save(png_image, format="PNG")
Image.open(image_bytestream).save(png_image, format="PNG")
png_image.name = 'cover.png'
png_image.seek(0)
return png_image