mirror of
https://github.com/kemayo/leech
synced 2026-05-09 05:20:46 +02:00
Clean up cover downloading and add logging
This commit is contained in:
parent
e765594e9e
commit
d357bd17e2
1 changed files with 7 additions and 4 deletions
|
|
@ -5,6 +5,7 @@ import textwrap
|
||||||
import requests
|
import requests
|
||||||
import logging
|
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):
|
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)
|
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):
|
def make_cover_from_url(url, title, author):
|
||||||
try:
|
try:
|
||||||
|
logger.info("Downloading cover from " + url)
|
||||||
img = requests.Session().get(url)
|
img = requests.Session().get(url)
|
||||||
cover = BytesIO(img.content)
|
cover = BytesIO(img.content)
|
||||||
|
|
||||||
if Image.open(cover).format != "PNG":
|
if Image.open(cover).format != "PNG":
|
||||||
cover = _convert_to_png(cover)
|
cover = _convert_to_png(cover)
|
||||||
except:
|
except Exception as e:
|
||||||
#logger.info("Encountered an error downloading cover, reverting to default cover")
|
logger.info("Encountered an error downloading cover: " + e)
|
||||||
cover = make_cover(title, author)
|
cover = make_cover(title, author)
|
||||||
|
|
||||||
return cover
|
return cover
|
||||||
|
|
||||||
def _convert_to_png(image_bytestream):
|
def _convert_to_png(image_bytestream):
|
||||||
img = Image.open(image_bytestream)
|
|
||||||
png_image = BytesIO()
|
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)
|
png_image.seek(0)
|
||||||
|
|
||||||
return png_image
|
return png_image
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue